I am trying to fetch order data from order table with its id number and show only fields which are not empty in a table. Above that, I want to show the contact details of the order pulled from the users table with username grabbed from the order table.
I considered using JOIN in the query but was getting confused as to how to display only the non empty fields of order form in a table and the contact details from users above as simple text in lines one below the other.
Finally I came up with the code below
if(isset($_POST['subsorder']))
{
include('../conn.php');
$ordt="";
if($sql=$orderdb->query("SELECT * FROM orders WHERE id='$_POST[sorder]'") or die($orderdb->error))
{
if($sql->num_rows!=0)
{
$info=$sql->fetch_assoc();
$un=$info['username'];
$h = "";
$d="";
while($row = $sql->fetch_array())
{
foreach($row as $key => $value)
{
if($key != 'id' && !is_numeric($key) && $value != 0)
{
$h.= "<th>$key</th>";
$d.= "<td>$value</td>";
}
}
}
}
if($sql2=$usersdb->query("SELECT * FROM users WHERE username='$un'") or die($usersdb->error))
$info2=$sql2->fetch_assoc();
echo $info2['firstname']." ".$info2['firstname']."<br>";
echo $info2['address1']."<br>".$info2['address2']."<br>".$info2['address2']."<br>";
echo $info2['phone']."<br>".$info2['email']."<br>";
echo "<table border='1' style='border-collapse:collapse'>
<tr>$h</tr>
<tr>$d</tr>
</table>";
}
}
But this is creating a problem. This echoes only the contact details derived from the info2 result set. not the table below derived from $row result set. But if I remove the info result set at the beginning, from the code, then table is shown ! But I will have to have the info result set because that pulls the username for use in the next query.
I am wondering whether having two fetch function on one query is the problem. Or I don't know if i have made some oversight or whether basically this usage is wrong.