0

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!

4

2 回答 2

2

一种方法是使用 yourcategory作为键创建一个新数组,并将属于该类别的所有名称放入其中的一个数组中。循环遍历新数组并创建您的 HTML。

PHP

$temp_array=array();//temporary array

$result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='$level' ORDER BY categories");

while($row = mysqli_fetch_array($result)){
    $temp_array[$row['categories']][]=$row['name'];//put data into temporary array
}

foreach($temp_array AS $category=>$names){
    $data.='<h1>'.$category.'</h1><ul class="shop_listing">';
    foreach($names AS $name){
        $data.='<li>
               <p class="float_left">'.$name.'</p>
               </li>';
    }
    $data.='<ul>';
}

未经测试,但它应该可以满足您的需求。

于 2013-11-07T08:46:28.893 回答
1

尝试这个

$result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='$level' ORDER BY 

    categories");
    while($row = mysqli_fetch_array($result)){
                    if($previousVal != $row['categories']){
                         if($data) {
                               $data .= "</ul>";
                         }
                         $data .= '<h1>'.$row['categories'].'</h1><ul class="shop_listing">';
                         $previousVal = $row['categories'];
                    }
                     $data .= '<li>


                     <p class="float_left">'.$row['name'].'</p>
                                   </li>';
                    }
$data .= "</ul>";
于 2013-11-07T08:50:10.913 回答