0

Possible Duplicate:
outputting values from database into html table PHP

I am trying to create a table in my .php document which is populated with values from a table in a database. but i cannot get it to work.

Firstly it is not deleting values when there are more than 1 row (There can only ever be 1 item on that particular day)

Secondly if there is no data for a particular day it just puts it into the cell before it, meaning it is on the wrong 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 it gets the data from:

<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 on the end is meant to be on Sunday but is behind because a previous day does not have a item. How would i make that item go to Sunday, while keeping a gap where the other item isn't.

3
  • 1
    What is the point of the if statement ? the output is always the same ? echo '<td>', $info2['title'], '</td>'; Commented Dec 2, 2011 at 15:28
  • if(mysql_num_rows($check) == 1) this must be like if(mysql_num_rows($check) > 0) this doesnot solve the problem offcourse. but cause issue Commented Dec 2, 2011 at 15:52
  • Please don't repost your questions. If you want to add further details, edit the original question instead. Commented Dec 2, 2011 at 15:55

1 Answer 1

1

Use if to check if value is empty or not. Some thing like this.

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

it will fill it properly, without disturbing it layout.

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

I have tried putting that if statement at the front of the others, but the result is still the same.
try it while you are printing it in your table..
oh, where did you put the above if statement? Inside or outside the while loop?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.