Jump to content


Photo

Auto-incrementing


  • Please log in to reply
20 replies to this topic

#1 allydm

allydm
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 04 September 2006 - 08:33 PM

Hi there.

I'm building a shopping cart, and it needs to be intergrated into another piece of software (not made by me so i can't change how it works).
Basically, when someone clicks 'Buy' on the shopping cart on my site, i need all the information on that shopping cart to be passed to the payment processor made by the other company. They require post information, to be sent in this format:

<input type="hidden" name="item_name_X" value="Item">
<input type="hidden" name="amount_X" value="Price">

This information is required for each individual item, so say someone bought 2 items, the form would have to look like this:

<input type='hidden' name='item_name_1' value='Medium Ceramic Pot' />
<input type='hidden' name='amount_1' value='18.00' />
<input type='hidden' name='item_name_2' value='Small Ceramic Pot' />
<input type='hidden' name='amount_2' value='14.00' />

Notice how the item_name_1 changes to item_name_2 for the second item.
All the information on the items in the cart is collected from this query:

$product_query = "SELECT * FROM `robin_basket` WHERE session_id = '".session_id()."'";
	$product_result = mysql_query($product_query);
	$product_info = mysql_fetch_assoc($product_result);

So i need the form to be automatically generated for each item in the cart, with the item_name and amount incrementing by 1 for each new item.

Hope you understand, if you don't just tell me which bit you need clarification on (would really like to get this sorted).

allydm

#2 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 04 September 2006 - 09:13 PM

try
$product_query = "SELECT * FROM `robin_basket` WHERE session_id = '".session_id()."'";
$product_result = mysql_query($product_query);
echo '<form action ="some.php" metod="POST">';
while($product_info = mysql_fetch_assoc($product_result)) {
	echo "<input type='hidden' name='item_name_$i' value='{$product_info['name']}' />
	<input type='hidden' name='amount_$i' value='{$product_info['price']}' />";
	$i++;
}
echo '<input type="submit"></form>';


#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 04 September 2006 - 09:17 PM

There may be a slight issue with that. The array always starts with 0 so what I do is this

$i = 0;
$product_query = "SELECT * FROM `robin_basket` WHERE session_id = '".session_id()."'";
$product_result = mysql_query($product_query);
echo '<form action ="some.php" metod="POST">';
while($product_info = mysql_fetch_assoc($product_result)) {
	echo "<input type='hidden' name='item_name_[$i + 1]' value='{$product_info['name']}' />
	<input type='hidden' name='amount_[$i + 1]' value='{$product_info['price']}' />";
	$i++;

give this a try

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 04 September 2006 - 09:21 PM

Seems easier just to start with $i = 1
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 04 September 2006 - 09:23 PM

^..lol.. There I go tryin to over think things..lol

Good point Barand

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#6 allydm

allydm
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 04 September 2006 - 09:40 PM

Thankyou for the replies, unfortunatly the code above dosn't seem to work.
The contents of the cart arn't sent to the page (i have checked and its not the 3rd party payment processor not working).
The post data (that the php code above generates) isn't sent, i have added another field to the form to see if the form completely wasn't working, but the other field does work.

Any ideas?

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 04 September 2006 - 09:47 PM

Because you use "SELECT * FROM `robin_basket`..." there is no way of knowing what the column names are the robin_basket table.

Sasa assumes (reasonably) that the ones you are intersted in are "name" and "price".

Is that the case?

Better to specify the columns you want to select and don't just use "*" every time.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 allydm

allydm
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 04 September 2006 - 09:54 PM

I understand that, and when i inputted the code i changed the "name" and "price" to the correct column values.

The two columns are called product_name and product_price.

However the code still does not seem to work.

#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 04 September 2006 - 09:57 PM

Have you tried viewing the page source to look at the generated form code?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#10 allydm

allydm
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 04 September 2006 - 10:04 PM

No i hadn't! Sorry.

Basically, non of the php generated stuff is showing on the form. The entire code for the form is...

<form name="_xclick" action="test1.php" method="post">
		<input type="hidden" name="cmd" value="_cart">
		<input type="hidden" name="upload" value="1">
		<input type="hidden" name="business" value="******">
		<input type="hidden" name="currency_code" value="GBP">
	<?php
	while($product_info = mysql_fetch_assoc($product_result)) {
	echo "<input type='hidden' name='item_name_[$i + 1]' value='{$product_info['product_name']}' />
			<input type='hidden' name='amount_[$i + 1]' value='{$product_info['product_price']}' />";
	$i++;
	}
	?>
		<input type="image" src="******" border="0" name="submit">
	</form>

However only this...

  <form name="_xclick" action="test1.php" method="post">
		<input type="hidden" name="cmd" value="_cart">
		<input type="hidden" name="upload" value="1">
		<input type="hidden" name="business" value="*******">
		<input type="hidden" name="currency_code" value="GBP">
			<input type="image" src="*******" border="0" name="submit">
	</form>

is there on the page source.

Any ideas why? I can't see...the echo seems valid.

*edit*
The rest of the code is further up the page, and reads as...

$i = 1;
	$product_query = "SELECT * FROM `robin_basket` 
				WHERE session_id = '".session_id()."'";
	$product_result = mysql_query($product_query);


#11 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 04 September 2006 - 10:16 PM

Could be the query returns no results

Change
$product_result = mysql_query($product_query);

to
$product_result = mysql_query($product_query) or die (mysql_error() );

Also, echo $product_query to check the actual query that is being executed.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#12 allydm

allydm
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 04 September 2006 - 10:21 PM

Hi.
$product_query is being executed correctly, and the query is returning results.
Is there any more information i should provide to help with this?

#13 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 04 September 2006 - 10:34 PM

I cannot understnd why no input fields if the query produces results.

Add the line in red, it won't affect the form


        echo "There are ". mysql_num_rows($product_result) . "items<br>";
while($product_info = mysql_fetch_assoc($product_result)) {
echo "<input type='hidden' name='item_name_[$i + 1]' value='{$product_info['product_name']}' />
<input type='hidden' name='amount_[$i + 1]' value='{$product_info['product_price']}' />";
$i++;
}


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#14 allydm

allydm
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 04 September 2006 - 10:38 PM

That outputs...

"There are 3items"

#15 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 04 September 2006 - 10:58 PM

Curiouser and curiouser!

May as well get the field names right and remove those [$i + 1] 's, but it doesn't explain why they don't appear - should be 3 of them
while($product_info = mysql_fetch_assoc($product_result)) {
   echo "<input type='hidden' name='item_name_$i' value='{$product_info['product_name']}' />
         <input type='hidden' name='amount_$i' value='{$product_info['product_price']}' />";
   $i++;
   }

Are you looping through the query results in the code before getting to this bit?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#16 allydm

allydm
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 04 September 2006 - 11:02 PM

Are you looping through the query results in the code before getting to this bit?


I am, as it displays the current cart contents in a table further up the page...is this a problem?

#17 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 04 September 2006 - 11:04 PM

Yes.

You have already reached the end of the results so when you come to this loop there are no more to be read.

Do the query again immed before this while loop.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#18 allydm

allydm
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 04 September 2006 - 11:18 PM

We have progressed!

Okay, now i'm getting two of the three items information sent through, however one of the items isn't.

Items:
item_name_1
item_name_2
i am getting, but i'm not getting:
item_name_3

Now i've tried all sorts of combinations (been up to item_name_11), tried item_name, item_name_, item_name_0 however the third item just dosn't seem to appear.

Any ideas why? There are 3 rows fetched by the query, and 3 items are shown by the same query earlier in the page, but not here.


#19 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 04 September 2006 - 11:30 PM

It's 30 past midnight here and I'm up  at 6:30 so I'll have to look at it tomorrow. Post current code so I can see what's happening..
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#20 allydm

allydm
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 04 September 2006 - 11:58 PM

Here is the section of code:

$i = 1;
	$product_query1 = "SELECT * FROM `robin_basket` 
				WHERE session_id = '".session_id()."'";
	$product_result1 = mysql_query($product_query1) or die (mysql_error() );
	$product_info1 = mysql_fetch_assoc($product_result1);
	
	while($product_info1 = mysql_fetch_assoc($product_result1)) {
   		echo "<input type='hidden' name='item_name_$i' value='{$product_info1['product_name']}' />
         		<input type='hidden' name='amount_$i' value='{$product_info1['product_price']}' />";
   		$i++;
   }

I have changed the name of the variables from $product_info to $product_info1,  just so that i could differenciate between them.

I really do appreciate your help with this




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users