Jump to content

bradba

New Members
  • Posts

    7
  • Joined

  • Last visited

bradba's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Got the combo to populate .... a step closer.... As you can see I inserted the variable meat.... Of course don't want to do that but wanted to populate it which was the goal. I'm stuck on how to use $type to select the correct $data variable. And that is the many different variables I need for the value. Meaning I need meatID, meat, meatAmount, meatAmountLabel..... and the same for fruit etc. to insert or update the db table. foreach($data as $row) echo "<option>$row[meat]</option>";
  2. Yes meat - hamburger, pork chops,....... fruit - oranges, bananas, ....... vegs - all the vegetables.... Yes the options for meat, fruit, and vegetables will be the same for all meals and days of the week. If you're asking what did it look like before adding your code it was like this (look below) - I had it like this because I had meat, fruit, vegs in separate combos and populated them like that but as you know that takes a lot of code for all these meals and days..... Meat and vegs combos would be the same but with $row['meatID'], $row['meat']etc etc. Located in each combo. <?php foreach ((array)$data as $row) : <option value="<?php echo $themeal = '1' ?>,<?php echo $row['fruitID'] ?>,<?php echo $row['fruit'] ?>,<?php echo $row['fruitAmount'] ?>, <?php echo $row['fruitAmountLabel'] ?>"><?php echo $row['meat'] ?></option> <?php endforeach ?> I tried to make it like your foreach code removing (array) and adding it like this - $data = array($meat,$fruit,$vegs); like you did for the other values but it's not populating the combos. Arrays really confuse me but looking at your code it simplified it just not sure how to add it to populate the combos from the db. I also tried $row['fruitID'] etc...... but doesn't populate...
  3. This code works except I can't figure out how to use the code below with your code. I've added and messed with it for hours but can't seem to get it work. Basically it's not populating the comboboxes with the values coming from the db. I can't even get it to do that. Plus I need to tell it to print the values for the meat for the meat combobox and fruit for the fruit combobox. The orginal code - <?php foreach ((array)$data as $row) : <option value="<?php echo $themeal = '1' ?>,<?php echo $row['fruitID'] ?>,<?php echo $row['fruit'] ?>,<?php echo $row['fruitAmount'] ?>, <?php echo $row['fruitAmountLabel'] ?>"><?php echo $row['meat'] ?></option> <?php endforeach ?> I've tried many ways but the latest - $data = array($meat,$fruit,$vegs); and tried a nested foreach foreach($types as $type){ echo "$type: "; echo "<select name='{$type}[$meal]'>"; foreach($data as $row) echo "<option value='$day,$type,$meal_number'>$row </option>"; echo "</select><br>"; }
  4. Yes I used the query and it wasn't working. But I cleared the table and ran the query and now it works. You guys are the best. Thanks for the help. I wish I could give all three of you guys the credit for the best answer.
  5. Thanks. I'm using phpmyadmin and I created tables manually rather than using SQL.... I'm a beginner here ... plus the tables are created already. Do I need to recreate the table or can I do this manually in phpmyadmin?
  6. Thanks mac_gyver for all the code. That was going to be my next post on how to code this better. I'll give it a try.... Thank you. I was hoping that would work but since Day of the Week and Meal id are not unique I get an error < everyone will have a userID which is unique, Day of the Week which is not unique, and meal id which is not unique .... adding unique to Day of the Week and Meal ID ... I get > Error in processing request #1062 - Duplicate entry 'Sunday' for key 'dayofweek' .... because each id will have Sunday - Saturday. Same for Meal ID. Maybe you have a different way to do this?
  7. I'm having a problem with determining if the values should be inserted or updated. There's a lot of information needed so I'll try to explain in the quickest way. I'm creating a grocery shopping list. There are many variables which I have slimmed it down because there's a lot of code. The shopping list will have 3 meals per day. Meal 1, Meal 2, Meal 3 A form for each day of the week - Sunday - Saturday I'm only showing code for 2 foods for Meal 1 - Reason there's a lot of code. Please keep in mind 3 foods per meal, 3 meals per day for each day of the week. The problem: I want to update only if 3 values = the values in the db - userID, Day of the week, and Meal. If the values don't exist for the userID, Day of the week, and Meal then insert the data. At this time I'm using ON DUPLICATE KEY UPDATE. It works with the unique key being the userID but the userID will be used many times based on meal and day of week. The userID will be used a total of 3 times per day, 21 times per week as you an see I can't use ON DUPLICATE KEY UPDATE. I would like to compare userID, Day of the week, and Meal and either insert or update based on the comparison. I'm not sure how to do the comparison as explained above using PDO. Here's what I have so far - <form method="post" action=""> <input type="hidden" name="dayofweek" id="dayofweek" value="Sunday"> <h6><strong>Meal 1</strong></h6> <select name="meatSundayMealOne"> <option value="<?php if(isset($errorsmealone)){ echo $_POST['meatSundayMealOne']; } ?>"></option> <?php foreach ((array)$data as $row) : <option value="<?php echo $themeal = '1' ?>,<?php echo $row['meatID'] ?>,<?php echo $row['meat'] ?>,<?php echo $row['meatAmount'] ?>, <?php echo $row['meatAmountLabel'] ?>"><?php echo $row['meat'] ?></option> <?php endforeach ?> </select> <select name="fruitSundayMealOne"> <option value="<?php if(isset($errorsmealone)){ echo $_POST['fruitSundayMealOne']; } ?>"></option> <?php foreach ((array)$data as $row) : <option value="<?php echo $themeal = '1' ?>,<?php echo $row['fruitID'] ?>,<?php echo $row['fruit'] ?>,<?php echo $row['fruitAmount'] ?>, <?php echo $row['fruitAmountLabel'] ?>"><?php echo $row['meat'] ?></option> <?php endforeach ?> </select> <input type="submit" name="sunday_meals" value="Save"> </form> if(isset($_POST['sunday_meals'])){ if(empty($_POST['meatSundayMealOne'])){ $errorsmealone[] = 'Select a Meat for Meal One'; } else { } if(empty($_POST['fruitSundayMealOne'])){ $errorsmealone[] = 'Select a Fruit for Meal One'; } else { } $userID = $_SESSION['userID']; $dayofweek = $_POST['dayofweek']; if(!isset($errorsmealone)){ try { $msmo=explode(",",$_POST['meatSundayMealOne']); $themeal = trim($msmo[0]); $meatID = trim($msmo[1]); $meat = trim($msmo[2]); $meatAmount = trim($msmo[3]); $meatAmountLabel = trim($msmo[4]); $fsmo=explode(",",$_POST['fruitSundayMealOne']); $themeal = trim($fsmo[0]); $fruitID = trim($fsmo[1]); $fruit = trim($fsmo[2]); $fruitAmount = trim($fsmo[3]); $fruitAmountLabel = trim($fsmo[4]); $stmt = $db->prepare('INSERT INTO grocerylist (themeal,dayofweek,meatID,meat,meatAmount,meatAmountLabel,fruitID,fruit,fruitAmount, fruitAmountLabel,userID,creatDate) VALUES (:themeal, :dayofweek, :meatID, :meat, :meatAmount, :meatAmountLabel, :fruitID, :fruit, :fruitAmount, :fruitAmountLabel, :userID, now()) ON DUPLICATE KEY UPDATE themeal=:themeal,dayofweek=:dayofweek,meatID=:meatID,meat=:meat,meatAmount=:meatAmount, meatAmountLabel=:meatAmountLabel,fruitID=:fruitID,fruit=:fruit,fruitAmount=:fruitAmount,fruitAmountLabel=:fruitAmountLabel,updateDate=now()'); $stmt->bindParam(':themeal', $themeal, PDO::PARAM_INT); $stmt->bindParam(':dayofweek', $dayofweek, PDO::PARAM_STR,20); $stmt->bindParam(':meatID', $meatID, PDO::PARAM_INT); $stmt->bindParam(':meat', $meat, PDO::PARAM_STR,20); $stmt->bindParam(':meatAmount', $meatAmount, PDO::PARAM_INT); $stmt->bindParam(':meatAmountLabel', $meatAmountLabel, PDO::PARAM_STR,20); $stmt->bindParam(':fruitID', $fruitID, PDO::PARAM_INT); $stmt->bindParam(':fruit', $fruit, PDO::PARAM_STR,20); $stmt->bindParam(':fruitAmount', $fruitAmount, PDO::PARAM_INT); $stmt->bindParam(':fruitAmountLabel', $fruitAmountLabel, PDO::PARAM_STR,20); $stmt->bindParam(':userID', $userID, PDO::PARAM_INT); $stmt->execute(); } catch(PDOException $e) { $errorsmealone[] = $e->getMessage(); exit(); } } }
×
×
  • 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.