请帮帮我,需要从mysql查询循环

时间:2012-10-31 17:54:28

标签: php loops while-loop

我有一个包含此信息的表格:

id ctype weeknum  daynum   posting
1  2     2        3        15.55
2  2     4        5        60.50
3  2     15       1        10.00
4  2     17       2        100.55

我希望使用此循环在正确的行和列上使用“发布”。

for($row=1;$row<=52;$row++) // 52 weeks in a year
{
for($col=1;$col<=7;$col){                // 7 days in a week

$query=mysql_query("select * from table where ctype=2");
$weekday=mysql_fetch_array($query);

if($row==$weekday['weeknum'] && $col==$weekday['daynum']){

echo "<input type='text' value'".$posting['posting']."' />";
} else {

echo "<input type='text' />";     // will display a blank textbox

}
}
}
这样的事情就是我想要实现的目标。 http://tvends.com/sample/sample.jpg

1 个答案:

答案 0 :(得分:0)

它应该是这样的

$mysql = new mysqli("localhost", "root", "", "test");
$result = $mysql->query("SELECT * FROM stack  WHERE ctype = 2");

$map = array();

while ( $row = $result->fetch_assoc() ) {
    $map[$row['weeknum']][$row['daynum']] = $row['posting'];
}

print("<table><thead><tr>");
printf("<td>Week</td>");
for($i = 1; $i <= 7; $i ++) {
    printf("<td>%s</td>", "Day $i");
}
print("</tr></thead>");

print("<tbody>");

for($w = 1; $w <= 52; $w ++) {
    print("<tr>");
    printf("<td>%d</td>", $w);
    for($d = 1; $d <= 7; $d ++) {
        $key = "day_{$w}_{$d}";
        if (isset($map[$w][$d])) {
            printf("<td><input type='text' value='%s' name='%s' /></td>", $map[$w][$d], $key);
        } else {
            printf("<td><input type='text' name='%s' /></td>", $key);
        }
    }
    print("</tr>");
}

print("</tbody></table>");