Forum Moderators: coopster

Message Too Old, No Replies

for each loop help needed

         

dwighty

8:36 pm on Jul 9, 2007 (gmt 0)

10+ Year Member



Hi Guys,

Can anyone help with the following issue I have.

Basically I have a shop basket which stores a new line per item detailing the customers id name and quantity.

When the customer checkouts out i want to transfer all lines in the basket which relates to the user and store these as new lines in the checkout table. the checkout table will then have a couple of additional information to be added.

So far I have no code as am not really sure where to start.

I do have
$userid = $_SESSION['user_id'];

$sql = "SELECT * FROM shop_basket WHERE cust_id = '$userid'";
$result = mysql_query($sql) or die("Error: " . mysql_error());

Can I then do the following?

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

/* Then do the sql */
INSERT INTO ...
DELETE ...

}

THanks

dwighty

9:16 pm on Jul 9, 2007 (gmt 0)

10+ Year Member



I have answered my own question - I did add the sql in the while loop.

If there is a better way then I would be grateful to hear it.

Dwighty

jatar_k

10:21 pm on Jul 9, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you shouldn't need to have the delete in there though, you could do the delete after the loop completes I would think

though having the delete in the loop would allow you to only delete the ones that are successfully written to the new table

borntobeweb

6:59 am on Jul 10, 2007 (gmt 0)

10+ Year Member



If you're just copying the values over, you can use a single insert statement instead of a loop, something like:

insert into shop_checkout (col1, col2, ...) select col1, col2, ... from shop_basket WHERE cust_id = '$userid'

And as jatar_k mentioned, delete everything afterwards:

delete from shop_basket WHERE cust_id = '$userid'

This should be more efficient too since you don't need to fetch all the data back and forth.