首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP fetch_row -对多个记录进行迭代

PHP fetch_row -对多个记录进行迭代
EN

Stack Overflow用户
提问于 2015-02-17 12:53:55
回答 1查看 222关注 0票数 3

我已经搞了三天了,研究和实验:是时候寻求帮助了。

我有一个时间表显示页面的一些代码,它遍历一个数据库表,每次约会类型一次。它收集每周的所有此类约会,并连续返回它们,如下所示:

代码语言:javascript
运行
复制
+------+------+-----+-----+-----+-----+-----+-----+-----+------+
| Name | Year | Mon | Tue | Wed | Thu | Fri | Sat | Sun | Purp |
+------+------+-----+-----+-----+-----+-----+-----+-----+------+
| IST  |  NA  | 9-4 |     |     |     |     |     |     | ABC  |
+------+------+-----+-----+-----+-----+-----+-----+-----+------+
| OT   |  NA  |     | 8-2 | 8-2 | 8-2 | 8-2 |     |     | DEF  |
+------+------+-----+-----+-----+-----+-----+-----+-----+------+

我的代码完全按照我的要求工作,有一个致命的缺陷。如果一个“名称”有多行,我就把它作为我的行:

代码语言:javascript
运行
复制
+------+------+-----+-----+-----+-----+-----+-----+-----+------+
| Name | Year | Mon | Tue | Wed | Thu | Fri | Sat | Sun | Purp |
+------+------+-----+-----+-----+-----+-----+-----+-----+------+
| IST  |  NA  | 9-4 |     |     |     |     |     |     | ABC  |
+------+------+-----+-----+-----+-----+-----+-----+-----+------+
| OT   |  NA  |     | 8-2 | 8-2 | 8-2 | 8-2 |     |     | DEF  |
+------+------+-----+-----+-----+-----+-----+-----+-----+------+-+-----+-+-+-+-+-+-----+
| MTG  |      | 1-2 |     |     |     |     |     |     |      | | 1-2 | | | | | | GHI |
|      |      |     |     |     |     |     |     |     |      | |     | | | | | | JKL |
+------+------+-----+-----+-----+-----+-----+-----+-----+------+-+-----+-+-+-+-+-+-----+

因此,它不是插入到适当的,它是一个全新的集合。这真的很令人沮丧,因为我确信这是很简单的事情,但我看不见.:(

代码:

代码语言:javascript
运行
复制
$apptnamestop = array("IST", "OT", "MTG", "TR-CN", "EVENT", "EN", "REC", "TO");
$daysofweek = array("1", "2", "3", "4", "5", "6", "0");
foreach ( $apptnamestop as $name) {
    print str_repeat($tab, 8) . "<tr>\n";
    print str_repeat($tab, 9) . "<td class=\"td1s\">$name</td>\n";
    print str_repeat($tab, 9) . "<td class=\"td1s\">N/A</td>\n";
    $getdataquery = "SELECT appointmentName as name
, appointmentType as type, appointmentStartDateTime as sdt
, appointmentEndDateTime as edt, appointmentPurpose as purp 
from tblAppointments 
where appointmentStaffIDsToAttend like '%{$_SESSION['user_id']}%' 
and appointmentName = '$name' 
and (appointmentStartDateTime >= DATE_ADD(CURDATE(), INTERVAL (9 - IF(DAYOFWEEK(CURDATE())=1, 8, DAYOFWEEK(CURDATE()))) DAY) 
and appointmentEndDateTime < DATE_ADD(CURDATE(), INTERVAL (16 - IF(DAYOFWEEK(CURDATE())=1, 8, DAYOFWEEK(CURDATE()))) DAY))
";
    $getdataqueryresults = $mysqli->query($getdataquery)  
       or trigger_error("<p class=\"error\">We're very sorry, but an error has occurred when interacting with the CHAIRS database.  Please try again and see if the error repeats.  If it does, please get the following information in its entirety to your database adminapptrator so the CHAIRS developer can get the error resolved.<br />Error Message: " . $mysqli->error, E_USER_ERROR);
    $datarowcnt = $getdataqueryresults->num_rows;
    if ($datarowcnt > 0) {
        while ($row = $getdataqueryresults->fetch_row()) {
            $rows[] = $row;
        }

        foreach ($rows as $row) {
            $title = $row[0];
            $type = $row[1];
            $sdt = $row[2];
            $edt = $row[3];
            $purp = $row[4];

            $c=7;

            if ($type == 1) {
                $typew = "Mandatory";
            } else {
                $typew = "Elective";
            }

            $sparts = explode(" ", $sdt);
            $eparts = explode(" ", $edt);
            $tdiff = getTimeDiff($sparts[1], $eparts[1]);

            foreach ($daysofweek as $day) {
                if ($title == $name) {
                    if ($day == date('w', strtotime("$sparts[0]"))) {
                        if ($sparts[0] == $eparts[0]) {
                            print str_repeat($tab, 9) . "<td class=\"td1s\">$sparts[1] - $eparts[1]<br />($tdiff) - $typew</td>\n";
                            $c--;
                        } else {
                            $s = strtotime("$sparts[0]");
                            $e = strtotime("$eparts[0]");

                            for ($i=$s; $i<=$e; $i+=86400) {
                                print str_repeat($tab, 9) . "<td class=\"td1s\">$sparts[1] - $eparts[1]<br />($tdiff) - $typew</td>\n";
                                $c--;
                            }
                        }
                    } 
                    if ( $c > 0) {
                            $c--;
                            print str_repeat($tab, 9) . "<td class=\"td1s\"></td>\n";
                    }
                }
            }
        }
        $rc++;
    } else {
        foreach ($daysofweek as $day) {
            print str_repeat($tab, 9) . "<td class=\"td1s\"></td>\n";
        }
    }
    print str_repeat($tab, 9) . "<td class=\"td1s\">$purp</td>\n";
    $purp = "";
    print "</tr>\n";
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-17 13:19:39

新行在这里启动:

代码语言:javascript
运行
复制
foreach ( $apptnamestop as $name) {
    print str_repeat($tab, 8) . "<tr>\n";
    print str_repeat($tab, 9) . "<td class=\"td1s\">$name</td>\n";

如您所见,这个新行在$apptnamestop中每个元素启动一次,而不是从数据库中的每一行启动一次。代码并不关心它在表中找到了多少匹配行,它只会在每个名称中插入一个新的tr

根据您的需要,有几种方法可以改善这种情况。

如果您需要始终在每个名称中插入一行,无论数据库中是否有匹配的行,请保持第4、5和6行与现在相同。我们需要修改内环(foreach ($rows as $row)),以便为第一个循环以外的每个循环输出新的表行:

代码语言:javascript
运行
复制
$rowCounter = 0;
foreach ($rows as $row) {
    if ($rowCounter > 0) {
        print "</tr>\n"; // Close the previous table row
        print str_repeat($tab, 8) . "<tr>\n";
        print str_repeat($tab, 9) . "<td class=\"td1s\">$name</td>\n";
        print str_repeat($tab, 9) . "<td class=\"td1s\">N/A</td>\n";
    }
    $rowCounter++;
    // Remainder of the loop code goes here
    ...
}

如果在找不到匹配的数据库行时,希望跳过表行的创建,请将行创建语句完全移动到内环中;这样,只有在找到数据库行时才能运行。

最后,请考虑实现视图模板系统;通过将逻辑代码与显示代码分离,您可以轻松地修改其中一个而不破坏另一个。您的代码在将来变得更容易阅读和修改。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28562002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档