PHP从数据库创建HTML表

时间:2011-12-02 15:20:27

标签: php sql phpmyadmin

  

可能重复:
  outputting values from database into html table PHP

我正在尝试在.php文档中创建一个表,该表填充了数据库中表的值。但我无法让它发挥作用。

首先,当行数超过1行时,不会删除值(在该特定日期只能有1个项目)

其次,如果某一天没有数据,只需将其放入其前的单元格中,这意味着它在错误的一天。

以下是代码:

<?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 {
?>

这是从以下数据获取数据的形式:

<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>

Example - 最后的项目是在星期日,但是因为前一天没有项目而落后。我如何将该项目转到星期日,同时保持其他项目不存在的差距。

1 个答案:

答案 0 :(得分:1)

使用if检查值是否为空。有点像这样。

if($info2['dayid'] == "") {
      echo '<td>&nbsp;</td>';
   }

它会正确填充它,而不会打扰它的布局。

希望这有帮助。