PHP从数据库创建HTML表 [英] PHP create HTML table from database
问题描述
可能重复:
将数据库中的值输出到html表PHP
Possible Duplicate:
outputting values from database into html table PHP
我正在尝试在.php文档中创建一个表,该表中填充了数据库中表中的值.但我无法使它正常工作.
I am trying to create a table in my .php document which is populated with values from a table in a database. but i cannot get it to work.
首先,它不会删除多于1行的值(在该特定日期只能有1个项目)
Firstly it is not deleting values when there are more than 1 row (There can only ever be 1 item on that particular day)
第二天,如果某天没有数据,则将其放在其之前的单元格中,这意味着该日期是错误的一天.
Secondly if there is no data for a particular day it just puts it into the cell before it, meaning it is on the wrong day.
这是代码:
<?php
if(!empty($_POST['recipe'])) {
$week = mysql_real_escape_string($_POST['week']);
$day = mysql_real_escape_string($_POST['day']);
$mealtime = mysql_real_escape_string($_POST['mealtime']);
$recipe = mysql_real_escape_string($_POST['recipe']);
$check = mysql_query("SELECT * FROM menu WHERE dayid = '".$day."' AND mealtimeid = '".$mealtime."'");
if(mysql_num_rows($check) == 1) {
mysql_query("DELETE FROM menu WHERE mealtimeid = '".$mealtime."' AND dayid = '".$day."'");
$success = mysql_query("INSERT INTO menu (weekid, dayid, mealtimeid, recipeid)
VALUES('".$week."', '".$day."', '".$mealtime."', '".$recipe."')");
if($success) {
echo "<h1>Success</h1>";
echo "<p>Your recipe was successfully added.</p>";
}
else {
echo "<h1>Error</h1>";
echo "<p>Sorry there was a problem, please try again.</p>";
}
}
else {
$success = mysql_query("INSERT INTO menu (weekid, dayid, mealtimeid, recipeid) VALUES('".$week."', '".$day."', '".$mealtime."', '".$recipe."')");
if($success) {
echo "<h1>Success</h1>";
echo "<p>Your recipe was successfully added.</p>";
}
else {
echo "<h1>Error</h1>";
echo "<p>Sorry there was a problem, please try again.</p>";
}
}
}
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(empty($info2['dayid'])) {
echo '<td></td>';
}
elseif($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');
}
}
else {
?>
这是它从以下位置获取数据的形式:
This is the form it gets the data from:
<form method="post"
action="">
<fieldset>
<label for="week">Select Week:</label> <select name="week">
<option value="0">
Select Week<?php echo $item; ?>
</option>
</select> <label for="day">Select Day:</label> <select name=
"day">
<option value="0">
Select Day<?php echo $item2; ?>
</option>
</select><br />
<br />
<label for="mealtime">Select Meal Time:</label> <select name=
"mealtime">
<option value="0">
Select Meal Time<?php echo $item3; ?>
</option>
</select><br />
<br />
<label for="recipe">Select Recipe:</label> <select name="recipe">
<option value="0">
Select Recipe<?php echo $item4; ?>
</option>
</select> <input type="submit"
id="login-submit"
value="Add to Menu" />
</fieldset>
</form>
<form method="post"
action="">
<label for="selectweek">Select Week:</label> <select name=
"selectweek">
<option value="0">
Select Week<?php echo $item; ?>
</option>
</select> <input type="submit"
id="login-submit"
value="View Menu" />
</form>
-末尾的项目是在星期日,但由于前一天而落后没有项目.我将如何使该商品进入星期日,同时在其他商品与其他商品之间保持间隔.
-- The item on the end is meant to be on Sunday but is behind because a previous day does not have a item. How would i make that item go to Sunday, while keeping a gap where the other item isn't.
推荐答案
使用是否检查值是否为空.像这样的东西.
Use if to check if value is empty or not. Some thing like this.
if($info2['dayid'] == "") {
echo '<td> </td>';
}
它将正确填充它,而不会干扰其布局.
it will fill it properly, without disturbing it layout.
希望这会有所帮助.
这篇关于PHP从数据库创建HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!