/    Sign up×
Community /Pin to ProfileBookmark

run another query on mysqli_fetch_assoc array ONLY on first array

Hi I’m working menu page for a restaurant and in the process of fetching data from MySQL database to display on page.
The problem I’m having is that there is 3 Main Categories, being Traditional Favourites, Starters, and Mains
ONLY Within Traditional Favourites and are 3 SubCategories, being Dosa, Steamed Rice Cakes & Breads

Traditional Favourites as Heading
Dosa, Steamed Rice Cakes & Breads as Sub Headings
under each Sub Heading should be dish titles and price

Below is what I’ve created so far:

[code]
$connect = mysqli_connect($hostname, $username, $password);
mysqli_select_db($connect, $database_name);

if ($connect) {
$queryCatTitle = “SELECT lookupTblMenuItem_Category.lookupTblMenuItem_Category_CategoryTitle FROM lookupTblMenuItem_Category WHERE lookupTblMenuItem_Category.lookupTblMenuItem_Category_CategoryTitle = ‘Traditional Favourites'”;
$runSql = mysqli_query($connect, $queryCatTitle);
if($runSql) {
if (mysqli_num_rows($runSql) > 0) {
while ($resultCatTitle = mysqli_fetch_assoc($runSql)) {
print_r($resultCatTitle);
}
}
}
} else {
die(“Connection could not be established: ” . mysqli_connect_error());
}

?>
[/code]

being a newbie to PHP MySQL I’m not even sure is this is correct but having tested it it displays Array ( [lookupTblMenuItem_Category_CategoryTitle] => Traditional Favourites ) on the page so I know I’m half way there.

Could someone please possible provide me with a pointer, ideas, or even a solution?

Many thanks

__(Added `[code]…[/code]` tags ~ MOD)__

to post a comment
PHP

18 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 02.2019 — Assuming I'm understanding what you want to do, I might do a query that gets everything, then loop through the results to populate an array structured to give you the various categories.
<i>
</i>$sql = "
SELECT
each,
field,
you,
want,
including,
lookupTblMenuItem_Category_CategoryTitle
FROM lookupTblMenuItem_Category
";
$result = mysqli_query($connect, $sql);
$menu = array();
while($row = mysqli_fetch_assoc($result)) {
switch($row['lookupTblMenuItem_Category_CategoryTitle']) {
case 'Dosa':
case 'Steamed Rice Cakes':
case 'Breads':
$menu['Traditional Favorites'][$row['lookupTblMenuItem_Category_CategoryTitle']] = $row;
break;
default:
$menu[$row['lookupTblMenuItem_Category_CategoryTitle']] = $row;
}
}

// See what the array looks like now:
echo "&lt;pre&gt;menu:n".print_r($menu,1)."&lt;/pre&gt;";

You could then extract the sub-arrays you want from that $menu array (assuming that code works ;) ), and loop through it to output as desired.
Copy linkTweet thisAlerts:
@mandyrozanaApr 02.2019 — You have to check both codes once. set array according to your requirement.
Copy linkTweet thisAlerts:
@ginerjmApr 02.2019 — Have to address your query structure first off.

Instead of writing this let's try something to simplify it:
<i>
</i> $queryCatTitle = "SELECT a.lookupTblMenuItem_Category_CategoryTitle
FROM lookupTblMenuItem_Category a
WHERE a.lookupTblMenuItem_Category_CategoryTitle = 'Traditional Favourites'";

Here I demonstrate how to use an alias to shorten the references to your fields. Not necessary but since you were doing it I am showing you how to make it easier to read.

Now - do your fieldnames ACTUALLY reiterate the table name within them? Is your field name simply just "Category_Title" or did you embed the table name into each and every table column?

__(Changed [php] tags to [code] tags ~ MOD )__
Copy linkTweet thisAlerts:
@ginerjmApr 02.2019 — PS - tried to use the </> symbol to wrap my code, but once again this forum doesn't always work right.
Copy linkTweet thisAlerts:
@NogDogApr 02.2019 — > @ginerjm#1602425 PS - tried to use the </> symbol to wrap my code, but once again this forum doesn't always work right.

It's ..., not [php]...[/php] (that the old forum software used to support). ;)
Copy linkTweet thisAlerts:
@ginerjmApr 02.2019 — Darn... Still - it would be nice if the icon worked.
Copy linkTweet thisAlerts:
@nsathauthorApr 03.2019 — I was thinking it would have been something along the lines of a while / for loop which would use for example $row[0], and then run another query to retrieve the subCategories but @NogDog your idea of a switch statement seems more logical
Copy linkTweet thisAlerts:
@nsathauthorApr 03.2019 — @ginerjm#1602424

ok yeh I had written queries both ways one without table names and also with but used the one with table names
Copy linkTweet thisAlerts:
@nsathauthorApr 03.2019 — @ginerjm#1602425

I did but didn't turn out right
Copy linkTweet thisAlerts:
@nsathauthorApr 03.2019 — as with my replies 🤣
Copy linkTweet thisAlerts:
@nsathauthorApr 04.2019 — [upl-image-preview url=https://www.webdeveloper.com/assets/files/2019-04-04/1554342218-239353-aim.jpeg]

basically this is what i'm trying to accomplish if it helps
Copy linkTweet thisAlerts:
@nsathauthorApr 04.2019 — I reworked the php and this is where I've gotten to but nothing displays on the page

if ($connect) {<br/>
$queryCatTitle = "SELECT lookupTblMenuItem_Category_CategoryTitle FROM lookupTblMenuItem_Category";<br/>
$runSql = mysqli_query($connect, $queryCatTitle);<br/>
if ($runSql) {<br/>
if (mysqli_num_rows($runSql) &gt; 0) {


<i> </i><CODE> $i = 0;

<i> </i> while ($resultCatTitle = mysqli_fetch_array($runSql)) {

<i> </i> while ($lookupTblMenuItem_Category_CategoryTitle[$i] === $resultCatTitle[0]) {

<i> </i> ?&gt;

<i> </i> &lt;div class="row"&gt;

<i> </i> &lt;p class="category-title"&gt;

<i> </i> &lt;?php echo $lookupTblMenuItem_Category_CategoryTitle[$i]; ?&gt;

<i> </i> &lt;/p&gt;
<i> </i> &lt;!-- // category-title --&gt;

<i> </i> &lt;/div&gt;
<i> </i> &lt;!-- // row --&gt;

<i> </i> &lt;?php


<i> </i> $querySubCatTitle = "SELECT lookupTblMenuItem_SubCategory_SubCategoryTitle FROM lookupTblMenuItem_SubCategory INNER JOIN lookupTblMenuItem_Category ON lookupTblMenuItem_SubCategory_FK_MenuItemCategoryId = lookupTblMenuItem_Category_PK_MenuItemCategoryId WHERE lookupTblMenuItem_Category_CategoryTitle = 'Traditional Favourites'";
<i> </i> $runSql1 = mysqli_query($connect, $querySubCatTitle);
<i> </i> if ($runSql1) {
<i> </i> if (mysqli_num_rows($runSql1) &gt; 0) {
<i> </i> while ($resultSubCatTitle = mysqli_fetch_assoc($runSql1)) {
<i> </i>
<i> </i> }
<i> </i> }
<i> </i> }
<i> </i> }
<i> </i> }


<i> </i> }
<i> </i> }
<i> </i>}
Copy linkTweet thisAlerts:
@ginerjmApr 04.2019 — Add an echo showing how many rows were returned.

ps - your previous replies to my posts made no sense at all. Plus you didn't anwer my questions.
Copy linkTweet thisAlerts:
@nsathauthorApr 04.2019 — Yeh sorry about the structure of the queries

I did use the another format being:

<i>
</i>$queryCatTitle = "SELECT lookupTblMenuItem_Category_CategoryTitle
FROM lookupTblMenuItem_Category";


I did embed the table name into each and every column,

also I have used print_r instead of the echo which the result is as below (screenshot[upl-image-preview url=https://www.webdeveloper.com/assets/files/2019-04-04/1554394932-944132-untitled.png]

)
Copy linkTweet thisAlerts:
@nsathauthorApr 04.2019 — @nsath#1602507 also NOTE: I changed it from mysqli_fetch_assoc to mysqli_fetch_array
Copy linkTweet thisAlerts:
@ginerjmApr 04.2019 — YOu should use fetch_assoc since fetch_array gives you two arrays when you really only need the assoc one. Where is the count of the number of rows that I asked for? You say you are not getting results so I wanted to have you print out how many results the query was returning.

WHY in h... would you embed the table name into every single field of your table????? What a nightmare to have to type all of those long field names EVERY TIME you use one of them. Please - wake up and stop doing this.

Ps - what is that print that you managed to provide us even tho it is hardly readable since it is so tiny? Does it tell you anything? It doesn't tell me anything.
Copy linkTweet thisAlerts:
@ginerjmApr 04.2019 — Don't know what happened to the post I made about 10 mins ago. I'll try again.

Go back to using assoc, not array. Fetch_array returns two whole arrays of results. Why do you need that? Stick with the one that works for your current situation, which is usually fetch_assoc.

Did you find out how many rows were being returned? You didn't respond with any info on my question regarding it. And what the h... is that print_r(?) showing us? An array obviously but meaningless without some context.
Copy linkTweet thisAlerts:
@ginerjmApr 04.2019 — Something is wrong with the forum. My post keeps disappearing and comes back only if I hit refresh on my browser. That and the fact that the OP is reluctant to make this a more readable topic make me realize that I need to stop following this.

Good luck.
×

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.11,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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