Jump to content

If i have a empty sql array how do i make it show?


Nismo240
 Share

Recommended Posts

Hi i have the following code

// Connects to your Databasemysql_connect($server, $dbuser, $dbpass) or die(mysql_error());mysql_select_db($database) or die(mysql_error());$data = mysql_query("SELECT * FROM `products` WHERE `type` = 'Desktop' AND `make` = 'apple' AND `viewable` = 'y'")or die(mysql_error());$x = "0";Print "<table summary='' width='100%' border='0' cellspacing='' cellpadding='10' align='center'>";Print "<tr>";while($info = mysql_fetch_array($data)){  extract($info);  if ($x == "0"){	Print "</tr><tr>";  }  $x++;  if ($x == "3"){	$x = "0";  } // EMPTY ARRAY CHECK  if ($info) {Print "<td align='center' valign='top' width='33%'>[box]<img class='aligncenter size-thumbnail wp-image-41' title='' src='http://blahblah.net/wordpress/wp-content/uploads/". $info['image'] . "' alt='' width='150' height='150' /><strong>". $info['make'] . " " . $info['model'] . " - $". $info['price'] . "</strong><BR>[tooltip text='<b>". $info['make'] . " " . $info['model'] . "</b><BR>". $info['desc'] . "'][More Info][/tooltip][/box]</td>";  } elseif (empty($info)) {   Print "<td align='center' valign='top' width='33%'>[box]Currently Out of This Make.[/box]</td>";  }// END}Print "</tr>";Print "</table>";

My code displays 3 items in a row then drops to a new row, etc.. How ever if my array is empty (Like if there are no Apple Desktops available) i would like it to display something like Sorry we are currently out of Apple Desktops right now. yada yada.. How would i go about doing this correctly.. Thanks.

Link to comment
Share on other sites

ok hmm i tried this

// Connects to your Databasemysql_connect($server, $dbuser, $dbpass) or die(mysql_error());mysql_select_db($database) or die(mysql_error());$data = mysql_query("SELECT * FROM `products` WHERE `type` = 'Desktop' AND `make` = 'apple' AND `viewable` = 'y'")or die(mysql_error());$x = "0";Print "<table summary='' width='100%' border='0' cellspacing='' cellpadding='10' align='center'>";Print "<tr>";while($info = mysql_fetch_array($data)){  extract($info);  if ($x == "0"){    Print "</tr><tr>";  }  $x++;  if ($x == "3"){    $x = "0";  }// EMPTY ARRAY CHECK$num_rows = mysql_num_rows($data);  if ($num_rows = 0) {  Print "<td align='center' valign='top' width='33%'>[box]Currently Out of This Make.[/box]</td>";  } else {Print "<td align='center' valign='top' width='33%'>[box]<img class='aligncenter size-thumbnail wp-image-41' title='' src='http://cheaptronics.net/wordpress/wp-content/uploads/". $info['image'] . "' alt='' width='150' height='150' /><strong>". $info['make'] . " " . $info['model'] . " - $". $info['price'] . "</strong><BR>[tooltip text='<b>". $info['make'] . " " . $info['model'] . "</b><BR>". $info['desc'] . "'][More Info][/tooltip][/box]</td>";  } // END}Print "</tr>";Print "</table>";

didnt work also moved the

$num_rows = mysql_num_rows($data);

up under

$data = mysql_query("SELECT * FROM `products` WHERE `type` = 'Desktop' AND `make` = 'apple' AND `viewable` = 'y'")or die(mysql_error());

doesnt work :/

Link to comment
Share on other sites

Guest So Called

What doesn't work? When something doesn't work you should explain exactly what you're seeing so people who are trying to help you won't have to guess. BTW:

if ($num_rows = 0)

I think you meant to use the equality == operator (two = signs). The test you're using will always be false.

Link to comment
Share on other sites

When i mean it doesnt work i mean

$num_rows = mysql_num_rows($data);

either isnt grabbing the info or

  if ($num_rows == 0) {  if ($num_rows = 0) {  if (!$num_rows) {

these dont work either. When i say they dont work its not showing

  Print "<td align='center' valign='top' width='33%'>[box]Currently Out of This Make.[/box]</td>";

which you would understand if you read my first post.. but either way i dont get any errors codes either..

Link to comment
Share on other sites

When i mean it doesnt work i mean
$num_rows = mysql_num_rows($data);

either isnt grabbing the info or

  if ($num_rows == 0) {   if ($num_rows = 0) {   if (!$num_rows) {

these dont work either. When i say they dont work its not showing

  Print "<td align='center' valign='top' width='33%'>[box]Currently Out of This Make.[/box]</td>";

which you would understand if you read my first post.. but either way i dont get any errors codes either..

no, you have to provide us more information. You aren't bothering to debug or echo any information, so you really have no idea what the values of anything are. We're in an even less able position to know that. Provide us context, trace the execution of your script with echo statements, echo the values of your variables. Do something first to show us that you are trying to figure it out at least. also, you haven't added any error display to your page either, so you might want to do that. see, you have to take some initiative first before you start slagging people off for trying to help you and provide feedback to your own problem
Link to comment
Share on other sites

Ok i appreciate your post, but the code snipplet in my first post is just that a snipplet i have

error_reporting(E_ALL);

at the top of my page and im not seeing any errors.. also i believe that the

$num_rows = mysql_num_rows($data);

Isnt right because

mysql_connect($server, $dbuser, $dbpass) or die(mysql_error());mysql_select_db($database) or die(mysql_error());$data = mysql_query("SELECT * FROM `products` WHERE `type` = 'Desktop' AND `make` = 'apple' AND `viewable` = 'y'")or die(mysql_error());$num_rows = mysql_num_rows($data);echo "You have " . $num_rows . " number of rows...";

just puts out

You have number of rows...
If i dont mind asking what else could i give to help you guys out ? :Pleased:
Link to comment
Share on other sites

Im sorry one second, now

mysql_connect($server, $dbuser, $dbpass) or die(mysql_error());mysql_select_db($database) or die(mysql_error());$data = mysql_query("SELECT * FROM `products` WHERE `type` = 'Desktop' AND `make` = 'apple' AND `viewable` = 'y'")or die(mysql_error());$num_rows = mysql_num_rows($data);echo "You have " . $num_rows . " number of rows...";

Seems to be outputting

You have 0 number of rows...
Which is correct, thats weird it wasnt working a min ago now it is lol.. well i guess now to move on to the if/else statement..
Link to comment
Share on other sites

Ok well nvm i figured it out :crazy:

mysql_connect($server, $dbuser, $dbpass) or die(mysql_error());mysql_select_db($database) or die(mysql_error());$data = mysql_query("SELECT * FROM `products` WHERE `type` = 'Desktop' AND `make` = 'apple' AND `viewable` = 'y'")or die(mysql_error());$num_rows = mysql_num_rows($data);$x = "0";Print "<table summary='' width='100%' border='0' cellspacing='' cellpadding='10' align='center'>";Print "<tr>";if ($num_rows == 0) {   Print "<td align='center' valign='top' width='33%'>[box]Currently Out of This Make.[/box]</td>";} else {while($info = mysql_fetch_array($data)){  extract($info);  if ($x == "0"){    Print "</tr><tr>";  }  $x++;  if ($x == "3"){    $x = "0";  }Print "<td align='center' valign='top' width='33%'>[box]<img class='aligncenter size-thumbnail wp-image-41' title='' src='http://cheaptronics.net/wordpress/wp-content/uploads/". $info['image'] . "' alt='' width='150' height='150' /><strong>". $info['make'] . " " . $info['model'] . " - $". $info['price'] . "</strong><BR>[tooltip text='<b>". $info['make'] . " " . $info['model'] . "</b><BR>". $info['desc'] . "'][More Info][/tooltip][/box]</td>";}}Print "</tr>";Print "</table>";

Works perfect.. if we dont have any Apple Desktops listed then it will show "Sorry we are currently out of stock etc etc.." and if we do have it in stock it will list them 3 per row :) thank you birbal and justsomeguy for pointing me in the direction of

mysql_num_rows();

Link to comment
Share on other sites

Ok i appreciate your post, but the code snipplet in my first post is just that a snipplet i have
error_reporting(E_ALL);

also, you haven't added any error display to your page either, so you might want to do that.
well, we didn't know that, but also reporting and displaying errors are not the same thing.http://php.net/manua...r-reporting.phphttp://www.php.net/m....display-errors this is why we ask these questions, to get this kind of information. either way, the issue turned out to be a logic error, not a syntax error. Edited by thescientist
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...