By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,723 Members | 1,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,723 IT Pros & Developers. It's quick & easy.

array type problem

P: n/a
AJ
I'm writing my own little shopping cart system and am having a slight
problem at the final hurdle.

Visitor hits the site and once they add to their cart a row is created
in a table with the session_id as their cart id. There are columns
for the product and the price.

On each page, the 'carts' table is looped through to display the users
cart.

On the checkout page, I want to loop through the carts table twice, or
make it so that the array is available more than one.

This is the bit of code that I'm running:

<?php do {
$cart_total=$cart_total+$row_current_cart['product_price'];
} while ($row_current_cart = mysql_fetch_assoc($current_cart));
?>
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
AJ
an**@redcatmedia.net (AJ) wrote in message news:<51**************************@posting.google. com>...
This is the bit of code that I'm running:

<?php do {
$cart_total=$cart_total+$row_current_cart['product_price'];
} while ($row_current_cart = mysql_fetch_assoc($current_cart));
?>


And to actually make some sense of that now that my Mac is playing
again...

Basically, what's happening is that the second time I try to print the
cart out, or parts of it, on one page, it's got to the end of the
array and prints nothing.

I've tried reset($current_cart) but no joy.

Any ideas please.

Andy
Jul 17 '05 #2

P: n/a
an**@redcatmedia.net (AJ) wrote in message news:<51**************************@posting.google. com>...
an**@redcatmedia.net (AJ) wrote in message news:<51**************************@posting.google. com>...
This is the bit of code that I'm running:

<?php do {
$cart_total=$cart_total+$row_current_cart['product_price'];
} while ($row_current_cart = mysql_fetch_assoc($current_cart));
?>


And to actually make some sense of that now that my Mac is playing
again...

Basically, what's happening is that the second time I try to print the
cart out, or parts of it, on one page, it's got to the end of the
array and prints nothing.

I've tried reset($current_cart) but no joy.

Any ideas please.

Andy


You might've typed in your snippet of code wrong here but I believe
the while condition needs to have == not =.
Jul 17 '05 #3

P: n/a
AJ

"dogeater" <gg******@yahoo.com> wrote in message
news:7f*************************@posting.google.co m...
an**@redcatmedia.net (AJ) wrote in message

news:<51**************************@posting.google. com>...
an**@redcatmedia.net (AJ) wrote in message news:<51**************************@posting.google. com>...
This is the bit of code that I'm running:

<?php do {
$cart_total=$cart_total+$row_current_cart['product_price'];
} while ($row_current_cart = mysql_fetch_assoc($current_cart));
?>


And to actually make some sense of that now that my Mac is playing
again...

Basically, what's happening is that the second time I try to print the
cart out, or parts of it, on one page, it's got to the end of the
array and prints nothing.

I've tried reset($current_cart) but no joy.

Any ideas please.

Andy


You might've typed in your snippet of code wrong here but I believe
the while condition needs to have == not =.


Not sure about that. This works in as much as it displays the cart. The
problem I have is when I run this bit of code twice. I'm pretty sure that
the problem is because it loops through the array and displays the contents
of each element. Once it's finished at position 'n', the array counter
stays at position 'n'. What I need to do is run the code again from
position 0. I've even tried mysql_data_seek(0). This almost works but you
get:

[blank]
Product 1 9.99
Total 19.98

when you run it the second time, even though there is only one item in the
cart. The first time it runs, it displays what it should.

Andy
Jul 17 '05 #4

P: n/a

"AJ" <no****@redcatmedia.net> wrote in message
news:ca**********@hercules.btinternet.com...

"dogeater" <gg******@yahoo.com> wrote in message
news:7f*************************@posting.google.co m...
an**@redcatmedia.net (AJ) wrote in message news:<51**************************@posting.google. com>...
an**@redcatmedia.net (AJ) wrote in message news:<51**************************@posting.google. com>...
> This is the bit of code that I'm running:
>
> <?php do {
> $cart_total=$cart_total+$row_current_cart['product_price'];
> } while ($row_current_cart = mysql_fetch_assoc($current_cart));
> ?>

And to actually make some sense of that now that my Mac is playing
again...

Basically, what's happening is that the second time I try to print the
cart out, or parts of it, on one page, it's got to the end of the
array and prints nothing.

I've tried reset($current_cart) but no joy.

Any ideas please.

Andy


You might've typed in your snippet of code wrong here but I believe
the while condition needs to have == not =.


Not sure about that. This works in as much as it displays the cart. The
problem I have is when I run this bit of code twice. I'm pretty sure that
the problem is because it loops through the array and displays the

contents of each element. Once it's finished at position 'n', the array counter
stays at position 'n'. What I need to do is run the code again from
position 0. I've even tried mysql_data_seek(0).
The corect syntax for this is
bool mysql_data_seek ( resource result_identifier, int row_number)

--
Tony Marston

http://www.tonymarston.net
This almost works but you get:

[blank]
Product 1 9.99
Total 19.98

when you run it the second time, even though there is only one item in the
cart. The first time it runs, it displays what it should.

Andy

Jul 17 '05 #5

P: n/a
Why not create a $rows array that contains your query results?
Then work with the array rather than unnecessarily reading from the DB..

here's a general purpose function I use that returns the rows you want:

function get_query_rows($query) {
if ( $result = @mysql_query($query) ) {
$rows = array();
$num_rows = mysql_num_rows($result);
for ( $i=0; $i<$num_rows; $i++ )
$rows[] = mysql_fetch_assoc($result);
}
else {
$rows = false;
}
return $rows;
}
an**@redcatmedia.net (AJ) wrote in message news:<51**************************@posting.google. com>...
I'm writing my own little shopping cart system and am having a slight
problem at the final hurdle.

Visitor hits the site and once they add to their cart a row is created
in a table with the session_id as their cart id. There are columns
for the product and the price.

On each page, the 'carts' table is looped through to display the users
cart.

On the checkout page, I want to loop through the carts table twice, or
make it so that the array is available more than one.

This is the bit of code that I'm running:

<?php do {
$cart_total=$cart_total+$row_current_cart['product_price'];
} while ($row_current_cart = mysql_fetch_assoc($current_cart));
?>

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.