Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post

    "Continue Shopping" PHP Sessions

    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

    PHP Code:
    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 Code:
    $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!

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 Posts
    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.

  • #3
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    In the header.php file which I have included, I do have the session_start called as the first line before the <html> tag.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 Posts
    How about "quote.php"?

  • #5
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Yes, quote.php also includes header.php on line 9.

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 Posts
    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.

  • #7
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts
    Hey could also do:
    PHP Code:
    for ($i=0$i $N$i++) {
         
    $_SESSION['Part'][$i] = '$partnum[$i]';

    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #8
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    @mlseim - How do I echo all the items in the array? If I type:

    PHP Code:
    echo $parts
    It returns Array. If I type:

    PHP Code:
    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.

  • #9
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,313
    Thanks
    4
    Thanked 207 Times in 204 Posts
    Quote Originally Posted by nicky View Post
    @mlseim - How do I echo all the items in the array? If I type:

    PHP Code:
    echo $parts
    It returns Array.
    PHP Code:
    print_r($parts); 
    Last edited by djm0219; 12-08-2010 at 04:49 PM.
    Dave .... HostMonster for all of your hosting needs

  • #10
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    After trying print_r, this is what it gives me:

    Code:
    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 Code:
    <?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); ?>

  • #11
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 Posts
    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 Code:
    <?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");

  • #12
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    When I put the period in, I get this error:

    Parse error: syntax error, unexpected '.'

  • #13
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    .= rather than =.

  • #14
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    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

  • #15
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by nicky View Post
    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •