Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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"));

    }
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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>';
}
share|improve this answer
    
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. –  Alex Sarnowski Jun 6 '13 at 7:37
    
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. –  Barmar Jun 6 '13 at 7:46
    
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. –  Alex Sarnowski Jun 6 '13 at 7:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.