Jump to content

farahZ

Members
  • Posts

    80
  • Joined

  • Last visited

Posts posted by farahZ

  1. ya creating an array session is better than the counter

    does it show you the printed array or ur not testing it?

     

     

     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <?php session_start(); ?>
    <?php
    if (isset($_POST['add']))
    {
      if (empty($_POST['foodType']))
      {
        echo 'You need to select some food!'; 
      }
      else
      {
        if (!isset($_SESSION['foodTypes']))
        {
          $_SESSION['foodTypes'] = array();
        }
     
        $_SESSION['foodTypes'][] = $_POST['foodType'];
      }
     
      echo '<pre>' . print_r($_SESSION['foodTypes'], true) . '</pre>';
    }
    ?>
    <input name="Submit" type="submit" value="Submit" />
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Calorie Counter</title>
    </head>
        
    <body>
    <p>It's Breakfast </p>
    <p>
    <form action="" method="post">
      <p>Keep on adding items:</p>
      <p> when you are done, click submit</p>
      <p>
        <select name="foodType" size="1" id="foodType">
          <option value="Labneh" selected="selected">Labneh - لبنة</option>
          <option value="Cucumber">Cucumber-خيار</option>
          <option value="Tomato">Tomato-بندورة</option>
          <option value="Lettuce">Lettuce-خس</option>
          <option value="Cucumber Pickles">Cucumber Pickles -كبيس خيار</option>
          <option value="Olive Oil">Olive Oil- زيت زيتون</option>
          <option value="Cucumber without peal">Cucumber without peal - خياره مقشرة</option>
          <option value="Olive Black">Olive Black- زيتون أسود</option>
          <option value="Olive Green">Olive Green - زيتون اخضر</option>
        </select>
        <input name="submit" type="submit" id="submit"  value="add">
      </p>
     
    </form>
     
    </body>
    </html>
    
  2. hello, your reply is much appreciated  :happy-04: 
    I corrected everything you said.. i'm new to all this world

     

    i will make myself clearer.. the calorie counter idea is that the user must add to the list all items from the dropdown list he wants by choosing the food item and clicking add. upon each 'add' click, all items chosen are continuously printed. when  he is done adding, he must press submit for the calculation of the calories to take place.(count the calories of the food he chose)

     

    here is the corrected code

     

     
    <?php session_start(); ?>
    <?php
    if (isset($_POST['submit']))
    {
         chosenItems();
    }
     
    $_SESSION['counter'] = (!$_SESSION['counter']) ? 0 : $_SESSION['counter'];
    if($_POST['submit']) {
     $_SESSION['counter']++;
    }
    function chosenItems()
    {
    $foodSelected[
    $_SESSION['counter']]= $_REQUEST["foodType"];
    echo "food added: ", $_SESSION['counter'], "<BR>";
     
    for ($i=0; $i<$_SESSION['counter']; $i++)
    echo $foodSelected[$i], "<BR>";
    }
    ?>
    <input name="Submit" type="submit" value="Submit" />
    <?
    if($_POST['submit']) {
     unset($_SESSION['counter']);
    }
    echo "session destroyed";
    ?>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Calorie Counter</title>
    </head>
        
    <body>
    <p>It's Breakfast </p>
    <p>
    <form action="" method="post">
      <p>Keep on adding items:</p>
      <p> when you are done, click submit</p>
      <p>
        <select name="foodType" id="foodType">
          <option value="Labneh" selected="selected">Labneh - لبنة</option>
          <option value="Cucumber">Cucumber-خيار</option>
          <option value="Tomato">Tomato-بندورة</option>
          <option value="Lettuce">Lettuce-خس</option>
          <option value="Cucumber Pickles">Cucumber Pickles -كبيس خيار</option>
          <option value="Olive Oil">Olive Oil- زيت زيتون</option>
          <option value="Cucumber without peal">Cucumber without peal - خياره مقشرة</option>
          <option value="Olive Black">Olive Black- زيتون أسود</option>
          <option value="Olive Green">Olive Green - زيتون اخضر</option>
        </select>
        <input name="submit" type="submit" id="submit"  value="add">
      </p>
     
    </form>
     
    </body>
    </html>
    
  3. hello, what am trying to do is that to allow the user choose from a dropdown list items (item by item) and add each, constantly viewing the added items. the added items must be saved in an array (foodSelected).

    the problem i am facing is with creating is with creating a variable, and incrementing this variable by one each item the user adds an item!!
    i want that counter for several reason:

    - array printing

    - sending the array items to the database

     

    i read in the forums something about $_session variables, i tried to implement it.. but still got errors

    thats the code i wrote, its a calorie counter.. i will appreciate any help

     

     

    <?php session_start(); ?>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Calorie Counter</title>
    </head>
        
    <body>
    <p>It's Breakfast </p>
    <p>
    <form action="" method="post">
      <p>
        <select name="foodType" id="foodType">
          <option selected="selected">Labneh</option>
          <option>Cucumber</option>
          <option>Tomato</option>
          <option>Lettuce</option>
          <option>Cucumber Pickles</option>
          <option>Olive Oil</option>
          <option>Cucumber without peal</option>
          <option>Olive Black</option>
          <option>Olive Green</option>
        </select>
        <input name="submit" type="submit" id="submit"  value="add">
      </p>
    </p>
    </form>
    <?php
    if (isset($_POST['submit']))
    {
         chosenItems();
     
    }
     
    $_SESSION['counter'] = (!$_SESSION['counter']) ? 0 : $_SESSION['counter'];
    if($_POST['add']) {
     $_SESSION['submit']++;
    }
    function chosenItems()
    {
    $foodSelected[$counter]= $_REQUEST["foodType"];
    echo "food added: ", "<BR>";
    print_r($foodSelected);
    }
    ?>
     
    </body>
    </html>

     

×
×
  • 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.