PHP - 代码只给出 Mysql 数据库中的一个值,而它应该给出更多值

标签 php mysql

我有一段执行日历的代码。在这个日历上,某些名字必须出现。但是,每个日期仅出现一个名称,而在数据库中,某些日期最多可包含 20 个名称。

这张图片有望澄清一些事情:http://imgur.com/a31pj0s

在此图像中,您可以看到每个日期,其中一些日期有一个名称。如前所述,其中大多数包含更多名称。

现在,我明白为什么这不起作用,因为它只执行一次,但我怎样才能让它真正起作用呢?请帮帮我:)

这里还有同一段代码的链接,只是更容易阅读: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>';

?>

最佳答案

正如评论中提到的,您不应该使用 mysql_ 函数,因为它们已被弃用且不安全。请改用 MySQLi 或 PDO。

为了回答您的问题,下面的代码块是它每天只显示一个条目的原因。在 while 循环中,您每次都会覆盖所有四个变量,因此 for 循环中只会显示最后一个变量。 for 循环不是必需的。

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>';
    }
}

尝试将代码更改为如下所示:

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>';
    }
}

关于PHP - 代码只给出 Mysql 数据库中的一个值,而它应该给出更多值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18167077/

相关文章:

javascript - 使用 json 编码对 MySQL 数组进行分块

javascript - 在 foreach 循环中遍历 HTML DOM 元素

php - Mysql查询 - 增加值大于X但小于Y的值

php - 查询表中的行并排除特定行(如果它们存在于另一个表中)

php - Plupload 浏览按钮在 IE10 中不起作用

MySQL 从列中返回几个最大值

mysql - 复杂sql语句的建议

mysql - 如何从子查询访问先前 select 语句中的列数据?

php - 使用 Laravel 5.2 从 Y-m-d 中存储在数据库中的日期计算年龄

php - Doctrine 记录的深拷贝