0

and thankyou for reading my question. Using a for loop in PHP, I was looking to accomplish an echo for a list of times (offset by 5 minutes) in between an open and closing time of a business. That challenge was not a problem to program, my code outputs a huge list of <option> tags with time values inside each of the (offset by 5 minutes) succesfully:

I.E:

<option value="2013-06-06 08:00:00">8:00 am</option>
<option value="2013-06-06 08:05:00">8:05 am</option>
<option value="2013-06-06 08:10:00">8:10 am</option>

.....etc.

However, I have a MYSQL table setup that has a column inside of each row (setup in DATETIME format), that is the time of tee off....as many may know you can't have more than one person booked to tee off at once so I am trying to make it where, if that time (inside that for loop) is equal to any of the (booked) times fetched from database, that value will not echo in that list.

Below is my code, (the for loop at least) this is what is outputing the <option> tags: (Also as you can see I attempted to make an IF statement there that says if the $time equals $tee_times_from_mysql don't echo that <option>...then continue to check the next, and so on.) This worked, however, when the $tee_times_from_mysql was SELECT-ed from my MYSQL database, it only took the most recently booked tee time (only one) and excluded that....the rest got ignored (or simply were never fetched, not sure)....so it doesn't work if there is more than one booked tee time (which there is obviously going to be more than that)....my question is how do I exclude a time if they equal any of times in the database.

for ($i=$startHour; $i <= $endHour; $i++)
{
    for ($j = 0; $j <= 55; $j+=5)
    {
        $time_pre = $i.":".str_pad($j, 2, '0', STR_PAD_LEFT);
        $time = ''.$time_pre .' '.$day.'';

        /////////////////////////////

        $time_formated_for_cosmetic = date("g:i a", strtotime("$time"));
        $time_formated_for_mysql = date("Y-m-d H:i:s", strtotime("$time"));

         if ($time == $tee_times_from_mysql){
            $time = '';
        }
         else{
            $time = '<option value="' . $time_formated_for_mysql . '">'.$time_formated_for_cosmetic .'</option>';
        }
        echo $time;
    }
}

THIS IS MY MYSQL FETCH ALSO:

$tee_times_from_mysql = "";

$sql = "SELECT * FROM tee_times";
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_assoc($query)){
    $tee_times_from_mysql = $row["date_time"];
    $tee_times_from_mysql =  date("G:i n/j/y", strtotime("$tee_times_from_mysql"));

    }

1 Answer 1

1

Your MySQL fetch loop is only setting $tee_times_from_mysql to the last time returned by the query, not all of them. You need to use an array. This code creates an array whose keys are all the times:

$tee_times_from_mysql = array();

$sql = "SELECT * FROM tee_times";
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_assoc($query)){
    $tee_time = date("G:i n/j/y", strtotime($row["date_time"]));
    $tee_times_from_mysql[$tee_time] = true;
}

Then the code to create the options should look like:

if (isset($tee_times_from_mysql[$time])) {
    $time = '';
} else {
    $time = '<option value="' . $time_formated_for_mysql . '">'.$time_formated_for_cosmetic .'</option>';
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is causing all the options (before the latest timed one) to be completely gone....so for example my latest booked tee time for today is 2:15pm...it shows all the <option> tags at (and after) 2:20pm and nothing before.
I don't see how that could be. My code only compares the times from the loop with the times saved in the array. There's no less-than comparison, they have to be exact matches. I think you must have merged my code into your loop incorrectly.
You are indeed correct sir. Me, being the careless person I am, combined your code with a previous version of my non-working code without noticing. Everything works like a charm, thank-you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.