I have a code snippet that will return the rows of a table if the id matches the posted id, but when I place the elseif statement in to where if num_rows returns 0 or false, it doesn't post the table row properly.

I do know that if the statement returns 0 rows, it's considered as FALSE, but I cannot for the life of me figure out why it is not posting the table row as told. If I reverse the code, this functions as well through the if and elseif statement.

include '../../../db_connections/config.php';
include '../../../db_connections/opendb.php';

$query = "SELECT * FROM `items_db` WHERE `store_id` =$id AND type_id =1 AND `status_id` =1 ORDER BY `item_name` ASC";

$result = mysql_query($query,$connect) or mysql_error();
$num_rows = mysql_num_rows($result);
$row_count = mysql_num_rows($result);

$color1 = "#FFFFFF";
$color2 = "#F0F0F0";
$row_count = 0;

echo ("<table bgcolor='#FFFFFF' width='65%' border='1' cellspacing='1' cellpadding='1' align='center' style='border-collapse: collapse' bordercolor='#000000'>\n");
echo ("<tr align='center'>\n");
echo ("<td colspan='4' bgcolor='#FFCC00' align='center' valign='center'><font size='2' color='#800000' face='Tahoma'><b>Items&nbsp;($num_rows)</b></td>\n");
echo ("</tr>\n");
echo ("<tr align='center'>\n");
echo ("<td width='35%' align=center valign=top><b>Name</b></td>\n");
echo ("<td width='30%' align=center valign=top><b>Manufacturer</b></td>\n");
echo ("<td width='15%' align=center valign=top><b>Type</b></td>\n");
echo ("<td width='10%' align=center valign=top><b>Price</b></td>\n");
echo ("</tr>\n");

while ($r = mysql_fetch_array($result)) {

if ( $num_rows >= 1 ) {

$row_color = ($row_count % 2) ? $color1 : $color2;
echo ("<tr align='center'>\n");
echo ("<td bgcolor='$row_color' align='left' valign='center'>&nbsp;<a href='ride_detail.php?id=$r[ride_id]' onclick=\"NewWindow(this.href,'name','650','550','yes');return false\">$r[item_name]</a></td>\n");
echo ("<td bgcolor='$row_color' align='left' valign='center'>&nbsp;<a href='manu_detail.php?id=$r[manu_id]'>$r[manufacturer_name]</a></td>\n");
echo ("<td bgcolor='$row_color' align='left' valign='center'>&nbsp;$r[category]</td>\n");
echo ("<td bgcolor='$row_color' align='center' valign='center'>&nbsp;$r[current_price]</td>\n");
echo ("</tr>\n");

$row_count++;

} elseif ( $num_rows = 0 ) {

echo ("<tr align='center'>\n");
echo ("<td bgcolor='$row_color' colspan='4' align='left' valign='center'>&nbsp;<i>No Current Items</i></td>\n");
echo ("</tr>\n");

}

}
include '../../../db_connections/closedb.php';

Can anyone help?

    Using

    } elseif ( $num_rows = 0 ) { 
    

    You are setting $num_rows to be equal to zero, not checking if it is.

    Use

    } elseif ( $num_rows == 0 ) { 
    

    To compare if they are both the same

      Changed it to $num_rows == 0 and it still returns the same result where the "No Items Found" table row is not being posted under the table header and columns.

      Also, if I change this to a if and else statement, the same thing occurs in the else statement where I do not specify any conditions.

      😕

        If the query returns 0 rows, mysql_fetch_array($result) returns false, so nothing is printed in the loop.

        The problem is with logic and indentation: your check of the row count is done in the loop, by which time it is too late to print the "no rows" message.

          OK, I understand, anyway to correct that prior so it will print inside the loop?

            OK, I understand, anyway to correct that prior so it will print inside the loop?

            No. You have to change the logic and check for an empty result set before the loop. In pseudocode:

            if num rows > 0
                fetch row and loop while there are more rows to fetch
                    print row
                end loop
            else
                print "no current items" message
            end if

              OMG .. THANK YOU .. I feel kinda dumb now .. but thanks again, your a saviour

                You're welcome 🙂
                Remember to mark this thread as resolved (if it is) using the thread tools.

                  Write a Reply...