Jump to content

Recommended Posts

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.



<?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>
Link to comment
https://forums.phpfreaks.com/topic/11444-posting-arrays-to-follow-on-pages/
Share on other sites

the a:3:{i:0;s:6:"sb1253";} looks like a serilized array, check these pages out

[a href=\"http://us3.php.net/manual/en/function.serialize.php\" target=\"_blank\"]serialize()[/a]

[a href=\"http://us3.php.net/manual/en/function.unserialize.php\" target=\"_blank\"]unserialize() [/a]
At the start of your script "shop_cart.php" put the following line:
[code]<?php echo '<pre>' . print_r($_POST,true) . '</pre>'; ?>[/code]
This will show you what is being returned to your script from the form. From that display, you should be able to figure out the code needed to retrieve the values. If you still have problems, post again.

BTW, your code for displaying the form can be reduced to many fewer lines.

Ken
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.