将数据库中的值输出到html表PHP中 [英] outputting values from database into html table PHP
问题描述
我有下面的代码,该代码根据用户选择的星期从表中获取数据,只有2个星期可供选择.
I have the code below which grabs data from a table based on what week the user has chosen, there are only 2 possible weeks to choose from.
它根据if语句将配方的标题放入相关表中,但似乎产生了奇怪的输出.
It puts the title of the recipe into the relevant table based on the if statement but seems to be producing weird output.
每当我尝试查看仅填充部分数据的一周时,将一堆数据打包到错误的单元格中;看这张图: 位于第一行的沙拉"应位于星期日"
When ever i try to view a week which only has part data filled in it bunches of the data onto the wrong cells; look in this image: 'Salad' located on the top row should be in 'Sunday'
这仅在表不完整时发生.
This only happens when the table is incomplete.
if(!empty($_POST['selectweek'])) {
$selectweek = mysql_real_escape_string($_POST['selectweek']);
function ouptutMeal($selectweek, $mealtime, $mealname) {
$sqlmeasurement2 = mysql_query("SELECT title, dayid
FROM recipe
JOIN menu ON recipe.recipeid = menu.recipeid
WHERE menu.weekid = '$selectweek'
AND menu.mealtimeid = '$mealtime'
ORDER BY dayid");
echo "<br/>
<table>
<td></td>
<td><strong>Monday</strong></td>
<td><strong>Tuesday</strong></td>
<td><strong>Wednesday</strong></td>
<td><strong>Thursday</strong></td>
<td><strong>Friday</strong></td>
<td><strong>Saturday</strong></td>
<td><strong>Sunday</strong></td>
<tr>
<td><strong>$mealname</strong></td>";
while($info2 = mysql_fetch_array( $sqlmeasurement2 )) {
if($info2['dayid'] == '1') {
echo '
<td>', $info2['title'], '</td>';
}
elseif($info2['dayid'] == '2') {
echo '
<td>', $info2['title'], '</td>';
}
elseif($info2['dayid'] == '3') {
echo '
<td>', $info2['title'], '</td>';
}
elseif($info2['dayid'] == '4') {
echo '
<td>', $info2['title'], '</td>';
}
elseif($info2['dayid'] == '5') {
echo '
<td>', $info2['title'], '</td>';
}
elseif($info2['dayid'] == '6') {
echo '
<td>', $info2['title'], '</td>';
}
else {
echo '
<td>', $info2['title'], '</td>';
}
}
echo '</tr>
</table>';
}
ouptutMeal($selectweek, 1, 'Breakfast');
ouptutMeal($selectweek, 2, 'Lunch');
ouptutMeal($selectweek, 3, 'Evening Meal');
ouptutMeal($selectweek, 4, 'Pudding');
ouptutMeal($selectweek, 5, 'Supper & Snacks');
}
推荐答案
执行if语句以查看是否存在值
Do an if statement to see if a value exists if not then
<td>& nbsp;</td>
所以它可以正确地填满你的桌子.
so it fills your table correctly.
这篇关于将数据库中的值输出到html表PHP中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!