Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi See my code below,
My code seems to be working okay .. but it just seems like a lot of copy and paste code to suit to each food.

I am currently displaying meals in relation to 'keywords' in which were given to a user, which they were able to select which ones they prefer. Basically, what it's doing is displaying meals based on their preferences(which they completed via a checkbox form)

If anyone can help me out here that would be really much appreciated.

What I have tried:

include ("dbConnect.php");
$1 = ("cheese");
$2 = ("yoghurt");
$3 = ("egg");
$4 = ("fruit");
$5 = ("cereal");
$6 = ("oatmeal");
	
   $dbQuery=$conn->prepare("select * from FoodQuestionnaireResults where UserID=$UserID");
   $dbQuery=$conn->prepare("select * from Recipes where Keyword = '$1'");
   $dbQuery->execute();
   while ($dbRow=$dbQuery->fetch(PDO::FETCH_ASSOC)) {
if ($B1 = '1') {
  echo "<br><br><h4>".$dbRow["Name"]."</h4><br>"."<br><img src=Breakfast/".$dbRow['Picture']."' width='150' height='150' />"."<br><br>".$dbRow["Instructions"].";
} else {
    echo "";
}
   $dbQuery=$conn->prepare("select * from Recipes where Keyword = '$2'");
   $dbQuery->execute();
   while ($dbRow=$dbQuery->fetch(PDO::FETCH_ASSOC)) {
if ($B2 = '1') {
  echo "<br><br><h4>".$dbRow["Name"]."</h4><br>"."<br><img src=Breakfast/".$dbRow['Picture']."' width='150' height='150' />"."<br><br>".$dbRow["Instructions"].";
} else {
    echo "";
}
   }
   }
?>
Posted
Updated 23-Mar-18 21:38pm
Comments
CHill60 23-Mar-18 14:53pm    
What is the actual problem? Why would you cut and paste for each food? Use an array perhaps? PHP: Arrays - Manual[^] or a collection? PHP: Collection - Manual[^]

1 solution

I suspect you'll find that if ($B1 = '1') doesn't do what you think it does. Hint: In javascript, = is the assignment operator, == is the check-for-equality operator. ;)
SQL unfortunately, uses a single = to check for equality - this confuses many new users when they must use different operators in different languages to perform the same task.

Here's how I'd go about it.

<?php
$userName = 'root';
$password = '';
$pdo = new PDO('mysql:host=localhost', $userName, $password);
$pdo->query("use testjobsdb");
	
$materials = Array("10A GPO Triple","10A GPO Double","10A GPO Single","10A GPO Double W/extra");
$stmt = $pdo->prepare("select * from Materials where name = :someStockItem");

forEach($materials as $curMaterial)
{
	$stmt->bindParam(":someStockItem", $curMaterial);
	$stmt->execute();
	while ( $dbRow = $stmt->fetch(PDO::FETCH_ASSOC) )
	{
		echo "<h4>".$dbRow["id"]."</h4><br>" . $dbRow['name'] . "<br>";
	}
}
?>
 
Share this answer
 
Comments
Member 13637584 29-Mar-18 18:35pm    
Hi, wheres the $curMaterial being defined here?
enhzflep 31-Mar-18 6:53am    
In the forEach loop. If you don't understand the syntax, you should look it up in the php documentation, here: PHP.net forEach control structure

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900