douchao0358 2013-11-07 08:31
浏览 71

使用html标记在while循环中使用多个数据显示一次类别

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!

  • 写回答

2条回答 默认 最新

  • duanju9104 2013-11-07 08:46
    关注

    One way to do it is create a new array using your category as a key and put all names belonging to that category into an array within that. Loop through the new array and create your 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>';
    }
    

    Not tested, but it should do what you need.

    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线