outputting values from database into html table PHP

前端 未结 2 1571
伪装坚强ぢ
伪装坚强ぢ 2020-12-22 01:06

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.

It puts the title of the

相关标签:
2条回答
  • 2020-12-22 02:03

    Do an if statement to see if a value exists if not then

       <td>& nbsp;</td>
    

    so it fills your table correctly.

    0 讨论(0)
  • 2020-12-22 02:06

    You could also declare the variables as empty before the query is run, example:

    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');
    }
    
    0 讨论(0)
提交回复
热议问题