Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
855 views
in Technique[技术] by (71.8m points)

sql - outputting values from database into html table PHP

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 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: Table problem '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');
                        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

   <td>& nbsp;</td>

so it fills your table correctly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...