Jump to content


Photo

Posting Arrays to follow-on pages


  • Please log in to reply
3 replies to this topic

#1 kah

kah
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 07 June 2006 - 09:26 PM

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>


#2 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 08 June 2006 - 12:29 AM

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]

NoGray.com


#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 June 2006 - 02:44 AM

At the start of your script "shop_cart.php" put the following line:
<?php echo '<pre>' . print_r($_POST,true) . '</pre>'; ?>
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

#4 kah

kah
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 08 June 2006 - 05:10 AM

Thanx for your help nogray and ken. I have partially resolved it, but have decided that I am going to do have to learn more about sessions if I am to complete this project properly.

Ken I would appreciate any direction you can give in reducing the code.

ta,
kah




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users