Comdemned Posted March 10, 2008 Share Posted March 10, 2008 The problem I have having is that this is displaying either Timeslot Not available or the link with the date and time for each record in the table. I need it to only display the if the timeslot is available or not once no matter how many records are in the table. At the moment there are 3 rows in the table. if the query is true im getting Timeslot not available Wednesday 12 Mar 2008 3:30 pm test Wednesday 12 Mar 2008 3:30 pm test If the query is fales im getting Wednesday 12 Mar 2008 3:30 pm test Wednesday 12 Mar 2008 3:30 pm test Wednesday 12 Mar 2008 3:30 pm test What I want to get is either Timeslot not available or Wednesday 12 Mar 2008 3:30 pm test to be displayed no matter how many times the loop runs this is my current code. what do i need to change to only echo the output once no matter how many times the loop runs. <?php for ($c=0; $c < $rowcheck; $c++){ $row = mysql_fetch_array($result, MYSQL_BOTH); if ($row['date_time'] == $newdate){ echo "Timeslot not available"; } else { echo " " . date('l d M Y g:i a', $newdate) . "<a href=\"" . $_SERVER['PHP_SELF'] . "?newappdate=$newdate\"> test</a> "; // displays the link in the table with the current date and time. } } ?> Thanks Murray Quote Link to comment Share on other sites More sharing options...
uniflare Posted March 10, 2008 Share Posted March 10, 2008 <?php for ($c=0; $c < $rowcheck; $c++){ $row = mysql_fetch_array($result, MYSQL_BOTH); if ($row['date_time'] == $newdate){ $string = "Timeslot not available"; break; } else { $string = " " . date('l d M Y g:i a', $newdate) . "<a href=\"" . $_SERVER['PHP_SELF'] . "?newappdate=$newdate\"> test</a> "; // displays the link in the table with the current date and time. } } echo($string); ?> try this, because the loop is going through 3 times, your echoing on true and false statements... you will always get 3 results this way, store to a string and break loop if true, then echo string after the loop. Quote Link to comment Share on other sites More sharing options...
bradkenyon Posted March 10, 2008 Share Posted March 10, 2008 try looking up pagination http://php.about.com/od/phpwithmysql/ss/php_pagination.htm Quote Link to comment Share on other sites More sharing options...
Comdemned Posted March 18, 2008 Author Share Posted March 18, 2008 Thanks uniflare Worked a treat. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 18, 2008 Share Posted March 18, 2008 There is probably an easier way to do this. Since you didn't show us your query, I can only guess. Your query probably looks something like: <?php $q = "select `date_time` from yourtable"; ?> If you add a "where" clause onto the query that is the date_time you're looking for, you can do something like: <?php $q = "select `date_time` from yourtable where `date_time` = $newdate"; $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); if (mysql_num_rows($rs) != 0) echo "Timeslot not available"; else echo " " . date('l d M Y g:i a', $newdate) . '<a href="' . $_SERVER['PHP_SELF'] . '?newappdate=$newdate"> test</a> '; // displays the link in the table with the current date and time. ?> Using this code, you don't even need a loop. Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.