3

I would like to know how to increase a element, each time that element is posted.

I have to use for loop for the auto increment, but i am not getting right. So any advise or guidance will be great.

Here is the way i have tried to do:

Thanks

<?php

$id=0;    
if (isset($_POST['submit'])) {
  $do = $_POST['prodCode'];
  $di = count($do);

  while ($di > $id) { 
    $id++;
    echo $id;
  }
}
?>

<!DOCTYPE HTML>
<html>
  <head>
    <title>Session test</title>
  </head>
  <body>
    <div class="holder">
      <div class="im">
        <img src="session-test/images/bestorange-juice.jpg" />
        <p>bestorange-juice</p>
        <form method="post" action="sessiontest.php">
          <input type="hidden" id="prodCode" name="prodCode" value="f102" />
          <input type="hidden" id="prodPrice" name="prodPrice" value="25" />
          <!--<input type="text" id="prodQty" name="prodQty" value="1" size="1"/>-->
          <input type="submit" value="send value" name="submit" id="submit" />
        </form>
      </div>
      <div class="im">
        <img src="session-test/images/milkshake-juice.jpg" />
        <p>bestorange-juice</p>
        <form method="post" action="sessiontest.php">
          <input type="hidden" id="prodCode" name="prodCode" value="W122" />
          <input type="hidden" id="prodPrice" name="prodPrice" value="1" />
          <!--<input type="text" id="prodQty" name="prodQty" value="1" size="1"/>-->
          <input type="submit" value="send value" name="submit" id="submit" />
        </form>
      </div>
    </div>
  </body>
</html>
2
  • Do you mean increment quantity each time send value is clicked? Commented Sep 29, 2015 at 19:52
  • @mergenchik: Yes that is what i want increase the quantity..each time the send value is clicked...thanks Commented Sep 29, 2015 at 20:25

2 Answers 2

2

Try below code, counts are stored in session, but for real life app you should use database and also you should get your products from database:

<?php

// initialize counts for f102 and W122 products
if (!isset($_SESSION['count_f102']) {
   $_SESSION['count_f102'] = 0;
}
if (!isset($_SESSION['count_W122']) {
   $_SESSION['count_f102'] = 0;
}

if (isset($_POST['submit'])) {
  $do = $_POST['prodCode'];
  // increment count for product which was submitted
  $_SESSION['count_'.$do] = 1+ (int) $_SESSION['count_'.$do];
}
?>

<!DOCTYPE HTML>
<html>
  <head>
    <title>Session test</title>
  </head>
  <body>
    <div class="holder">
      <div class="im">
        <img src="session-test/images/bestorange-juice.jpg" />
        <p>bestorange-juice</p>
        <form method="post" action="sessiontest.php">
          <input type="hidden" id="prodCode" name="prodCode" value="f102" />
          <input type="hidden" id="prodPrice" name="prodPrice" value="25" />
          <input type="text" id="prodQty" name="prodQty" value="<?php $_SESSION['count_f102'] ?>" size="1" readonly="readonly" />
          <input type="submit" value="send value" name="submit" id="submit" />
        </form>
      </div>
      <div class="im">
        <img src="session-test/images/milkshake-juice.jpg" />
        <p>bestorange-juice</p>
        <form method="post" action="sessiontest.php">
          <input type="hidden" id="prodCode" name="prodCode" value="W122" />
          <input type="hidden" id="prodPrice" name="prodPrice" value="1" />
          <input type="text" id="prodQty" name="prodQty" value="<?php $_SESSION['count_W122'] ?>" size="1" readonly="readonly" />
          <input type="submit" value="send value" name="submit" id="submit" />
        </form>
      </div>
    </div>
  </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

1

Are your $_POST['prodCode'] always in the form one letter + id ? If yes maybe this could help :

if (isset($_POST['prodCode'])) {
    $value = $_POST['prodCode'];
    // save the letter
    $letter = substr($value, 0, 1);
    // get id
    $id = (int) substr($value, 1, strlen($value) - 1);
    // get value with letter and incremented id
    $valueIncremented = $letter . ++$id;
}
// with $_POST['prodCode'] = 'f102' you will get $valueIncremented = 'f103'

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.