Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Manipulating dates

Status
Not open for further replies.

lazydays

Technical User
Sep 29, 2003
93
GB
Hi guys!

I am having a complete mare and now can't see the wood for the trees!!

I have a table which holds orders - I want to be able to see how many times each customer has ordered, and when they last ordered.

I am there apart from the last time they ordered. Currently the date of their first order is returned, as the query takes the first date is comes to.
I am thinking that I will need to set up a new SQL query to run before I run the below, but my brain is completely mashed at the moment, so any help will be gratefully received!

$query = "SELECT orders_id, customers_id, count(*) AS number_of_orders, customers_name, customers_company, DATE_FORMAT(date_purchased, '%M %D %Y') AS purchase_date FROM orders GROUP BY customers_company";


$result = @mysql_query($query);



if ($result) {

echo '<table width="100%" align=center>
<tr><td valign=top><b>Company Name</b></td><td width="20%" align=center valign=top><b>Number of Orders</b></td><td width="5%">&nbsp;</td><td width="30%" valign=top><b>Date of Last Order</b></td></tr>';

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

if ($order_stats[customers_company] !="") {
if ($order_stats[customers_company] ==".") {
echo '<tr><td valign=top>=' . $order_stats[customers_name] . '</td><td align=center valign=top>' . $order_stats[number_of_orders] . '</td><td>&nbsp;</td><td valign=top>' . $order_stats[purchase_date] . '</td></tr>';
echo '<tr><td colspan=4 align=left><hr size=1 width="97%" color=336699></td></tr>';
} else {
echo '<tr><td valign=top>' . $order_stats[customers_company] . '</td><td align=center valign=top>' . $order_stats[number_of_orders] . '</td><td>&nbsp;</td><td valign=top>' . $order_stats[purchase_date] . '</td></tr>';
echo '<tr><td colspan=4 align=left><hr size=1 width="97%" color=336699></td></tr>';
}
} else {
echo '<tr><td valign=top>' . $order_stats[customers_name] . '</td><td align=center valign=top>' . $order_stats[number_of_orders] . '</td><td>&nbsp;</td><td valign=top>' . $order_stats[date] . '</td></tr>';
echo '<tr><td colspan=4 align=left><hr size=1 width="97%" color=336699></td></tr>';
}

}

echo '</table>';
}
 
You will, I think, have to perform two separate queries. Since the core of your question involves MySQL, not PHP, I recommnend that you post your question in the MySQL forum: forum436


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top