0

I have a table in PHP which displays the result of the MySQL query.

The database table item_em has the price details of the items that are available in hypermarkets available in the database table hypermarket_em.There are some items that aren't available in the hypermarket. Hence when i display the prices in the table I want those cells with no price as N/A.

So my problem is I tried a code myself to display message when database table field has no value.But it doesn't work and I don't get any error as well. Any ideas much appreciated. Thank you.

Here is my code

$res = mysql_query("SELECT h_id FROM hypermarket_em") or die(mysql_error());
echo"<tbody><tr>";
while($row = mysql_fetch_array( $res ))
{   
    $result = mysql_query("SELECT item_name FROM items_em WHERE h_id=".$row['h_id'])or die(mysql_error());
    while($drop_4 = mysql_fetch_array( $result ))
    {
         echo"<th scope=row>".$drop_4['item_name']."</th>";
         $rslt = mysql_query("SELECT price FROM items_em WHERE item_name='".$drop_4['item_name']."' and h_id=".$row['h_id'])or die(mysql_error());
         if (mysql_num_rows($rslt) == 0)
             echo"<td>N/A</td>";
         while($drop = mysql_fetch_array( $rslt ))
         {
             echo"<td>".$drop['price']."</td>";
         }
    }
}
echo"</tr></tbody>";
4
  • Try to get size of the $res, like this: size($res), if zero, means no result. Commented Nov 25, 2012 at 8:55
  • i get error saying "size" is undefined..:( Commented Nov 25, 2012 at 9:08
  • Can you explain what the X and Y axis are? What column do they correspond to? Commented Nov 25, 2012 at 9:19
  • X=h_name from hypermarket_em ,Y=item_name from items_em,cells are the price in items_em Commented Nov 25, 2012 at 9:20

4 Answers 4

1

Try reversing your mysql_num_rows and check if you have > than 0 rows-

if (mysql_num_rows($rslt) > 0){
    while($drop = mysql_fetch_array( $rslt )){
      echo"<td>".$drop['price']."</td>";}
}
else{
     echo"<td>N/A</td>";}

Edit-

I think what you are wanting to do then is something like this-

while($drop = mysql_fetch_array( $rslt )){
    if ($drop['price'] == ''){
      echo"<td>N/A</td>";}
    else{
      echo"<td>".$drop['price']."</td>";}
Sign up to request clarification or add additional context in comments.

5 Comments

as many times as the price for the item for a particular hypermarket is not available
I have tried this code already. But the line "if ($drop['price'] == '')" doesn't seem to work, that's why i used mysql_num_rows..
what do you get when you echo mysql_num_rows($rslt)? Is it 6, what your screenshot shows?
Hey thanks. Now i get it. I have to write the code in my previous while loop.
Yeah your SELECT is only getting the row where h_id=".$row['h_id'], so you won't get additional rows until you loop again at the top with while($row = mysql_fetch_array( $res ))
0
$data = mysql_fetch_array( $res );
if (!empty($data)) {
   while($row = mysql_fetch_array( $res ))
   {   
      $result = mysql_query("SELECT item_name FROM items_em WHERE h_id=".$row['h_id'])or die(mysql_error());
      while($drop_4 = mysql_fetch_array( $result ))
      {
         echo"<th scope=row>".$drop_4['item_name']."</th>";
         $rslt = mysql_query("SELECT price FROM items_em WHERE item_name='".$drop_4['item_name']."' and h_id=".$row['h_id'])or die(mysql_error());
         if (mysql_num_rows($rslt) == 0)
             echo"<td>N/A</td>";
         while($drop = mysql_fetch_array( $rslt ))
         {
             echo"<td>".$drop['price']."</td>";
         }
      }
   }
} else {
   echo "<td>No record found</td>";
}

1 Comment

this did work. but the message is being displayed in wrong order.
0

Not really sure what you're trying to do here, but it seems like you might want to simplify your query as well. Try something like:

SELECT
    *
FROM
    hypermarket_em h
    LEFT JOIN items_em i ON ( h.h_id = i.h_id );

Using this query, you can do something like:

while($drop = mysql_fetch_array( $rslt )){
    echo"<td>" . ( empty( $drop['price'] ) ? 'N/A' : $drop['price'] ) . "</td>";
}

Comments

0

So here is my answer.

$res = mysql_query("SELECT DISTINCT h_id FROM hypermarket_em") or die(mysql_error());
echo"<tbody><tr>";
while($row = mysql_fetch_array( $res ))
{   
    $result = mysql_query("SELECT item_name FROM items_em WHERE h_id=".$row['h_id'])or die(mysql_error());
    while($drop_4 = mysql_fetch_array( $result ))
    {
            echo"<th scope=row>".$drop_4['item_name']."</th>";
            $rslt = mysql_query("SELECT price FROM items_em WHERE item_name='".$drop_4['item_name']."' and h_id=".$row['h_id'])or die(mysql_error());
        while($drop = mysql_fetch_array( $rslt ))
        {
            echo"<td>".$drop['price']."</td>";
        }
    }
}
$res = mysql_query("SELECT DISTINCT h_id FROM hypermarket_em") or die(mysql_error());
while($row = mysql_fetch_array( $res ))
{
    $result = mysql_query("SELECT item_name FROM items_em WHERE h_id=".$row['h_id'])or die(mysql_error());
    if(mysql_num_rows($result)<=0)
        echo"<td>N/A</td>";
}
echo"</tr></tbody>";

I had to execute the same query twice as i want the item names and the price values in the order. So I first print the item names and then only the prices or "N/A" message.

Thank you all for your help..:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.