Jump to content

New to php / quick question


Mordero

Recommended Posts

I am taking a php class in college as an elective...I have an assignment to take four numeric inputs from the user, assign them to
session variables, echo them out on a new page within a table.  What I can't figure out is how to keep the amounts from resetting..I need for the user to be able to "add more to cart" so to speak.  here is the code.

INPUT FIELDS (fruity.php)

<h1>Welcome to the Client & Server Side Fruit Store</h1>
<div id="fixedcontainer">

<form action="fruitbasket.php" method="post">

<div class="fruit"><img src="bananas.jpg" alt="bananas mon!" />
<p>Qty: <input type="text" size="2" maxlength="2"  name="banana" id="banana" /></p></div>

<div class="fruit"><img src="apples.jpg" alt="apples mon!" />
<p>Qty: <input type="text" size="2" maxlength="2" name="apple" id="apple" /></p></div>

<div class="fruit"><img src="papaya.jpg" alt="papayas mon!" />
<p>Qty: <input type="text" size="2" maxlength="2" name="papaya" id="papaya" /></p></div>

<div class="fruit"><img src="pineapple.jpg" alt="pineapples mon!" />
<p>Qty: <input type="text" size="2" maxlength="2" name="pineapple" id="pineapple" /></p></div>

<input type="submit" value="Add to Fruitbasket" name="fruitsub" />
</form>
</div>


FRUITBASKET.php (this is where I need to keep track of the number of fruits they want..has to be updated if they select continue shopping)

<?php
session_start();

$getbanana=$_POST['banana'];
$getapple=$_POST['apple'];
$getpapaya=$_POST['papaya'];
$getpineapple=$_POST['pineapple'];


$_SESSION['totbananas'] = $getbanana;
$_SESSION['totapples'] = $getapple;
$_SESSION['totpapayas'] = $getpapaya;
$_SESSION['totpineapples'] = $getpineapple;


echo "<table border='1'>";
echo "<tr>";
echo "<td>Bananas</td><td>Qty=</td><td>" . $_SESSION['totbananas'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Apples</td><td>Qty=</td><td>" . $_SESSION['totapples'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Papayas</td><td>Qty=</td><td>" . $_SESSION['totpapayas'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Pineapples</td><td>Qty=</td><td>" . $_SESSION['totpineapples'] . "</td>";
echo "</tr>";
echo "</table";



?>
<a href="fruity.php">Continue Shopping</a>
</body>
</html>


--------I'm not trying to be a hardcore php programmer, I'm just trying to make it through this class...any help would be greatly appreciated! I know i know, i'm a n00b ; (
Link to comment
https://forums.phpfreaks.com/topic/29155-new-to-php-quick-question/
Share on other sites

what you need to do is use

[code=php:0]
if(isset($_SESSION['totbananas'])) {
$temp = $_SESSION['totbananas'];
$_SESSION['totbananas'] = $getbanana + $temp;
}[/code]

:) I would write a function for it personally, but if you don't want to be 'hardcore', why? :)
This might point you in the right direction:

[code]<?php
  session_start();

  if(isset($_POST['submit'])) {

  $banana = $_POST['banana'];
  $apple = $_POST['apple'];
  $papaya = $_POST['papaya'];
  $pineapple = $_POST['pineapple'];

  $_SESSION['totbananas'] = $banana_sess;
  $_SESSION['totapples'] = $apple_sess;
  $_SESSION['totpapayas'] = $papaya_sess;
  $_SESSION['totpineapples'] = $pineapple_sess;

  addfruit();

  echo"
  <table align=\"center\">
  <tr><td>Bananas = </td><td>$_SESSION['totbananas']</td></tr>
  <tr><td>Bananas = </td><td>$_SESSION['totapples']</td></tr>
  <tr><td>Bananas = </td><td>$_SESSION['totpapayas']</td></tr>
  <tr><td>Bananas = </td><td>$_SESSION['totpineapples']</td></tr>
  </table>";

  }

    function addfruit() {
   
    $_SESSION['totbananas'] = ($bananas + $banana_sess);
    $_SESSION['totapples'] = ($apples + $apple_sess);
    $_SESSION['totpapayas'] = ($papaya + papaya_sess);
    $_SESSION['totpineapples'] = ($pineapples + $pineapple_sess);

    }

?>[/code]

And yes...those values will reset if the user closes their browser. I suggest using cookies if you'd like it to keep those values if they accidently close their browser.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.