Sending basket string

Hi all
I am writing a script to send the contents of my SQL table to SagePay as a string $basket. I have got it to work OK with adding one item in. My code is this:

$query_basket = mysql_query(“SELECT * FROM store_basket WHERE sessionid=’”.$sessionid."’");
$rows = mysql_num_rows($query_basket);
$allitems = count($rows);
while($result_basket = mysql_fetch_array($query_basket)) {
$price_without_tax = $result_basket[‘price’];
$tax = 17.5;
$price_with_tax = $result_basket[‘price’] + $tax;
$price_times_qty = $result_basket[‘price’] * $result_basket[‘qty’];
$basket_item = $result_basket[‘item’];
$basket_qty = $result_basket[‘qty’];
}

if($allitems == 1) {
$basket .= “1”.":".$basket_item.":".$basket_qty.":".$price_without_tax.":".$tax.":".$price_with_tax.":".$price_times_qty;
} else {
$basket .= $allitems.":".$basket_item.":".$basket_qty.":".$price_without_tax.":".$tax.":".$price_with_tax.":".$price_times_qty;
}

$rows++;

However, it only shows one item in the basket when there is more than one in my table.

I need to do a loop so that each item in the table is outputted in the $basket string.

Thanks
Pete

In your code, $rows is a scalar value. So this expression make no sense:
[php]$allitems = count($rows);[/php]

Probably it supposed to be this?:
[php]$allitems = $rows;[/php]

Thanks for your reply. How do I loop the below script?

$query_basket = mysql_query("SELECT * FROM `store_basket` WHERE sessionid='".$sessionid."'");
    $rows = mysql_num_rows($query_basket);
	$r = mysql_fetch_array($query_basket);
	$allitems = $rows;
    while($result_basket = mysql_fetch_array($query_basket)) {
		
		
	$price_without_tax = $result_basket['price'];
    $tax = 17.5;
    $price_with_tax = $result_basket['price'] + $tax;
    $price_times_qty = $result_basket['price'] * $result_basket['qty'];
	$basket_item = $result_basket['item'];
	$basket_qty = $result_basket['qty'];
	$basket_initial1 = $result_basket['initial1'];
	$basket_initial2 = $result_basket['initial2'];
	$basket_initial3 = $result_basket['initial3'];

}

if($allitems == 1) {
$basket .= $allitems.":".$basket_item." “.strtoupper($basket_initial1).”-".strtoupper($basket_initial2)."-".strtoupper($basket_initial3).":".$basket_qty.":".$price_without_tax.":".$tax.":".$price_with_tax.":".$price_times_qty;
} else {
$allitems = $rows;

$basket .= “:”.$basket_item." “.strtoupper($basket_initial1).”-".strtoupper($basket_initial2)."-".strtoupper($basket_initial3).":".$basket_qty.":".$price_without_tax.":".$tax.":".$price_with_tax.":".$price_times_qty;

 }

Basically if there is more than one row I need to loop the $basket string so that it shows the values for each item.

Thanks
Pete

You need to move this entire block within loop - right before closing } of your while() loop:

[php]if($allitems == 1) {
$basket .= $allitems.":".$basket_item." “.strtoupper($basket_initial1).”-".strtoupper($basket_initial2)."-".strtoupper($basket_initial3).":".$basket_qty.":".$price_without_tax.":".$tax.":".$price_with_tax.":".$price_times_qty;
} else {
$allitems = $rows;

$basket .= “:”.$basket_item." “.strtoupper($basket_initial1).”-".strtoupper($basket_initial2)."-".strtoupper($basket_initial3).":".$basket_qty.":".$price_without_tax.":".$tax.":".$price_with_tax.":".$price_times_qty;

}
[/php]

p.s. if you post more messages with your code, please use button named “php” to highlight the code. This button can be found in toolbar, above smiles icons.

Hi there

Thanks for your reply, that doesn’t show anything at all if there is only one item in the basket and just the last item if there is more than 1?

Pete

What is the separator of items in your basket string - colon “:” ? This may be a problem because you have colon as separator of values within each item too. It is not obvious from your code, how it will parse $basket string into items to display them.

Sponsor our Newsletter | Privacy Policy | Terms of Service