...

View Full Version : "Continue Shopping" PHP Sessions



nicky
12-07-2010, 03:43 PM
I am developing a catalog that customers can select from, then sends them to a "Request a Quote" form. All the items in the catalog are in a MySQL database. On the products page, I first selected only the product names from the database. Then when they click on the product name, the page reloads to display all the part numbers for that particular product. From there they can check the checkbox to send the part numbers to the request a quote form, however, from the RFQ form, I would like to enable the option to go back and continue adding items to the form, yet I can't seem to figure out PHP sessions. It's driving me bonkers!

products.php



include("header.php");

include("config.php");
include("connect.php");

if(!isset($_GET['item'])) {
$sql="SELECT DISTINCT product FROM $tbl_name";
}

else {
$sql="SELECT * FROM $tbl_name WHERE product = '" . addslashes($_GET['item']) . "'";
}

$result=mysql_query($sql) or die(mysql_error());

if(!isset($_GET['item'])) {
echo "<table>
<tr>
<th>Product</th>
</tr>";
}

else {
echo "<h1>{$_GET['item']}</h1>

<form action=\"quote.php\" method=\"post\">
<table style=\"width:100%;\">
<tr>
<th>Part Num</th>
<th>Description</th>
<th>Finish</th>
<th>Dem A</th>
<th>Dem B</th>
<th>Dem C</th>
<th>Dem D</th>
<th>Dem E</th>
<th>Dem F</th>
<th></th>
</tr>";
}

while($rows=mysql_fetch_array($result)){

if(!isset($_GET['item'])) {
echo "<tr>
<td><a href=\"?item={$rows['product']}\">{$rows['product']}</a></td>
</tr>";
}

else {

echo "<tr>
<td>{$rows['part_num']}</td>
<td>{$rows['description']}</td>
<td>{$rows['finish']}</td>
<td>{$rows['dem_a']}</td>
<td>{$rows['dem_b']}</td>
<td>{$rows['dem_c']}</td>
<td>{$rows['dem_d']}</td>
<td>{$rows['dem_e']}</td>
<td>{$rows['dem_f']}</td>
<td>{$rows['dem_g']}</td>
<td><input name=\"partnum[]\" type=\"checkbox\" value=\"{$rows['part_num']}\"></td>
</tr>";
}
}


if (!isset($_GET['item'])) {
echo "</table>"; }

else {
echo "<tr>
<td colspan=\"10\"><input name=\"add\" type=\"submit\" id=\"add\" value=\"Request a Quote\"/></td>
</tr>
</table>
</form>";
}

mysql_close();
include("footer.php");


quote.php



$partnum = $_REQUEST['partnum'];

$N = count($partnum);

for ($i=0; $i < $N; $i++) {
$_SESSION['Part'] = '$partnum[$i]';
}

include("header.php"); ?>

<h1>Request a Quote</h1>

<p><a href="products.php">Continue Shopping</a></p>

<p><?php echo $_SESSION['Part'];?></p>

<table>
<tr>
<td><label for="name">Name:</label></td>
<td colspan="2"><input type="text" id="name" name="name" size="35"/></td>
</tr>
<tr>
<td><label for="email">Email:</label></td>
<td colspan="2"><input type="text" id="email" name="email" size="35"/></td>
</tr>
<tr>
<td class="center" colspan="3">Specify the Quantity</td>
</tr>
<?php for($i=0; $i < $N; $i++) { echo "<tr>\n\t\t <td></td>\n\t\t <td>$partnum[$i]</td>" . "\n\t\t <td><input type=\"text\" name=\"quantity[]\" size=\"3\"/>" . "</td>\n\t\t</tr>\n\t\t"; } ?>
<tr>
<td colspan="3" class="center"><input type="submit" value=" Request a Quote "/> <input type="reset" value=" Reset Form "/></td>
</tr>
</table>

<?php include("footer.php");


Any help would be appreciated. Thank you!

mlseim
12-07-2010, 05:44 PM
At the very top of EVERY script that uses SESSION, it needs to look like this:

<?php
session_start();


I see you happen to include other files.
Those included files do not need to have the session_start() in them.

nicky
12-07-2010, 09:28 PM
In the header.php file which I have included, I do have the session_start called as the first line before the <html> tag.

mlseim
12-07-2010, 09:54 PM
How about "quote.php"?

nicky
12-07-2010, 10:43 PM
Yes, quote.php also includes header.php on line 9.

mlseim
12-07-2010, 11:10 PM
Well, as with most other scripts we see on this forum ... I can't test anything myself,
so it's too hard to troubleshoot using this forum.

=====

This part looks wrong to me ...

for ($i=0; $i < $N; $i++) {
$_SESSION['Part'] = '$partnum[$i]';
}

What happens there is, $_SESSION['Part'] will be the value of the last item.
There are no arrays with SESSION.

You'll need to build the SESSION variable and then explode it later on.

Sort of like this:

$build="";
for ($i=0; $i < $N; $i++) {
$build =. $partnum[$i]."|";
}
$_SESSION['Part'] = $build;

Now you will have a variable ($build) look like this: 23|12|4|67|14|
That's what your SESSION['Part'] will be.

When you need to see that SESSION, you'll explode it to get it back into an array ...

$parts=explode("|",$_SESSION['Part']);

now, $parts is an array ...
$parts[0]=23
$parts[1]=12
$parts[2]=4
etc.

dniwebdesign
12-08-2010, 05:27 AM
Hey could also do:

for ($i=0; $i < $N; $i++) {
$_SESSION['Part'][$i] = '$partnum[$i]';
}

nicky
12-08-2010, 03:23 PM
@mlseim - How do I echo all the items in the array? If I type:



echo $parts;


It returns Array. If I type:



echo $parts[1];


It'll give me a value, but I need all the values. Also, it's resetting every time I add new items. It's like the sessions isn't working.

@dniwebdesign - Thank you for your suggestion, but I tried and it didn't work.

djm0219
12-08-2010, 04:26 PM
@mlseim - How do I echo all the items in the array? If I type:



echo $parts;


It returns Array.


print_r($parts);

nicky
12-08-2010, 06:37 PM
After trying print_r, this is what it gives me:



Array ( [0] => BD1330 [1] => )


The 3 parts that I added to the request a quote form are BD1170, BD1270, BD1330. It's only the printing the last one as part of the array? I'm so confused.

To recap, here's my code so far based off of everyone's help.

quote.php


<?php $partnum = $_REQUEST['partnum'];

$build = "";

for ($i=0; $i < count($partnum); $i++) {
$build= $partnum[$i]."|";
}

$_SESSION['Part'] = $build;

$parts = explode("|", $_SESSION['Part']);

include("header.php");

php print_r($parts); ?>

mlseim
12-08-2010, 09:24 PM
You took off the period on the $build =. line

But try this (below). My thinking is to take what is already in SESSION and add to it.
So, instead of resetting $build each time, it adds to it.




<?php $partnum = $_REQUEST['partnum'];

$build = $_SESSION['Part'];

for ($i=0; $i < count($partnum); $i++) {
$build=. $partnum[$i]."|";
}

$_SESSION['Part'] = $build;

$parts = explode("|", $_SESSION['Part']);

include("header.php");

nicky
12-08-2010, 09:47 PM
When I put the period in, I get this error:

Parse error: syntax error, unexpected '.'

MattF
12-08-2010, 09:52 PM
.= rather than =.

nicky
12-08-2010, 10:03 PM
That worked to solve the error as to why my array was printing off weird, but the session is still not saving, and when I remove the print_r and put echo instead, the word "Array" still appears.

Hm... This is frustrating lol

MattF
12-08-2010, 10:09 PM
and when I remove the print_r and put echo instead, the word "Array" still appears.

That's doing exactly as it should. Post your code as is.

mlseim
12-08-2010, 10:20 PM
Thanks Matt ... I messed up the .=

This is really a problem with using this forum for troubleshooting code.
We can't test anything ourselves -- we can only offer ideas to try.

It is frustrating. You might want to find someone that can actually go into your files
using FTP and help you out "for real" ... not like us ... guessing at what we're doing.


.

nicky
12-08-2010, 10:23 PM
products.php


<?php

$partnum = $_REQUEST['partnum'];

$build = "";

for ($i=0; $i < count($partnum); $i++) {
$build= $partnum[$i]."|";
}

$_SESSION['Part'] = $build;

include("header.php");

include("config.php");
include("connect.php");

if(!isset($_GET['item'])) {
$sql="SELECT DISTINCT product FROM $tbl_name";
}

else {
$sql="SELECT * FROM $tbl_name WHERE product = '" . addslashes($_GET['item']) . "'";
}

$result=mysql_query($sql) or die(mysql_error());

if(!isset($_GET['item'])) {
echo "<table>
<tr>
<th>Product</th>
</tr>";
}

else {
echo "<h1>{$_GET['item']}</h1>

<form action=\"quote.php\" method=\"post\">
<table style=\"width:100%;\">
<tr>
<th>Part Num</th>
<th>Description</th>
<th>Finish</th>
<th>Dem A</th>
<th>Dem B</th>
<th>Dem C</th>
<th>Dem D</th>
<th>Dem E</th>
<th>Dem F</th>
<th></th>
</tr>";
}

while($rows=mysql_fetch_array($result)){

if(!isset($_GET['item'])) {
echo "<tr>
<td><a href=\"?item={$rows['product']}\">{$rows['product']}</a></td>
</tr>";
}

else {

echo "<tr>
<td>{$rows['part_num']}</td>
<td>{$rows['description']}</td>
<td>{$rows['finish']}</td>
<td>{$rows['dem_a']}</td>
<td>{$rows['dem_b']}</td>
<td>{$rows['dem_c']}</td>
<td>{$rows['dem_d']}</td>
<td>{$rows['dem_e']}</td>
<td>{$rows['dem_f']}</td>
<td>{$rows['dem_g']}</td>
<td><input name=\"partnum[]\" type=\"checkbox\" value=\"{$rows['part_num']}\"></td>
</tr>";
}
}


if (!isset($_GET['item'])) {
echo "</table>"; }

else {
echo "<tr>
<td colspan=\"10\"><input name=\"add\" type=\"submit\" id=\"add\" value=\"Request a Quote\"/></td>
</tr>
</table>
</form>";
}

mysql_close();
include("footer.php"); ?>

quote.php


<?php

$title = "Request a Quote";
$page = "contact";

$partnum = $_REQUEST['partnum'];

$build = $_SESSION['Part'];

for ($i=0; $i < count($partnum); $i++) {
$build= $partnum[$i]."|";
}

$_SESSION['Part'] = $build;

$parts = explode("|", $_SESSION['Part']);

include("header.php"); ?>

<h1>Request a Quote</h1>

<p><a href="products.php">Continue Shopping</a></p>

<p><?php echo explode("|", $_SESSION['Part']); ?></p>

<table>
<tr>
<td><label for="name">Name:</label></td>
<td colspan="2"><input type="text" id="name" name="name" size="35"/></td>
</tr>
<tr>
<td><label for="email">Email:</label></td>
<td colspan="2"><input type="text" id="email" name="email" size="35"/></td>
</tr>
<tr>
<td class="center" colspan="3">Specify the Quantity</td>
</tr>
<?php for($i=0; $i < count($partnum); $i++) { echo "<tr>\n\t\t <td></td>\n\t\t <td>$partnum[$i]</td>" . "\n\t\t <td><input type=\"text\" name=\"quantity[]\" size=\"3\"/>" . "</td>\n\t\t</tr>\n\t\t"; } ?>
<tr>
<td colspan="3" class="center"><input type="submit" value=" Request a Quote "/> <input type="reset" value=" Reset Form "/></td>
</tr>
</table>

<?php include("footer.php"); ?>

And the first line in header.php is...


<?php session_start(); ?>

MattF
12-08-2010, 10:30 PM
Why are both of these lines without the period before the equals sign?



$build= $partnum[$i]."|";

nicky
12-08-2010, 10:48 PM
I had 'em there. Weird. Anyway, added the period back in, and still no session carrying over.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum