PHP creates an HTML table from a database

Possible duplicate:
output values ​​from the database to the html table PHP

I am trying to create a table in my .php document that is populated with the values ​​from the table in the database. but I can't get it to work.

Firstly, it does not delete values ​​when there is more than one row (there can only be one element on this particular day)

Secondly, if there is no data for a certain day, he simply puts them in the cell before that, that is, on the same day.

Here is the code:

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

This is the form from which it receives data:

 <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 - The item at the end should be on Sunday, but lags because the previous day does not have an element. How can I do this item on Sunday, keeping a space where the other item is not.

0
source share
1 answer

Use if to check if a value is empty or not. Something like that.

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

he will fill it correctly without breaking its layout.

Hope this helps.

+1
source

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


All Articles