dongzhuang6247 2013-08-10 22:49
浏览 34
已采纳

PHP - 代码只从Mysql数据库中提供一个值,而它应该提供更多

I have a piece of code which executes a calendar. On this calendar certain names have to appear. However, on each date appears only one name, while in the database some dates hold up to 20 names.

This image should hopefully clarify things: http://imgur.com/a31pj0s

In this image you can see each date, which on some of them one name. As stated before, most of these contain many more names.

Now, I understand why this does not work, as it executes only once, but how can I get it to actually work? Please help me out :)

Here is also a link to the same piece of code, except easier to read: http://codepad.org/tVSzHDTx

<?php
    $j = 2;
    $i = 1;
    //query the database

    $query = mysql_query("SELECT * FROM months");

    //fetch the results / convert results into an array
    echo '<div class="accordion" id="calendar">';
    while($rows = mysql_fetch_array($query)) {
        $month = $rows['month'];
        $id = $rows['id'];

        echo '
        <div class="accordion-group">
            <div class="accordion-heading" id="month_'.$id.'">
                <a class="accordion-toggle" data-toggle="collapse" data-parent="#calendar" href="#monthsid'.$id.'">
                '.$month.'
                </a>
            </div>
            <div id="monthsid'.$id.'" class="accordion-body collapse">
                <div class="accordion-inner">';

        $n = $rows['num_of_days'];
        echo '<ul class="list-group" id="'.$id.'" style="margin-bottom: 0;">';

        for ($i = 1; $i <= $n; $i++) {
            if ($j == 7) {
                echo '<li class="list-group-item" id="'.$i.'" style="margin-bottom: 5px;">';
            } else {
                echo '<li class="list-group-item" id="'.$i.'">';
            }
                echo '<a data-toggle="modal" href="#myModal" class="add_member" id="'.$month.' '.$i.'">+</a>
                    <span class="badge">'.$i.'</span>';

            if ($i == date("j")) {
                echo '<div class="day_segment current_day">';
            } else {
                echo '<div class="day_segment">';
            }

            if ($j == 1) {
                echo '<i style="color: #aaa;">'.$i.' |</i> Monday';
                $j++;
            } else if ($j == 2) {
                echo '<i style="color: #aaa;">'.$i.' |</i> Tuesday';
                $j++;
            } else if ($j == 3) {
                echo '<i style="color: #aaa;">'.$i.' |</i> Wednesday';
                $j++;
            } else if ($j == 4) {
                echo '<i style="color: #aaa;">'.$i.' |</i> Thursday';
                $j++;
            } else if ($j == 5) {
                echo '<i style="color: #aaa;">'.$i.' |</i> Friday';
                $j++;
            } else if ($j == 6) {
                echo '<i style="color: #aaa;">'.$i.' |</i> Saturday';
                $j++;
            } else if ($j == 7) {
                echo '<i style="color: #aaa;">'.$i.' |</i> Sunday';
                $j = 1;
            }
            echo '</div>';


            $posts_query = mysql_query("SELECT * FROM posts WHERE day=$i ");

            while ($rows_items = mysql_fetch_array($posts_query)) {
                $entry_player = $rows_items['author'];
                $entry_comment = $rows_items['comment'];
                $entry_day = $rows_items['day'];
                $entry_month = $rows_items['month'];
            }
            for ($k = 1; $k <= 1; $k++) { /* I tried using another for loop, did not work */
                if ($id == $entry_month && $i == $entry_day) {
                    echo '<span class="label label-success" data-toggle="tooltip" rel="tooltip" title="'.$entry_comment.'">'.$entry_player.'</span>';
                } else {
                    echo '<span class="label"></span>';
                }
            }
            echo '</li>';
        } 

        echo '
                    </ul>
                </div>
            </div>
        </div>

        ';
    } /* End While Loop */
    echo '</div></div>';

?>
  • 写回答

1条回答 默认 最新

  • dtcd27183 2013-08-10 22:59
    关注

    As mentioned in the comments, you should not use the mysql_ functions as they are deprecated and not secure. Use MySQLi or PDO instead.

    To answer your question, the code block below is the reason why it only displays one entry per day. In the while loop, you overwrite all the four variables each time, thus only the last one will be displayed in your for loop. The for loop is not necessary.

    while ($rows_items = mysql_fetch_array($posts_query)) {
        $entry_player = $rows_items['author'];
        $entry_comment = $rows_items['comment'];
        $entry_day = $rows_items['day'];
        $entry_month = $rows_items['month'];
    }
    for ($k = 1; $k <= 1; $k++) { /* I tried using another for loop, did not work */
        if ($id == $entry_month && $i == $entry_day) {
            echo '<span class="label label-success" data-toggle="tooltip" rel="tooltip" title="'.$entry_comment.'">'.$entry_player.'</span>';
        } else {
            echo '<span class="label"></span>';
        }
    }
    

    Try changing your code to something like this:

    while ($rows_items = mysql_fetch_array($posts_query)) {
        $entry_player = $rows_items['author'];
        $entry_comment = $rows_items['comment'];
        $entry_day = $rows_items['day'];
        $entry_month = $rows_items['month'];
    
        if ($id == $entry_month && $i == $entry_day) {
            echo '<span class="label label-success" data-toggle="tooltip" rel="tooltip" title="'.$entry_comment.'">'.$entry_player.'</span>';
        } else {
            echo '<span class="label"></span>';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源