0

I'm trying to pull data from a database to show reservations in a table that looks like a weekly calendar. The reserved hours should take the "reserve" class. For some reason, it looks like my foreach loops aren't executing. The way the code is written now, the table should just read "test" for each data entry.

$schedule = mysql_query ("SELECT * FROM reservation WHERE location_id = '$reserve' AND year = '$year' AND quarter = '$quarter' AND date_note = 'Quarter' ORDER BY hour_start ASC");

foreach ($time as $hour) {
    $endhour = $hour + 30;
    foreach ($week as $day) {
        while ($sched_table = mysql_fetch_array($schedule)) {
            if ($sched_table['day_of_week'] == $day) {
                if ($sched_table['hour_start'] == "") {
                    echo "<td>test</td>";
                } elseif ($sched_table['hour_start'] >= $hour && $sched_table['hour_start'] < $endhour) {
                    echo "<td class='reserve'>test</td>";
                } else {
                    echo "<td>test</td>";
                }
            }
        }
    }
}

All of the variables above are defined earlier as such:

$reserve = $_POST['reserve'];
$year = 2013;
$quarter = "Fall";

$week = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
$time = array();
for ($x = 800; $x < 2001; $x += 100) {
    $y = $x + 30;
    array_push($time, $x, $y);
}

Some quick notes--I realize that the haven't been set before the foreach loops. I wanted to make sure the loops were running before I figured out where to start and end each row, so I just put a echo ""; and echo ""; at the beginning and the end of the entire statement. This should have made one long list of "test", but nothing is showing.

The database stores the start times as "0900" instead of "900". Would that explain why the loops aren't executing? And if that's the case, would I have to redefine $time so they exist as strings, or would I be able to work with them as a value.

Thanks for the help.

Joseph Oh
  • 15
  • 4
  • 2
    *PSA:* The `mysql_*` functions are [deprecated in PHP 5.5](http://php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated). It is not recommended for writing new code as it will prevent you from upgrading in the future. Instead, use either [MySQLi](http://php.net/manual/en/book.mysqli.php) or [PDO](http://php.net/manual/en/book.pdo.php) and [be a better PHP Developer](http://jason.pureconcepts.net/2012/08/better-php-developer/). – Jason McCreary Sep 10 '13 at 18:15
  • 1
    I get a Nickel every time someone is told not to use mysql_... I'm getting so rich! – Rottingham Sep 10 '13 at 18:20
  • does print_r($time); show you a good looking array?
    if you do echo ":" . $hour . ":"; right after your first foreach, what do you get? do you get :800: or ::?
    – bart2puck Sep 10 '13 at 18:21
  • I wish I got a nickel every time I've said it. – Jason McCreary Sep 10 '13 at 18:21
  • **By building SQL statements with outside variables, you are leaving yourself wide open to SQL injection attacks.** Also, any input data with single quotes in it, like a name of "O'Malley", will blow up your SQL query. Please learn about using parametrized queries, preferably with the PDO module, to protect your web app. http://bobby-tables.com/php has examples to get you started, and [this question](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has many examples in detail. – Andy Lester Sep 10 '13 at 19:48

2 Answers2

0

Things you should test:

  • Ensure $time is filled BEFORE entering the first foreach (add <?echo count($time);?> before that foreach)
  • Ensure $week is filled BEFORE entering the second foreach (add <?echo count($week);?> before that foreach)
  • Ensure your query is returning anything. (Print out the generated query string, and execute it with some database software like phpmyadmin.)

If you have performed this 3 steps, you have found the problem, and can solve it.

dognose
  • 20,360
  • 9
  • 61
  • 107
0

After the first time through the inner foreach loop mysql_fetch_array($schedule) will always return false. If the database doesn't have any schedules for 8:00 Sunday then it will look like none of the loops ran.

Try resetting the result pointer with mysql_data_seek($schedule, 0) (mysql_data_seek Documentation)

Jeff Horton
  • 904
  • 12
  • 18
  • I wish I could upvote you, but I don't have enough of a reputation. I moved the while loop to the front and put the foreach inside, and it's working perfectly. – Joseph Oh Sep 10 '13 at 19:16