Add items to a list

I am getting a list of results from a SQL query and trying to figure out how to add them to a list. There is a text field for users to enter a quantity and an “add to list” button. I am stuck on how to add the items in the loop to a list which can then be inserted into the body of an email. Anyone shed some light on this for me? Thanks in advance!

Code:

if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
         

        $i = "1";
        while($results = mysql_fetch_array($raw_results)){
          
          echo "<h3><div id='$i'>Product ID: ".$results['PID']."</div></h3>";
          echo "<p>Description: ".$results['Description']."</p>";
          echo "<p>Quantity: ".$results['Quantity']."</p>";
          echo "<p>Location: ".$results['Location']."</p>";
         // echo "<p>Link: ".$results['DataSheetLink']."</p>";
          if(!empty($results['DataSheetLink']))
          echo '<p>Data Sheet Link: <a href="'.$results['DataSheetLink'].'">Click Here</a></p>';
          echo "<a href='mailto:<email>?subject=Part Request: ".$results['PID']."&body=I would like to request (enter quantity of this item) of PID ".$results['PID']." - ".$results['Description']." '>Request Item</a><br><br>";
          
          echo "<form>";
          echo "<input type='text' id='qty$i' value='' placeholder='Quantity' width='50px' />";
          echo '<button name="add_to_cart" value="'.$results['PID'].'" type="submit">Add to List</button><br>'; 
          echo "</form>";
          echo "<hr>"; 
        
          
          $i++;


        }

Create a variable to hold the list.

$items = “”;
for($index = 0; $index < 50; $index++) {
$items .= “$index\n”;
}

echo $items;

And stop using mysql_* functions. They are bad, outdated, and a security risk.

Thanks. I’m new to php and just learned that those functions are outdated and bad so I am going to update.

Should I make the variable outside the loop?
How would I make the add_to_cart button add the selected item to $items with the entered value from the quantity text box?

This input needs a name. From there you are just adding the qty to the PID value in the cart.

Is this a homemade shopping cart, or something already out that you are working to customize?

Homemade. Not exactly going to be a cart. I am just looking to add the selected items and entered quantities to a list and then take that list and put it inside the body of an email and send it to myself.

Ideally I would just want the list to be:

SomePID, 10
SomePID2, 3
SomePID3, 6

Not exactly. You would want human readable, unless you are dealing with a warehouse that only cares about the numbers. Are you also storing the request in the database?

Based on what you said, what I posted is pretty much what you are after. You just want to include the PID in the string. However, being able to add multiple values means you need to store the items somewhere. So, $_SESSION is how you may want to go. You would create an array and store it in the session array. Then, when the order is complete, you would iterate through the array and make your email at that point.

The data is being sent to me, so it doesn’t necessarily have to be human readable as I can decipher it. Could you shed some light on the $_SESSION route of setting this up? Forgive me, this is all new to me. I feel like I should probably pay you at this point :slight_smile:

Alright,

echo “<form method=‘post’>”;
echo “<input type=‘text’ id=‘qty$i’ name=‘qty’ value=’’ placeholder=‘Quantity’ width=‘50px’ />”;
echo ‘<button name=“add_to_cart” value="’.$results[‘PID’].’" type=“submit”>Add to List</button><br>’;
echo “</form>”; echo “<hr>”;

You have your form, (which will default to a get request without a method specified). You need to process this request and save it somewhere.

 // process the request
 if($_SERVER[‘REQUEST_METHOD’] == ‘POST’){
     // check if the cart has anything in it
     if(!isset($_SESSION[‘cart’])) $_SESSION[‘cart’] = null;
         // validate something is there
         if($_POST[‘qty’] &gt;= 0) {
             // add the qty to the PID. Store it in the session array with the PID as the key
            $_SESSION[‘cart’][$_POST[‘add_to_cart’]] = $_POST[‘qty’];
     }
 }

Iterate over the cart to see what was added:
if(isset($_SESSION[‘cart’])){
$items = “”;
foreach($_SESSION[‘cart’] as $key => $value) {
$items .= “$key, $value\n”;
}
}
echo $items;

I can’t thank you enough for the help. I almost seem to have it working. I am getting the error " Undefined index: add_to_cart in C:\wamp\www\inv\search2.php on line 78" which is:

$_SESSION[‘cart’][$_POST[‘add_to_cart’]] = $_POST[‘qty’];

The list is getting the quantity added, but not the PID

Try adding a hidden field to the form.

echo “&lt;form method=‘post’&gt;”;
echo “&lt;input type=‘text’ id=‘qty$i’ name=‘qty’ value=’’ placeholder=‘Quantity’ width=‘50px’ /&gt;”;
echo "<input type='hidden' name='pid' value='pid'>";
echo ‘&lt;button name=“add_to_cart” value="’.$results[‘PID’].’" type=“submit”&gt;Add to List&lt;/button&gt;&lt;br&gt;’;
echo “&lt;/form&gt;”; echo “&lt;hr&gt;”;

Then in your cart you use that as the key
$_SESSION[‘cart’][$_POST[‘pid’]] = $_POST[‘qty’];

Ok thanks. That seems to have gotten rid of the error. Should I make a separate page for the “cart”? I am getting the item to add to the list, but will it add multiple items or just a single one? Thank you so much for helping me with this.

Try it out.

How do you want to handle the cart? These are all design decisions.

I don’t mind it like in the screenshot, but when I try to add another item, it overwrites the previous one? Could I have possibly done something incorrectly?

Hmmm… It should have multiple arrays within the cart value.

This is the code I have so far. It adds the one item to the list like in the screenshot. I type in the quantity and click add to list and it adds. (resets the scrolling back to top of page). But when I try to add another item, it overwrites the first value. Maybe I have some code in the incorrect place?

 if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
         

        $i = "1";
        while($results = mysql_fetch_array($raw_results)){
          
          echo "<h3><div id='$i'>Product ID: ".$results['PID']."</div></h3>";
          echo "<p>Description: ".$results['Description']."</p>";
          echo "<p>Quantity: ".$results['Quantity']."</p>";
          echo "<p>Location: ".$results['Location']."</p>";
         // echo "<p>Link: ".$results['DataSheetLink']."</p>";
          if(!empty($results['DataSheetLink']))
          echo '<p>Data Sheet Link: <a href="'.$results['DataSheetLink'].'">Click Here</a></p>';
          echo "<a href='mailto:[email protected]?subject=Part Request: ".$results['PID']."&body=I would like to request (enter quantity of this item) of PID ".$results['PID']." - ".$results['Description']." '>Request Item</a><br><br>";
          
         
          echo "<form method='post'>";
          echo "<input type='text' id='qty$i' name='qty' value='' placeholder='Quantity' width='50px' />";
          echo '<input type="hidden" name="pid" value="'.$results['PID'].'">';
          echo '<button name="add_to_cart" value="'.$results['PID'].'" type="submit">Add to List</button><br>';
          echo "</form>"; echo "<hr>";
        
          
          $i++;


            
            // posts results gotten from database
        }


          if($_SERVER['REQUEST_METHOD'] == 'POST'){
               // check if the cart has anything in it
               if(!isset($_SESSION['cart'])) $_SESSION['cart'] = null;
                   // validate something is there
                   if($_POST['qty'] >= 0) {
                       // add the qty to the PID. Store it in the session array with the PID as the key
                     // $_SESSION['cart'][$_POST['add_to_cart']] = $_POST['qty'];
                      $_SESSION['cart'][$_POST['pid']] = $_POST['qty'];
               }
           }

         if(isset($_SESSION['cart'])){
            $items = "";
            foreach($_SESSION['cart'] as $key => $value) {
            $items .= "$key, $value\n";
            }
            }
            if(!empty($items))
            echo $items;
    }
    else{ // if there is no matching rows do following
        echo "Sorry, No results were found";
    }
     
}
else{ // if query length is less than minimum
    echo "Minimum length is ".$min_length;
}
?>

Example

Move your processing code to the top of the page.

I would add a “view cart” page that outputs what is being stored in the session as well.

So I should move this part to the top of the page?

if($_SERVER['REQUEST_METHOD'] == 'POST'){
       // check if the cart has anything in it
       if(!isset($_SESSION['cart'])) $_SESSION['cart'] = null;
           // validate something is there
           if($_POST['qty'] >= 0) {
               // add the qty to the PID. Store it in the session array with the PID as the key
             // $_SESSION['cart'][$_POST['add_to_cart']] = $_POST['qty'];
              $_SESSION['cart'][$_POST['pid']] = $_POST['qty'];
       }
   }

And make a “cart.php” page that does this:

if(isset($_SESSION['cart'])){
            $items = "";
            foreach($_SESSION['cart'] as $key => $value) {
            $items .= "$key, $value\n";
            }
            }
            if(!empty($items))
            echo $items;

This is my full code at the moment: https://pastebin.com/qbQmEHFc

Yes to both of those changes.

To test the cart out. try

if(isset($_SESSION['cart']))
    print_r($_SESSION['cart']);
Sponsor our Newsletter | Privacy Policy | Terms of Service