Jump to content

filling 2 array sessions


farahZ

Recommended Posts

i can't figure out where my problem is!!
I'm trying to input the quantity (text box) of each food chosen (drop down list) by the user..

food inputs working fine but quantity is storing the first item in the array only!
 

 

if (!isset($_SESSION['foodTypes']))
    {
 // if the session is not yet created, create it now
      $_SESSION['foodTypes'] = array();
 $_SESSION['Quantity']= array ();
 $c=0;
    }
 

 // check to see if the newly added food type is not already in the array
    if (in_array($_POST['foodType'], $_SESSION['foodTypes']) === false) 
    {
      // The selected food item is not in the array
      // add the selected food item to total food array
      $_SESSION['foodTypes'][] = $_POST['foodType'];
 $foodQuan = $_POST['fooDquantity'];
 $foodVal=$_POST['quantityValue'];
//only first item of quantity is added
 $_SESSION['Quantity'][$c]= $foodQuan ." ". $foodVal;
 echo  $_SESSION['Quantity'][$c];
 echo $c++;
    }
 
//submitting the food to the database (first item of quantity is sent only)
// user presses submit, after he is done adding all food he wants

$c=0; (starting from ) to pass by all quantity registered)
foreach ($_SESSION['foodTypes'] as $food)
{
$cal=0;
$sql="";
$clid=$_SESSION['views'][0];
$q=$_SESSION['Quantity'][$c];
echo $q;
 
$sql = "INSERT INTO fooddiary (ID, Date, DayTime, Food, Quantity, Calories)
VALUES ('$clid', '$date', 'Lunch', '$food', '$q', $cal)";
$c++;
$result = mysqli_query($con, $sql) or die(mysqli_error());
}
 


 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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