Reply to Re: HELPPP!! Storing Arrays.

Your name:

Reply:


Posted by Siv Hansen on 11/05/05 03:15

joyn297@bellsouth.net wrote:
> Can someone please, please tell me whats wrong with this code. I got
> this code from a book and it is not working. Whenever I select my
> products from 16.4 and then I click on content hyperlink it doesnt show
> the products that I hae chosen. I dont know if it a prob with
> serialize/unserialize or what? Any help is GREATLY appreciated



>
> <?php
> session_start();
> ?>
> <html>
> <head>
> <title>Listing 16.4 Storing an array with a session</title>
> </head>
> <body>
> <h1>Product Choice Page</h1>
> <?php
> if (isset($_POST[form_products])) {
> if (!empty($_SESSION[products])) {
> $products = array_unique(
> array_merge(unserialize($_SESSION[products]),
> $_POST[form_products]));
> }
> $_SESSION[products] = serialize($products);
> print "<p>Your products have been registered!</p>";
> }
> ?>
Shouldn't this be moved up?
($_SESSION['products'] = serialize($products);)
Because you check for value in $_SESSION['products'] above the point
where you assign a value to it.
As far as I can see, your if-condition will never become true.
Something like this perhaps?

if (isset($_POST[form_products])) {
$_SESSION'products'] = serialize($products);
/* and since you've just assigned a value to $_SESSION['products']
I guess you don't have to check for a value */
if (!empty($_SESSION['products'])) {
$products = array_unique(
array_merge(unserialize($_SESSION['products']),
$_POST['form_products']));
}

print "<p>Your products have been registered!</p>";
}

> <form method="POST" action="<?php $_SERVER[PHP_SELF] ?>">
> <P>Select some products:<br>
> <select name="form_products[]" multiple size=3>
> <option>Sonic Screwdriver</option>
> <option>Hal 2000</option>
> <option>Tardis</option>
> <option>ORAC</option>
> <option>Transporter bracelet</option>
> </select>
> <br><br>
> <input type="submit" value="choose">
> </form>
> <br><br>
> <a href="listing16.5.php">content page</a>
> </body>
> </html>
>
>
>
> <?php
> session_start();
> ?>
> <html>
> <head>
> <title>Listing 16.5 Accessing session variables</title>
> </head>
> <body>
> <h1> Content Page</h1>
> <?php
> if (isset($_SESSION[products])) {
> print "<b>Your cart:</b><ol>\n";
> foreach (unserialize($_SESSION[products]) as $p) {
> print "<li>$p";
> }
> print "</ol>";
> }
> ?>
> <a href="listing16.4.php">Back to product choice page</a>
> </body>
> </html>
>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация