farahZ Posted May 29, 2013 Share Posted May 29, 2013 hey,i dont know if my work is correct or no ! I am trying to create several session arrays, and fill them with user inputs the common between the session arrays will be the counter ($c) then using the counter, with the help of a for loop, i want to print the arrays !! here is the code.. please have a look, any mistake or notice please inform me <?php if (isset($_POST['add'])) { // check if an option has been selected if (empty($_POST['foodType'])) { echo 'You need to select some food!'; } else { if (!isset($_SESSION['foodTypes']) || !isset($_SESSION['Quantity']) || !isset($_SESSION['Calories']) || !isset($_SESSION['Unit'])) { // if the session is not yet created, create it now $_SESSION['foodTypes'] = array(); $_SESSION['Quantity']= array (); $_SESSION['Calories']=array(); $_SESSION['Unit']=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'][$c] = $_POST['foodType']; $_SESSION['Quantity'][$c]= $_POST['fooDquantity']; $_SESSION['Unit'][$c]=$_POST['quantityValue']; echo $c; $c++; //$foodQuan = $_POST['fooDquantity']; // $foodVal=$_POST['quantityValue']; // $_SESSION['Quantity'][$c]= $foodQuan ." ". $foodVal; } } } function print_array(){ if(!empty($_SESSION['foodTypes'])){ echo '<br>'. '<br>'. "Food Added:" . '<br>'; // display the current food list for ($i =0; $i <=$c; $i++) { $fcal=getCalories($_SESSION['foodTypes'][i], $_SESSION['Quantity'][i], $_SESSION['Unit'][i]); echo $_SESSION['foodTypes'][i] . ' ' . $fcal . ' calories' .'<br>'; $calories= $calories + $fcal; } /* $_SESSION['Calories']=$fcal; foreach ($_SESSION['foodTypes'] as $food) { $fcal=getCalories($food, $foodQuan, $foodVal); echo $food . ' ' . $fcal . ' calories' .'<br>'; $calories= $calories + $fcal; //$calories= calculate($_SESSION['foodTypes']); } */ echo 'The amount of Calories is now: ' . $calories, "\n"; createDropdown($_SESSION['foodTypes']); } ?> } Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 29, 2013 Share Posted May 29, 2013 hey,i dont know if my work is correct or no ! Does it work? Are you really asking us to help with code and you don't even know if there's a problem? Quote Link to comment Share on other sites More sharing options...
farahZ Posted May 29, 2013 Author Share Posted May 29, 2013 Hey Its not working and I have no clue why Problem with the counter cz I tried echoing it.. no value nothing Quote Link to comment Share on other sites More sharing options...
requinix Posted May 29, 2013 Share Posted May 29, 2013 You aren't saving the value of $c and your for loop at the bottom is missing some $s. At least. Quote Link to comment Share on other sites More sharing options...
farahZ Posted May 29, 2013 Author Share Posted May 29, 2013 Saving the value of $c?? What do u mean? Any quick example Quote Link to comment Share on other sites More sharing options...
requinix Posted May 29, 2013 Share Posted May 29, 2013 You do stuff with $c but it never comes from the session or gets saved into the session. It starts at 0 every time. You don't even need it though if you moved your data around to something a little better. Try a structure like $_SESSION[food] = array( array( [type] => [quantity] => [calories] => [unit] => ), array( [type] => [quantity] => [calories] => [unit] => ), array( [type] => [quantity] => [calories] => [unit] => ), ... )If the food type is a string you can use it as array keys too so it's easier to tell if one has been... "used", or whatever... yet. Quote Link to comment Share on other sites More sharing options...
farahZ Posted May 30, 2013 Author Share Posted May 30, 2013 i think i got your idea but how will be able to call them afterwards for printing? because each foodtype has its own type, quantity and unit.. thats the neat code.. i really dont know if i'm able to make myself clear if (isset($_POST['add'])) { // check if an option has been selected if (empty($_POST['foodType'])) { echo 'You need to select some food!'; } else { if (!isset($_SESSION['foodTypes']) || !isset($_SESSION['Quantity']) || !isset($_SESSION['Unit'])) { // if the session is not yet created, create it now $_SESSION['foodTypes'] = array(); $_SESSION['Quantity']= array (); $_SESSION['Unit']=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'][$c] = $_POST['foodType']; $_SESSION['Quantity'][$c]= $_POST['fooDquantity']; $_SESSION['Unit'][$c]=$_POST['quantityValue']; $c++; } } } if (isset($_POST['submit'])) { // check if the session exists, or if its empty if (!isset($_SESSION['foodTypes']) || empty($_SESSION['foodTypes'])) { echo 'No food in the list to submit!'; } else { $con=mysqli_connect("localhost","root","","inshapewebsite"); if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $date=date("Y-m-d"); for ($i =0; $i <=$c; $i++) { $cal=getCalories($_SESSION['foodTypes'][i], $_SESSION['Quantity'][i], $_SESSION['Unit'][i]); $sql=""; $clid=$_SESSION['views'][0]; $q= $_SESSION['Quantity'][i]. ' ' . $_SESSION['Unit'][i]; $food=$_SESSION['foodTypes'][i]; $sql = "INSERT INTO fooddiary (ID, Date, DayTime, Food, Quantity, Calories) VALUES ('$clid', '$date', 'Lunch', '$food', '$q', $cal)"; var_dump($sql); $result = mysqli_query($con, $sql) or die(mysqli_error()); } $total= calculate($_SESSION['foodTypes'], $_SESSION['Quantity'], $_SESSION['Unit']); echo 'The total calories submitted is ' . $total, "\n"; unset($_SESSION['foodTypes']); mysqli_close($con); } function calculate($foodTypes, $Quantity, $Unit){ $calories = 0; for ($i =0; $i <=$c; $i++) { $fcal=getCalories($_SESSION['foodTypes'][i], $_SESSION['Quantity'][i], $_SESSION['Unit'][i]); $calories= $calories + $fcal; } return $calories; } function getCalories($food, $foodQuan, $foodVal) { // Create connection $con=mysqli_connect("localhost","root","","inshapewebsite"); // Check connection if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $query = "SELECT Size, Calories FROM food where Food='$food' "; $result = mysqli_query($con,$query); $row = mysqli_fetch_array($result); if($foodVal=='Portion') { $fcal=$foodQuan*$row['Calories']; } else if($foodVal=='Grams') { $fcal=($foodQuan*$row['Calories'])/$row['Size']; } else $fcal=$row['Calories']; return $fcal; } Quote Link to comment Share on other sites More sharing options...
farahZ Posted May 30, 2013 Author Share Posted May 30, 2013 as u have said before, the problem is with $c .. its not being changed or saved! Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 30, 2013 Share Posted May 30, 2013 by using three arrays, you are making three times too much code. do this - If the food type is a string you can use it as array keys too so it's easier to tell if one has been... "used", or whatever... yet. and all your code will be simple - if (isset($_POST['add'])){ if (empty($_POST['foodType'])){ echo 'You need to select some food!'; } else { if(!isset($_SESSION['food'][$_POST['foodType']])){ $_SESSION['food'][$_POST['foodType']] = array('Quantity' => $_POST['fooDquantity'], 'Unit' => $_POST['quantityValue']); } } } Quote Link to comment Share on other sites More sharing options...
farahZ Posted May 30, 2013 Author Share Posted May 30, 2013 and how can i print them and submit it? i.e. how can i extract their values??? using a for loop or foreach?? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 30, 2013 Share Posted May 30, 2013 you would just use a foreach(){} loop. for your database query to get the calorie information, you should not run a query inside of a loop. since the foodTypes are the keys of the $_SESSION['food'] array, just get all the keys and run one query to get all the rows you need - $types = implode("','",array_keys($_SESSION['food'])); $query = "SELECT Food, Size, Calories FROM food where Food IN('$types')"; $result = mysqli_query($con,$query); $calories = array(); while($row = mysqli_fetch_assoc($result)){ $calories[$row['Food']] = $row; // store the rows using the Food as the key so that this information can be easily accessed when needed } as you loop over the $_SESSION['food'] array, the key (foodType) from this array can be used to get the corresponding calories/size information from the $calories array that was formed in the above code. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.