Jump to content

[SOLVED] PHP/MySQL problem/question


SocomNegotiator

Recommended Posts

A guy was helping me with this problem, but we have not been able to figure it out.

 

This is my data set:

 

id      |    user_id     |      item_id     |    quantity     | date
4      |    4              |      3            |    3               | 10/2/04
5      |    4              |      2            |    3               | 10/2/04
6      |    4              |      1            |    5               | 10/3/04

 

Now the date is what pairs the items to an order. So if items have the same date then they were ordered at the same time. Notice the first two items are the same date...so they were ordered at the same time. Now the third was a day later and it is the only one with its date...so this item was ordered by itself. Well I am trying to display the orders that a user has submitted. So order 1, 2, 3, etc....Each order can contain multiple items. So order 1 would contain the first two items and then order 2 would be the third item and so on by the items dates.

 

 

Here is the code that I have tried:

 

<?php
$user_id = $user->inf['id'];
$db->query('SELECT * FROM item ORDER BY date DESC') or die(mysql_error());
$row = $db->fetch_array();

$sql = "SELECT * FROM `order` WHERE user_id=".$user_id." ORDER BY `date` ASC";
$result = mysql_query($sql) or die(mysql_error());
$i=0;
while($i<mysql_num_rows($result))
{
    if(!isset($orders[mysql_result($result, $i, 'date')]))
     {
        $orders[mysql_result($result, $i, 'date')]['date'] = mysql_result($result, $i, 'date');
        $orders[mysql_result($result, $i, 'date')]['items'] = array(array(mysql_result($result, $i, 'item_id'), mysql_result($result, $i, 'amount')));
        $orders[mysql_result($result, $i, 'date')]['id'] = array(mysql_result($result, $i, 'id'));
     }
    else
     {
        $orders[mysql_result($result, $i, 'date')]['items'] .= array(mysql_result($result, $i, 'item_id'), mysql_result($result, $i, 'amount'));
        $orders[mysql_result($result, $i, 'date')]['id'] .= mysql_result($result, $i, 'id');
     }
    $i++;
}

print_r($orders);
?>

 

This is what this code is displaying on my page:

 

  Array (     [2008-07-17 17:05:58] => Array         (             [date] => 2008-07-17 17:05:58             [items] => ArrayArray             [id] => Array2         )      [2008-07-18 14:01:17] => Array         (             [date] => 2008-07-18 14:01:17             [items] => Array                 (                     [0] => Array                         (                             [0] => 1                             [1] => 1                         )                  )              [id] => Array                 (                     [0] => 3                 )          )  )

 

It should look like this in a way:

 

Order #1

      4      |    4              |      3            |    3              | 10/2/04

      5      |    4              |      2            |    3              | 10/2/04

 

Order #2

      6      |    4              |      1            |    5              | 10/3/04

 

Any help would be much appreciated...

Link to comment
https://forums.phpfreaks.com/topic/115598-solved-phpmysql-problemquestion/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.