donglangtun1850 2013-10-06 17:22
浏览 10
已采纳

(PHP)基于mysql值分类为html opgroup

Basically I have a database with 66 bible books; some from old testament some from new. The bname value is the NAME of the book, while bsect has a value of O or N(new or old), how can I make my dropdown box dynamically display a book into an old or new optgroup based on whether its' bsect is O or N? My teacher said I have to make some array, but i have no idea how to do it. Any thoughts?

My database sample:

+-----------+-------+
| bname     | bsect |
+-----------+-------+
| Genesis   | O     |
| Exodus    | O     |
| Leviticus | O     |
+-----------+-------+

I don't want to have to rely on manually setting opgroups based on the NUMBER OF THE ENTRY, I want it to be dynamic based on value of bsect.

Right now I just have the following query with a select dropdown which puts the book into old or new based on its record number, but It will break if more books were to be added

    $query = $mysqli->query("select distinct bname as Name from kjv");



?>

<select name="book">
    <?php
    $i=1;
    while($option = $query->fetch_object()){ 
        if($i==1) echo "<optgroup label='Old Testament'>";
        else if($i==40) echo "<optgroup label='New Testament'>";
        echo "<option value='$i'>".$option->Name."</option>";
            $i++;   

    } 

    ?>
  • 写回答

2条回答 默认 最新

  • drnzpd579935 2013-10-06 17:31
    关注

    Simply order by bsect and display different optgroups dynamically

    <?php
    $query = $mysqli->query("SELECT DISTINCT bsect, bname AS Name FROM kjv ORDER BY bsect");    
    ?>
    
    <select name="book">
    <?php
        $i = 1;
        $bsect = "";
        while($option = $query->fetch_object()){ 
            if($bsect != $option->bsect) {
                $bsect = $option->bsect;
                echo "<optgroup label='{$bsect}'>";
            }
            else if($i==40) echo "<optgroup label='New Testament'>";
            echo "<option value='$i'>".$option->Name."</option>";
                $i++;   
    
        } 
    ?>
    

    Of course, then your books may be out of order. So what you would want to do is add a book-order column (border) that stores a number defining how to order the books in a given group, e.g.

    ALTER TABLE kjy 
        ADD COLUMN border INT U?NSIGNED NOT NULL DEFAULT 0;
    

    Then you can update the data to have the proper book order and do a query like this:

    SELECT DISTINCT bsect, bname AS Name FROM kjv ORDER BY bsect, border;
    

    Of course, this being the Bible, you aren't going to be adding books, so you can probably just define a static Book ID that defines the ordinality of each book. Then you could just sort by ID and know that "Old" and "New" books are coming out in the right order.

    ALTER TABLE kjy 
            ADD COLUMN id INT UNSIGNED NOT NULL PRIMARY KEY BEFORE (bname);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度