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>";
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>";}
$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>";
}
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>";
}
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..:)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.