Forum Moderators: coopster

Message Too Old, No Replies

Posting each item ordered in its own table row

how to post items down instead of across...

         

michlcamp

2:43 pm on Dec 18, 2005 (gmt 0)

10+ Year Member



Up until now I've had good success with entering individual orders from my shopping cart into its own MySql table row in the 'orders' table - this enters the order data ACROSS the row - using a query such as:

[2]$query = "INSERT INTO orders VALUES ('order_id','$item1','$item2','$item3','$subtotal','$shipping','$total')";[/2]

where $item# is actually the qty of product item ordered, then I can cross-reference against the products table and create a report and output the HTML

What I really need to do is post each product ordered to it's own table row...in other words, to create a row entry for each item ordered with fields like :

order_id ¦ item_num ¦ item_descr ¦ item_qty ¦ item_price ¦ item_total

this would give me a table that reads something like:

101 ¦ paint ¦ 5 gallon pail ¦ 2 ¦ 25.00 ¦ 50.00
101 ¦ brush¦ 2 inch broad ¦ 2 ¦ 10.00 ¦ 20.00
101 ¦ paint ¦ 1 gallon pail ¦ 3 ¦ 12.50 ¦ 37.50

(obviously I would have to include additional fields to those above)

can this be done?
I'm wondering how to write the INSERT INTO query so that it posts each item ordered across the row, then moves to the next row to enter the next item ordered, for as many items ordered?

hope this makes sense..
any/all help appreciated.
thanks in advance.
mc

justageek

3:18 pm on Dec 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks like you have the answers.

Just do a simple loop for each of the items.

Assuming the items and related information are in an array or could be put into an array then you could do:

for($i=0;$i<count($items_array);$i++){
// Do your insert query and relate the info to the order id
}

JAG

michlcamp

3:26 pm on Dec 18, 2005 (gmt 0)

10+ Year Member



I get what you're saying but I don't know how to write it...still learning as I go here...

michlcamp

3:55 pm on Dec 18, 2005 (gmt 0)

10+ Year Member



I failed to mention that the data I want to enter into the table is coming from a form - customer selects items ordered and then posts the form.
mc

justageek

4:48 pm on Dec 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have the info you need as you've been inserting them into the db as columns.

All you have to do now is loop through all the items and do the following:

for($i=0;$i<count($items_array);$i++){
// Do your insert query and relate the info to the order id
// This is just an example to insert the item(s) only
$query = "insert into db set item = ".$items_array[$i];
$rs = mysql_query($query);
}

Of course you will want to save all the values you need so add them to the query :-)

Once you do that loop you will have an entry for each item no matter how many there are and they will be related to the order id.

JAG