/    Sign up×
Community /Pin to ProfileBookmark

normal array or 2 dimensional array?

Query used retrieves two column’s values menuItem and the price

`$querySubCatDishTitlePrice = “SELECT MenuItemTitle, MenuItemPrice FROM MenuItem INNER JOIN SubCategory ON FK_SubCategoryId = PK_SubCategoryId WHERE SubCategoryTitle = ‘Dosai'”;`

from this for each row I need create a new bootstrap row with two columns one holding the item title/ dish name and the other holding the price.

`<div class=”row”>
<div class=”col-*-*”> <?php echo $title; ?> </div>
</div>
<div class=”row”>
<div class=”col-*-*”> <?php echo $price; ?></div>
</div>`

I’m not too sure but idea I have is create a two dimensional array from mysqli_fetch_assoc, the first dimension holding the title and the second holding the price. Am I right to presume this? or is there a better way of doing this?
Ideas, solutions, pointers appreciated

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@nsathauthorApr 23.2019 — OK, I had an instinct that I could just manipulate the results using the associative array returned instead of reassigning it to an array or even a 2 dimensional one at that. code as below:

if (mysqli_num_rows($runSql_qry1_1) &gt; 0) {<br/>
$resultArrSubCatgTitleDishTitlePrice = array();<br/>
while ($resultDishTitle_DishPrice = mysqli_fetch_assoc($runSql_qry1_1)) {


<i> </i><CODE> $title = $resultDishTitle_DishPrice['MenuItemTitle'];
<i> </i> $price = $resultDishTitle_DishPrice['MenuItemPrice'];
<i> </i> foreach ($resultDishTitle_DishPrice as $title =&gt; $price) {
<i> </i> ?&gt;

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

<i> </i> &lt;div class="col-sm-10"&gt;

<i> </i> &lt;?php echo $title; ?&gt;

<i> </i> &lt;/div&gt;

<i> </i> &lt;div class="col-sm-2"&gt;

<i> </i> &lt;?php echo $price; ?&gt;

<i> </i> &lt;/div&gt;

<i> </i> &lt;/div&gt;
<i> </i>
<i> </i> &lt;?php
<i> </i> }


<i> </i>
<i> </i> }
<i> </i> }

but its not exactly giving me what I wanted:

the result is:

[upl-image-preview url=https://www.webdeveloper.com/assets/files/2019-04-23/1555987377-29364-untitled1.png]
Copy linkTweet thisAlerts:
@nsathauthorApr 23.2019 — Well I fixed it now but its displaying each row twice

code:

if ($runSql_qry1_1) {<br/>
if (mysqli_num_rows($runSql_qry1_1) &gt; 0) {<br/>
while ($resultDishTitle_DishPrice = mysqli_fetch_assoc($runSql_qry1_1)) {<br/>
foreach ($resultDishTitle_DishPrice as $title =&gt; $price) {<br/>
$title = $resultDishTitle_DishPrice['MenuItemTitle'];<br/>
$price = $resultDishTitle_DishPrice['MenuItemPrice'];<br/>
?&gt;<br/>
&lt;div class="row"&gt;<br/>
&lt;div class="col-sm-10"&gt;<br/>
&lt;?php echo $title; ?&gt;<br/>
&lt;/div&gt;

<i> </i><CODE> &lt;div class="col-sm-2"&gt;
<i> </i> &lt;?php echo $price; ?&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i>
<i> </i> &lt;?php
<i> </i> }


<i> </i>
<i> </i> }
<i> </i> }
<i> </i>
<i> </i> }
Copy linkTweet thisAlerts:
@nsathauthorApr 23.2019 — [upl-image-preview url=https://www.webdeveloper.com/assets/files/2019-04-23/1555987974-48972-untitled1.png]
Copy linkTweet thisAlerts:
@NogDogApr 23.2019 — Not sure why you need/want the foreach loop inside of the while loop? Not sure if that's the problem, though, or if it's at the SQL level. If, before the loop, you do a mysql_fetch_all() and print_r() or var_export() the result, does it have multiple rows like what you're seeing output? I.e., something like
<i>
</i>if ($runSql_qry1_1) {
$data = mysqli_fetch_all($runSql_qry1_1, MYSQLI_ASSOC);
echo("&lt;pre&gt;DEBUG:n".print_r($data, 1)."&lt;/pre&gt;");
// or write to the php log via error_log(), if you prefer
Copy linkTweet thisAlerts:
@NogDogApr 23.2019 — PS: If you wrap your code blocks in ... tags, it will be easier for us to read. (Single back-ticks only work for in-line text, like an HTML <span> tag.
Copy linkTweet thisAlerts:
@nsathauthorApr 23.2019 — @NogDog#1603033 yeh two rows to be precise, the dish title and the price. This is basically a menu card or menu page
Copy linkTweet thisAlerts:
@NogDogApr 24.2019 — Probably because of the way it's joining, I suspect. Maybe the easy solution would be to do a SELECT DISTINCT ...
Copy linkTweet thisAlerts:
@nsathauthorApr 24.2019 — I know you said maybe SELECT DISTINCT, tried too but it yields the same result, twice of each dish title and price
Copy linkTweet thisAlerts:
@nsathauthorApr 24.2019 — also the reason I used the foreach is because in total there are 247 items in the menu, dishes, drinks and desserts, each one should be displayed in its own row
Copy linkTweet thisAlerts:
@NogDogApr 24.2019 — I think it is the foreach, which is looping through each element of the result row, so outputting twice. You should be able to simplify it to:
<i>
</i>if ($runSql_qry1_1) {
if (mysqli_num_rows($runSql_qry1_1) &gt; 0) {
while ($resultDishTitle_DishPrice = mysqli_fetch_assoc($runSql_qry1_1)) { ?&gt;
&lt;div class="row"&gt;
&lt;div class="col-sm-10"&gt;
&lt;?php echo $resultDishTitle_DishPrice['MenuItemTitle']; ?&gt;
&lt;/div&gt;
&lt;div class="col-sm-2"&gt;
&lt;?php echo $resultDishTitle_DishPrice['MenuItemPrice']; ?&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;?php <br/>
}
} <br/>
}
Copy linkTweet thisAlerts:
@nsathauthorApr 24.2019 — Yeh that's what it is, I put in the code you put up and it works a gem. Thank You so much!!! now I just need to just need to find a way of maintaining the grid layout for small screen devices as the column holding the price drops below the column holding the title
Copy linkTweet thisAlerts:
@TehseenApr 24.2019 — https://youtu.be/oa-q-0FCXIs
×

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 4.19,
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,
)...