I'm stuck for a while on a piece of code of which I can't find the solution.. Tried a whole bunch of options but none of it seems to work.
I have a table with all my customers. Its showing their name, postcode etc. But I also want to show the amount of open orders in the same table.
I got these tables mysql tables:
TABLE 1
Tablename: customer
Columns: customer_ID, postcode, customer_since, customer_name
TABLE 2
Tablename: status
Columsn: status_ID, status_name
TABLE 3
Tablename: orders
Columns: order_ID, customer_ID, status_ID
So far this is my code:
$sql = mysql_query ("SELECT customer.customer_ID, customer.postcode, customer.since, customer.name
FROM customer
ORDER BY customer.customer_ID desc ");
echo '<table border="0" width="515" >
<tr>
<td>
<table cellspacing="0" cellpadding="0" border="0" width="515" id="table1" >
<tr>
<th width="60" align="center"><span class="tabledescription">Number:</span></td> //customernumber
<th width="155" align="center"><span class="tabledescription">Name:</span></td> //customername
<th width="100" align="center"><span class="tabledescription">Postcode:</span></td>//customerpostcode
<th width="100" align="center"><span class="tabledescription">Orders open:</span></td>//amount of open orders
<th width="100" align="center"><span class="tabledescription">Since:</span></td>//customer since
</tr>
</table>
</td>
</tr>
<tr>
<td>
<div style="width:565px; height:322px; overflow:auto;">
<table id="table1" cellspacing="0" cellpadding="0" border="0" width="575" >';
while($row = mysql_fetch_array($sql, MYSQL_ASSOC))
{
$id = $row['customer_ID'];
$name= $row['name'];
$postcode = $row['postcode'];
$status = $row['status'];
$since = $row['customer_since'];
$probleem = $row['probleem'];
$csince = date('d-m-Y', $since);
echo "<tr><td width=64><a style=' color: #009bce; text-decoration: none;' href='detailvieuwcustomer.php?id=".$id."'>".$id."</a></td>
<td width=160>$name</td>
<td width=105>$postcode</td>
<td width=105>amount</td>
<td width=105>$csince</td></tr>";
}
echo ' </table>
</div>
</td>
</tr>
</table>';
So far that is working and showing my 8 customers at this moment. I have 7 different status types for each order. The last one is that its delivered so that one isn't open. I made this code:
$statusnumber = 7;
$sql1 = mysql_query("SELECT * FROM order WHERE customer_ID = '". $id . " ' AND status_ID != '". $statusnumber . "' ");
while($prow = mysql_fetch_array($sql1, MYSQL_ASSOC))
{
$openstatus = $prow['storing_ID'];
echo $openstatus;
This one is showing me every order which doesn't has status_ID 7.
Now I don't know how to count the number of orders which got status_ID 1 - 6 and put the amount of open orders in the table behind the right customer.
I also tried to join the tables:
$sql = mysql_query("SELECT status.status_ID, order.status_ID, order.customer_ID, customer.customer_ID, customer.name, customer.postcode, customer.since
FROM order
INNER JOIN status on (status.status_ID = order.status_ID)
INNER JOIN customer on (customer.customer_ID = order.customer_customer_ID)
ORDER BY customer.customer_ID desc ");
But when I do that its showing me all my customers multiple times, because he's getting the customer_ID from orders, and I got around 30 orders. Its giving me a result like: 1,1,1,1,2,2,2,3,4,4,5,5,5,5 etc.
I just can't seem to display all customers 1 time with the right amount of orders they have open..
Help would be appreciated.
mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. – KermitGROUP BY customer.customer_ID
? – Geo