only display if there are items in the table available

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

DirectoryConsultancy
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Oct 03, 2012 1:54 am

only display if there are items in the table available

Postby DirectoryConsultancy » Wed Oct 03, 2012 1:58 am

Members

41 posts
0 warning points


Posted 27 August 2012 - 03:27 PM

Hi, I have the following code to randomly select featured items.

there are sales sale_rent ='s'

and rentals sale_rent ='h'


i only want the items to dispaly if there are sale_rent ='s' in the table
or if there are sale_rent ='h' in the table
some countries may have only sales
some countries may have only rentals

not every country has both sales and rentals
if the country has only rentals then i only want sale_rent ='h' to display

if the country has only sales then i only want sale_rent ='s' to display

here is the code thanks

Code: Select all

<?php$select = "SELECT prop_id FROM property WHERE country= '$country' AND sale_rent ='s' AND ref <> 'GP' ";$result=mysql_query($select);$number_rows=mysql_num_rows($result);if($number_rows > 2){ $count=3;}else{    $count=$max;}$max=$number_rows;settype($max, "integer");$min=0;for($i=0;$i<=$count;$i++){$limit=rand($min,$max);        $select = "SELECT prop_id, ref, price, prop_type, town, province, country, location, image1 FROM property WHERE country= '$country' AND sale_rent ='s' LIMIT $limit, 1";$results = mysql_query($select, $link_id);$query_data = mysql_fetch_row($results);$prop_id = $query_data[0];$ref = $query_data[1];$price=($query_data[2]);$prop_type= $query_data[3];$town= $query_data[4];$province = $query_data[5];$country = $query_data[6];$location = $query_data[7];$url = $query_data[8];$url=trim($url);                                                                                                                                                $image_name="$url";echo"<td style=\"width:14%; border:#033387 solid 1px; padding:2px;\"><p class=\"featdesc\">For Sale<br />$location<br />$town<br />$province<br />$country<br />$prop_type</p><a href=\"property_sale.php?id=$ref_number\" title='$prop_type $province $country'><img src=\"$image_name\" width=\"150\" height=\"112\" class=\"featureimage2\" alt=\"$prop_type $province\" /></a><p class=\"pricefeat\">\n";if ($pound>0){        $price_dis ="&pound;". number_format($pound);}if($pound==0){        $price_dis ="&#8364;". number_format($price);}echo"$price_dis</p></td>\n";}echo"</tr><tr>\n";$select = "SELECT prop_id FROM property WHERE country= '$country' AND sale_rent ='h'";$result=mysql_query($select);$number_rows=mysql_num_rows($result);if($number_rows > 2){ $count=3;}else{    $count=$max;}$max=$number_rows;settype($max, "integer");$min=0;for($i=0;$i<=$count;$i++){$limit=rand($min,$max);$select = "SELECT prop_id, ref, price, prop_type, location, town, province, country, image1, sale_rent, rate1, rate2, rate3, currency FROM property WHERE country= '$country' AND sale_rent ='h' LIMIT $limit, 1";$results = mysql_query($select, $link_id);$query_data = mysql_fetch_row($results);$prop_id = $query_data[0];$ref = $query_data[1];$price=($query_data[2]);$prop_type= $query_data[3];$location= $query_data[4];$town= $query_data[5];$province = $query_data[6];$country = $query_data[7];$url = $query_data[8];$sale_rent= $query_data[9];$rate1=$query_data[10];$rate2=$query_data[11];$rate3=$query_data[12];$currency=$query_data[13];$url=trim($url);$image_name="$url";$rate=$rate3;if($rate==0)$rate=$rate2;if($rate==0)$rate=$rate1;if($rate==0)$rate=" on application";$symbol="&euro;";$period=" per Week";if($sale_rent=='l')$period=" per Month";if($currency=='p')$symbol= "&pound;";echo"<td style=\"width:14%; border:#033387 solid 1px; padding:2px;\"><p class=\"featdesc\">Holiday Rentals<br />$location<br />$town<br />$province<br />$country<br />$prop_type</p><a href=\"property_holiday.php?id=$ref_number\" title='$prop_type $province $country rental'><img src=\"$image_name\" width=\"150\" height=\"112\" class=\"featureimage2\" alt=\"$prop_type $province\" /></a><p class=\"pricefeat\">From<br />$symbol $rate<br />$period</p></td>\n";}?>

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: only display if there are items in the table available

Postby seandisanti » Thu Oct 04, 2012 1:05 pm

not going to mess around with your code since i don't have access to any sample data, but personally whenever i want to have something displayed or hidden based on a condition, i use inline css with a condition or ternary expression like this:

Code: Select all


<tr<?php echo ($showthis==1)? '' : echo ' style="display:none;"'; ?>>
   <td> data here </td>
</tr>



***edit*** removed extraneous space in tr tag


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 2 guests