donglv6747 2011-12-19 20:49
浏览 31
已采纳

PHP / MySQL显示结果列表

I need some help displaying my results in PHP. Below is what displays for my query:

SELECT service_names.id, service_names.name as service, service_titles.name 
as title,  user_id  FROM `service_names` INNER JOIN service_titles ON title_id 
= service_titles.id

enter image description here

The way the design is, I need to display the results like so:

<div class="list">
    <h3 class="secondary">Maintenance</h3>
        <ul>
            <li>Wiper Blades</li>
            <li>Air Filter</li>
            <li>Cabin Filter</li>
            <li>Fuel Filter</li>
            <li>Cooling System</li>
            <li>Brake System</li>
            <li>Fuel System</li>
            <li>Trans Fluid Exchange</li>
        </ul>
</div>
<div class="list">
    <h3 class="secondary">Tire Services</h3>
       <ul>
            <li>Tire Rotation</li>
            <li>Flat Repair</li>
            <li>Wheel Balance</li>
            <li>Wheel Alignment</li>
        </ul>
</div>

Here is my thread that I posted earlier which explains my database design and shows an image of what it needs to look like. So basically I have the database design in place but now I am not too sure how to create the query to loop through all the results and keep it in that HTML format posted above.

If anyone could help me I would really appreciate it.

Thanks!

  • 写回答

3条回答 默认 最新

  • dongxing1965 2011-12-19 21:03
    关注

    I think something like this ought to do the trick, where you create an array for each 'title' which contains all the 'service' elements under that title... then you just loop through each of the 'title' arrays to generate the bullets:

    $query=mysql_query('',[DB_CONNECTION]);
    if (!$query){
        //do something  
    }
    if (!mysql_num_rows($query)){
        //do something for no results   
    }
    
    $result_array = array();
    
    while($r=mysql_fetch_array($query)){
        if (!isset($result_array[$r['title']])){
            $result_array[$r['title']] = array();
        }
        $result_array[$r['title']][] = $r['service'];
    }
    
    $html = "";
    foreach($result_array as $key => $value){
        $html .= "
        <div class=\"list\">
            <h3 class=\"secondary\">$key</h3>
                <ul>";
            foreach($result_array[$key] as $service){
                $html .= "<li>$service</li>
    "; 
            }
        $html .= "</ul></div>";
    }
    
    echo $html;
    

    This is structured so it will work for as many "title" types as you have. (Obviously you'd need to put in the sql and connection in the mysql_query() at the top.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端