Jump to content

[SOLVED] if statement to print in assoc while loop problem


phpdragon

Recommended Posts

I have the following code which works fine except I cant get the "No Stockist" to echo when there are no stockist for a state, it does echo the ones that are there, so I am guessing I need to do the check a different way.

 

<?php $qry=mysql_query("SELECT * FROM states WHERE code='$ccode' ORDER BY nameorder");
if (mysql_num_rows($qry)>0) {
	while ($sres=mysql_fetch_assoc($qry)) {
	echo "<tr><td class='presstext' width='430'><b><font color='yellow' size='3px'>".$sres['name']."</font></b><br/>";
	$usersql=mysql_query("SELECT * FROM users WHERE state='".$sres['name']."' && status='active' && stockist='yes' ORDER BY pcode");
		while ($reseller=mysql_fetch_assoc($usersql)) {
		if ($reseller['biz']!="") { echo "<ul><li><font size='2px'><b>".$reseller['biz']."</b></font></li><li>".$reseller['street']."</li><li>".$reseller['suburb']."   ".$reseller['pcode']."</li><li>".$reseller['phone']."</li></ul></td></tr>"; } else if ($reseller['biz']=="") { echo "<ul><li>No stockist</li></ul></td></tr>"; }
		}
	}
}?>

I'm not too sure about your db setup or what kind of data is in your db and how you handle empty cols etc but you could give this a go.

 

<?php 

$qry=mysql_query("SELECT * FROM states WHERE code='$ccode' ORDER BY nameorder");
   if (mysql_num_rows($qry)>0) {
      while ($sres=mysql_fetch_assoc($qry)) {
      	echo "<tr><td class='presstext' width='430'><b><font color='yellow' size='3px'>".$sres['name']."</font></b><br/>";
      	$usersql=mysql_query("SELECT * FROM users WHERE state='".$sres['name']."' && status='active' && stockist='yes' ORDER BY pcode");
        while ($reseller=mysql_fetch_assoc($usersql) && mysql_num_rows($usersql) > 0) {
         	if ($reseller['biz']!="") { 
			echo "<ul><li><font size='2px'><b>".$reseller['biz']."</b>
					</font></li><li>".$reseller['street']."</li><li>".$reseller['suburb']."  
					 ".$reseller['pcode']."</li><li>".$reseller['phone']."</li></ul></td></tr>"; 
		} 
		else if ($reseller['biz']==NULL) { 
			echo "<ul><li>No stockist</li></ul></td></tr>"; 
		}
         }
	 if(mysql_num_rows($usersql) == 0){
	 	echo '<ul><li>No stocklist</li></ul></td></tr>';
	 }
      }
   }
   
   	else{
   		echo 'No results returned for that state';
}
   
?>

I fixed it with some assistance from your code thank you, and then decided to change it so it only displays states that have resellers active in them, like i do with the country menu that calls this page like so.

 

<?php $qry=mysql_query("SELECT * FROM users WHERE country='$ccode' ORDER BY state");
if (mysql_num_rows($qry)>0) {
	while ($sres=mysql_fetch_assoc($qry)) {
		if ($sres['state']!="") {echo "<tr><td class='presstext' colspan='2' width='410px'><b><font color='yellow' size='3px'>".$sres['state']."</font></b></td></tr>";
		}
	$usersql=mysql_query("SELECT * FROM users WHERE state='".$sres['state']."' && status='active' && stockist='yes' ORDER BY pcode");
			while ($reseller=mysql_fetch_assoc($usersql)) {
				if ($reseller['biz']!="") { echo "<tr><td class='presstext' width='250px'><ul><li><font size='2px'><b>".$reseller['biz']."</b></font></li>
				<li>".$reseller['street']."</li>
				<li>".$reseller['suburb']."   ".$reseller['pcode']."</li>
				<li>".$reseller['phone']."</li></ul></td>
				<td valign='middles align='center' width='160px'>";
				if (!empty($reseller['logo'])) { echo "<img src='biz_logos/".$reseller['logo']."'></td></tr>"; } else { echo " </td></tr>"; }
				} 
			}
	}
} ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.