I have been trying to solve this problem and have find a lot of solutions but still can't get to work. I am so frustrated with the codes and I think I will just do it step by step then instead of lumping it altogether and get it work at once.
Right now my tables are:
categories | name
fashion | chanel
fashion | prada
cafes | starbucks
cafes | tcc
dining | kfc
dining | macdonalds
What I want to achieve is
1) Echo-ing out the categories <h1>fashion</h1> with once instead of duplicating and <ul> if it is starting on a new category
2) Echo-ing out all the <li>name</li> no matter how many of names are under that fashion
3) Echo-ing out </ul> if it is the last name in that fashion
And so on for the rest of the categories. This is currently what my mind is thinking of and I do not know whether is this practical or not.
I have tried using this way:
$result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='$level' ORDER BY categories");
while($row = mysqli_fetch_array($result)){
if($previousVal != $row['categories']){
$data .= '<h1>'.$row['categories'].'</h1><ul class="shop_listing">';
$previousVal = $row['categories'];
}
$data .= '<li>
<p class="float_left">'.$row['name'].'</p>
</li></ul>';
}
Which I know there is something wrong in the looping of the li with the closing ul. This is where I am stuck for very long time and unable to find any solution anywhere. No matter what I do, foreach for different categories, if else, I still can't get anywhere near.
The result I get is either:
<h1>fashion</h1>
<ul class="shop_listing">
<li><p>chanel</p></li>
</ul>
<li><p>prada</p></li>
<h1>dining</h1>
<ul class="shop_listing">
<li><p>kfc</p></li>
</ul>
<li><p>macdonald</p></li>
Or:
<h1>fashion</h1>
<ul class="shop_listing">
<li><p>chanel</p></li>
<li><p>prada</p></li>
<h1>dining</h1>
<ul class="shop_listing">
<li><p>kfc</p></li>
<li><p>macdonald</p></li>
</ul>
</ul>
Hope you guys can help me out please. For those who just put links and commented read more on that, don't input your answers here. I don't need that, I am already very confused and I am just a beginner in this. I am glad if someone is willing to guide or even explain to me why my code doesn't work and why the solutions given will work. Thanks in advance!