inserting looped variables?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

inserting looped variables?

Post by C_Calav »

hi guys,

havent been on here in a while as have been working alot on CSS.

i have the following piece of code:

Code: Select all

HTMLEOF;
    
    while($row = mysql_fetch_array($result))
    { 
    $Subtotal += ($rowї&quote;qty&quote;] * $rowї&quote;P_Price&quote;]);
    $count += $rowї&quote;qty&quote;];

    $rowї&quote;P_Price&quote;] = number_format($rowї&quote;P_Price&quote;], 2, &quote;.&quote;, &quote;,&quote;);
    $unittotal = ($rowї&quote;qty&quote;] * $rowї&quote;P_Price&quote;]); 
	
    $unittotal = number_format($unittotal, 2, &quote;.&quote;, &quote;,&quote;);

print <<<HTMLEOF

    <tr> 
	<td height=&quote;64&quote; valign=&quote;top&quote;><img src=./pics/{$rowї&quote;P_Stock&quote;]}.jpg alt=&quote;image of plane&quote; width=&quote;82&quote; height=&quote;62&quote; class=&quote;planeimage&quote; /></td>
	<td>{$rowї&quote;P_Name&quote;]}</td>
	<td colspan=&quote;2&quote; valign=&quote;middle&quote;>\${$rowї&quote;P_Price&quote;]}</td>
	<td align=&quote;left&quote; valign=&quote;middle&quote;>{$rowї&quote;qty&quote;]}</td>
	<td colspan=&quote;2&quote; valign=&quote;middle&quote;> \${$unittotal}</td>
    </tr>

HTMLEOF;

	} //You have to have this read as php 

	$shipping = $shipping*$count;

	$AircraftTotal = $Subtotal;
now near the end of my page i have a insert statement which i insert things like $AircraftTotal, $shipping etc.

thats all fine.

but in the above code there is a loop and i want to insert in to the database variables like this

Code: Select all

{$row["P_Name"]}


How would i go about inserting looped variables like this?

thanks alot guys


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

huh? you want to insert already stored data? okay..

basically, you build an insert query for each iteration of the loop and execute it.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hey feyd,

yeah i want whats in his shopping cart (items) to be inserted with his details in a feild in the database. only prob is that the items are dynamic.

sounds like a good idea.

how do i build a insert query? can you give me a small example?

thanx feyd
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's just like building an insert query from a form... :?
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

im confused feyd,

im trying to fill a feild up in MySQL with the users order. do you think i am going the best way about this?

im not to sure how to build a insert query in a loop.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the stored carts I've done, store only a few bits of information. Mostly, it's an id of the product, quantity, and userid of who to attach it to.

It's as simple as

Code: Select all

while(row = fetch_record())
{
  query(insert into table rowїid], rowїqty], userid);
}
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

Hi Feyd,

thanx for that.

what i am trying to do is insert the user's final order (cart and personal details) i need them stored so i can get it all emailed to me when the order goes through.

feyd, im not too sure how this works

Code: Select all

while(row = fetch_record()){  query(insert into table rowїid], rowїqty], userid);}
can you please explain?

much appreciated
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's meant to illustrate what needs to happen. Nothing more... hence the label "~pseudocode."
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hi feyd,

i can see that,

what does this do = while(row = fetch_record())

and how do i keep inserting into the same feild in my DB.

thankyou
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you want to insert all the data into a single field?

That can be done with an update (but is inefficient). Instead, I'd use PHP as a storage cache until all records have passed through. Then smash them into a string in some fashion and insert the data. However, I'd recommend not doing that. The reasons are as simple as there is a finite length to how many items could be in this field. As your product database grows, the larger and larger product id's will take up far more space than the oldest ones. This creates an imbalance of number of products I can have in my cart. Also, searching within this field for someone that has x product, may be annoying, if not difficult...

Altogether, I'd store each product they have in their cart in seperate rows within the table. (This is why I used 'userid' in the pseudocode above.) For more reasons and explanations about my path of choice, read this: http://www.oreilly.com/catalog/javadtab ... r/ch02.pdf
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hi feyd,

i thought about adding each product as it went through the loop to a string last night. the problem i ran into is that the quantity's the user ordered for each model we would not know.

this is getting tricky for me.

are you suggesting to store the data like a shopping cart? where you have like 4 lines in the DB for the one userID? and the 4 lines would represent the 4 different products?

thanx feyd for your help

UPDATE:

i am doing this when the user adds a item to the cart

Code: Select all

@mysql_query("INSERT INTO cart(cookieId, itemId, qty) VALUES('" . GetCartId() . "', $itemId, $qty)");
can i use the cookieId as a link or something like that to link to the user's orders?

because what i am trying to achieve in this script i am writing is once a order is processed it will email me all the users personal + order details.

will this be the answer i am looking for?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

provided the "cookieID" isn't deleted from the table moments before, yeah.. sort of.
Post Reply