Storing data from an array to a session

Posted on 2005-04-12
Last Modified: 2013-12-12
Ok, heres my question. i have a array thats calling my data from a mysql database and displaying it in a table
i want to make it so each new row in the table is a seperate form, and when add to cart is clicked the data from that row is added to a session, can anyone let me know the easiest way to do this

 while ($row= mysql_fetch_array ($result, MYSQL_ASSOC)){
                  print "<tr bgcolor=\"#666666\">";
            print "<td>
                              <form method=\"post\" action=\"punk.php\">
            print "<td>$row[ProductArtist]</td>";
            print "<td>$row[ProductName]</td>";
            print "<td>$row[ProductType]</td>";
                print "<td>$row[ProductDescription]</td>";
            print "<td><img src=\"$row[ProductImage]\"></td>";
            print "<td>$$row[ProductCost]</td>";
                  print "<td>
                              <input type=\"submit\" name=\"addtocart\" value=\"Add To Cart\">
            print "</tr>";
            print "</table>";

thats the end of my array and table, how can i make it so that data from the add to cart button is on is stored in a session to be displayed on another page.

any help is greatly appreciated

Question by:shrocks
    LVL 32

    Accepted Solution

    Assign the rows in hidden variables in the form. In the action page, you can retrieve the posted values and assign them to session.

    while ($row = mysql_fetch_array($result))
        $ProductID = $row['ProductID'];
        $ProductArtist = $row['ProductArtist'];
        $ProductName = $row['ProductName'];
        $ProductType = $row['ProductType'];
        $ProductDescription = $row['ProductDescription'];
        $ProductCost = $row['ProductCost'];
        <tr bgcolor="#666666">
           <form method="post" action="punk.php">
           <?php echo $ProductID; ?>
          <td><?php echo $ProductArtist; ?></td>
          <td><?php echo $ProductName; ?></td>
          <td><?php echo $ProductType; ?></td>
          <td><?php echo $ProductDescription; ?></td>
          <td><img src="<?php echo $row['ProductImage']; ?>"></td>
          <td><?php echo $ProductCost; ?></td>
            <input type="submit" name="addtocart" value="Add To Cart"/>
           <input type="hidden" name="ProductID" value="<?php echo $ProductID; ?>"/>
           <input type="hidden" name="ProductArtist" value="<?php echo $ProductArtist; ?>"/>                   
           <input type="hidden" name="ProductName" value="<?php echo $ProductName; ?>"/>
           <input type="hidden" name="ProductType" value="<?php echo $ProductType; ?>"/>
           <input type="hidden" name="ProductDescription" value="<?php echo $ProductDescription; ?>"/>
           <input type="hidden" name="ProductCost" value="<?php echo $ProductCost; ?>"/>

    In punk.php:

    if(isset($_POST['addtocart']) && !empty($_POST['addtocart']))
        $_SESSION['ProductID'] = $_POST['ProductID'];
        $_SESSION['ProductArtist'] = $_POST['ProductArtist'];            
        $_SESSION['ProductName'] = $_POST['ProductName'];
        $_SESSION['ProductType'] = $_POST['ProductType'];
        $_SESSION['ProductDescription'] = $_POST['ProductDescription'];
        $_SESSION['ProductCost'] = $_POST['ProductCost'];
    LVL 32

    Expert Comment

    >>  Assign the rows in hidden variables in the form.

    should better read:

     Assign the row values as hidden variables inside the form.
    LVL 1

    Author Comment

    thanks for you answer, thats exactly what i was looking for, thank you
    LVL 32

    Expert Comment

    you are welcome and thanks for the points. :=)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now