Output values ​​from database to PHP html table

I have the code below that captures data from a table depending on which week the user selected, there are only 2 possible weeks.

It puts the recipe name in the appropriate table based on the if statement, but it seems to produce weird output.

When I try to look at a week that contains only partial data, filled in it with data bundles on the wrong cells; look at this image: Table problem The lettuce in the top row should be on 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'); } 
+1
source share
2 answers

Make an if statement to see if a value exists, if not then

  <td>& nbsp;</td> 

so that it correctly populates the table.

+1
source

You can also declare variables empty before starting the query, for 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
source

Source: https://habr.com/ru/post/922782/


All Articles