/    Sign up×
Community /Pin to ProfileBookmark

using foreach variable in sql query

aim: display the dish category title and then the dishes under/ foreach category title
this is what I’ve done so far well at least a snippet:

[code]
–> foreach ($assocarrsubcattitle_descript as $title => $descript) {
if ($descript != NULL) {
?>
<div class=”row”>
<div class=”col-xs-12″>
<p class=”titleSubCatg_title”>
<?php echo $title; ?>
</p>
<div>
<div class=”row”>
<div class=”col-xs-12″>
<p class=”titleSubCatg_title”>
<?php echo $descript; ?>
</p>
</div>
</div>
</div>
<?php
–> $querySubCatgDishTitle_DishPrice = “SELECT DishTitle, DishPrice FROM MainTblMenuItem INNER JOIN SubCategory ON FK_MenuItemSubCategoryId = PK_MenuItemSubCategoryId WHERE SubCategoryTitle = $title”;
$runSql2_qry1 = mysqli_query($connect, $querySubCatgDishTitle_DishPrice);

if ($runSql2_qry1) {
if (mysqli_num_rows($runSql2_qry1) > 0) {
while ($assocarrSubCatgDishTitles_MainDishPrices = mysqli_fetch_assoc($runSql2_qry1)) {
?>
[/code]

I’ve used –> to indicate the loop variable and the –> to indicate it the query
How would one use the loop variable $title in the where clause of another query?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmMay 21.2019 — You seem to be doing just fine. BUT - it is generally not good practice to perform queries inside loops. Instead re-write your original query to include the data you are trying to extract with the second query.
Copy linkTweet thisAlerts:
@NogDogMay 21.2019 — You need to put quotes around '$title' in the query -- assuming it's a text column of some sort? And the value should be appropriately escaped -- or better yet use a prepared statement with a bound parameter for that value.

I would agree that, if feasible, I'd look into doing it with one query, leveraging table joins and/or sub-queries, and possibly some aggregate functions with a group by clause -- but that depends on a clearer understanding of the data design and the actual logic you're trying to satisfy here.
Copy linkTweet thisAlerts:
@nsathauthorMay 22.2019 — @NogDog#1603902

Yeh I've done that, that's fine. I'd don't really know much about escape characters in PHP but after a few hours of searching around the net I found the solution

Just to give you a heads up

the '$title' just takes it as a string value to compare it against the value of SubCategoryTitle in the where clause

the solution is '".$title."'

Either way thanks for that
Copy linkTweet thisAlerts:
@nsathauthorMay 22.2019 — @ginerjm#1603898

I couldn't find way of doing this as each category has it own unique dishes hence why I thought along the lines of the foreach loop, I know its not good practice and all
Copy linkTweet thisAlerts:
@NogDogMay 22.2019 — > @nsath#1603905 I know its not good practice and all

If this is for that menu application I think I recall, then it's probably not a big deal, as we're talking about a pretty small data set. That being said, I _might_ consider implementing something that runs in the background to populate some data store (a JSON file, perhaps?) with the menu data structured the way you want to process it, since in theory it should only change occasionally. That way you could, perhaps, have the admin side of the application that allows you to add/update/delete menu items and categories to regenerate that file upon any successful change. Then when you want to display the menu, just file_get_contents() and json_decode(), and you have an array of menu data ready for you to output.
Copy linkTweet thisAlerts:
@ginerjmMay 22.2019 — Actually the solution is much, much simpler.
<i>
</i> $querySubCatgDishTitle_DishPrice = "SELECT DishTitle, DishPrice FROM MainTblMenuItem
INNER JOIN SubCategory
ON FK_MenuItemSubCategoryId = PK_MenuItemSubCategoryId
WHERE SubCategoryTitle = '$title'";

Again - strings in double quotes will have their php vars interpreted correctly unless they are what I call complex var names. For those guys curly braces will solve that problem; for normal simple var names the single quotes will suffice without having to resort to concatenation, as you did.
×

Success!

Help @nsath spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...