Page 1 of 1

Posting Arrays to follow-on page

Posted: Wed Jun 07, 2006 4:13 pm
by Kah
arborint | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi. I am attempting to build a very simple shopping cart using php for an assignment. I am displaying the images and relevant details for the product from a MYSQL database.

I want the customer to be able to select as many products as they wish, and then click the Purchase button which will take them to shop_cart.php. This page will display the products they have chosen.

I cannot work out how to display the chosen products in shop_cart.php. I have included the functions for first page - shop_didge() - and then the display - page shop_cart().

Where I want the id to display the id of sb1253 I get a:3:{i:0;s:6:"sb1253";} and $timber displays N; whereas it should be Stringy Bark.

Appreciate any help.

Code: Select all

<?php
}
function shop_didge(){
?>

<form action="shop_cart.php" method="post">
<table class='table9' align='center'>
<?php

// Get all the data from the "products" table
$result = mysql_query("select * from products where SOLD=''")
or die(mysql_error());

// keeps getting the relevant info until there are no more to get
while($col = mysql_fetch_array( $result )) {

echo "<tr><td colspan='2'><strong>Didge ID: ";
echo $col['productid'];
echo "</strong></td></tr><tr><td colspan='2'><img class='image' src='images/";
echo $col['longimage'];
echo "' />";
echo "</td><tr><td><img class='image' src='images/";
echo $col['mouthimage'];
echo "' /></td><td rowspan='2'>";
echo "<table class='table10'>";
echo "<tr><td>Timber:</td>";
echo "<td>";
echo $col['timber'];
echo "</td></tr><tr>";
echo "<td>Length:</td><td>";
echo $col['length'];
echo "cm</td></tr><tr>";
echo "<td>Mouth:</td><td>";
echo $col['mouth'];
echo "cm</td></tr><tr>";
echo "<td>End:</td><td>";
echo $col['end'];
echo "cm</td></tr><tr>";
echo "<td>Weight:</td><td>";
echo $col['weight'];
echo "kg</td></tr><tr>";
echo "<td>Key:</td><td>#";
echo $col['key'];
echo "</td></tr><tr>";
echo "<td>Soundfile:</td><td><a href='";
echo $col['soundfile'];
echo "'>";
echo $col['soundfile'];
echo "</a></td></tr><tr>";
echo "<td>Price:</td><td>$";
echo $col['price'];
echo "</td></tr><tr>";
echo "<td>Purhase:</td>";
//edit if wrong
echo "<td><input name='id[]'";
echo $col['productid'];
echo "' type='checkbox' value='";
echo $col['productid'];
echo "'></td>";
echo "</tr></table>";
echo "</td></tr>";
echo "<tr><td><img class='image' src='images/";
echo $col['endimage'];
echo "' /></td></tr>";
echo "<tr><td colspan='2'><hr /></td></tr>";
echo "$productid";

}
?>
<tr>
<td colspan="2" align="right"><input type="submit" name="purchase" value="Purchase" /></td>
</tr>
</table>
</form>

//Display page

<?php
}
function shop_cart(){
?>
<?php

$id=serialize($_POST['id']);
$timber=serialize($_POST['timber']);

// tried $id=$_POST['id']; only displayed Array


?>

<table class="table9" align="center">
<tr>
<td>Product ID</td>
<td>Description</td>
<td>Price</td>
</tr>

<?php
echo '<tr><td>';
echo $id;
echo '</td></tr><tr><td>';
echo $timber;
echo '</td></tr>';

?>

</table>
Reply With Quote

arborint | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Jun 07, 2006 4:57 pm
by PrObLeM
use sessions, then you can do:

page1.php

Code: Select all

$_SESSION['cartdata'] = $myArray;
page2.php

Code: Select all

print_r($_SESSION['cartdata']);

Posted: Wed Jun 07, 2006 6:08 pm
by RobertGonzalez
I'd be willing to bet that if you searched google for 'PHP classes tutorial' you would find about a thousand different pages for using an object oriented approach to creating a shopping cart. It is a classical tutorial example.

You will for sure want to use sessions or some form of database storage mechanism for the data.