2011-12-02 58 views
1

我有下面的代碼根據用戶選擇的星期從表中獲取數據,但只有2個可能的星期可供選擇。將數據庫中的值輸出到html表中PHP

它將配方的標題放入基於if語句的相關表中,但似乎產生了奇怪的輸出。

當我試圖查看僅填充了零件數據的一週時,將數據串集中到錯誤的單元格上;看看這個圖片: Table problem 「沙拉」位於最上面一行應在「週日

這隻有在表是不完整的情況。

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'); 
         } 

回答

1

做一個彷彿看到了聲明,如果

<td>& nbsp;</td> 

所以正確填寫您的表中的值存在,如果沒有的話。

0

你也可以在運行查詢之前聲明變量爲空,例如:

if(!empty($_POST['selectweek'])) { 
    $info1 = ''; 
    $info2 = ''; 
    $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'); 
} 
相關問題