Forum Moderators: coopster

Message Too Old, No Replies

Php Array problem

         

adamnichols45

2:05 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Im having a problem with a custom shopping cart at the moment.

Basically when a customer clicks add to cart the next page displays that product information from the database.

Now if they click another product that again is displayed under the first product.

Now I want both of them products ids to be outputted to a text field but the problem is what ever product is put in last is taking over the varible.

the code starts like this.

$sql = 'SELECT * FROM products WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
$ig = '$image';
extract($row);

bubbasheeko

2:25 pm on May 4, 2008 (gmt 0)

10+ Year Member



Hi adam,

While simple and yet somewhat practical, I would throw in:

$row = "";
$result = "";

Put them in before the $sql statement. Thus clearing the variable used to contain the array.

adamnichols45

2:45 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When I do that and go to output the varible it is stil only outputting the last inserted data.

Pico_Train

3:25 pm on May 4, 2008 (gmt 0)

10+ Year Member



The last result is overwritting the earlier ones.

try $row[] instead of $row - that might should do it...I think

adamnichols45

3:32 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$row = "";
$result = "";

I looked at this again i was not putting it before opening bracket.

That just stops outputting the whole varible. just shows 1st product in varible.

--------

trying out the row[] stops any data going through what so ever.

adamnichols45

3:33 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$row = "";
$result = "";

I looked at this again i was not putting it before opening bracket.

That just stops outputting the whole varible. just shows 1st product in varible.

--------

trying out the row[] stops any data going through what so ever.

There is also foreach ($contents as $id=>$qty) if that makes any difference.

The problem im having is that when the order are coming through all I can see is how much the total is which is no good.

adamnichols45

3:57 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



right im getting closer.

print_r($title);
print_r($qty);

thats out puts the following:

6" pen229" pencil2

that tells me the foloowing order

6" pen x 22
9" pencil x 2

But how can I format that a little better?

bubbasheeko

4:05 pm on May 4, 2008 (gmt 0)

10+ Year Member



Format it in what way? The pretty factor or the neat factor?

adamnichols45

4:28 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just format it so that it is pretty

pen x 2
pencil x 2

adamnichols45

5:10 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have got it formatted exactly how I want now.

print_r($title); echo" x ";
print_r($qty); echo" = ";
$tot = ($price * $qty);
$num1 = number_format ($tot, 2);
echo ($num1);
echo "<br>";

How would you suggest it best to save that into a varible so I can echo it in to a text field?

bubbasheeko

9:07 pm on May 4, 2008 (gmt 0)

10+ Year Member



I don't think I am understanding you on this....

You want the values of each or all into one text field? Or do you want the text field to have this:

pen x 2

adamnichols45

10:39 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just want the output of my script to be the value of a text field.