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 Link to comment https://forums.phpfreaks.com/topic/95372-single-result-from-loop/ 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. Link to comment https://forums.phpfreaks.com/topic/95372-single-result-from-loop/#findComment-488479 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 Link to comment https://forums.phpfreaks.com/topic/95372-single-result-from-loop/#findComment-488487 Share on other sites More sharing options...
Comdemned Posted March 18, 2008 Author Share Posted March 18, 2008 Thanks uniflare Worked a treat. Link to comment https://forums.phpfreaks.com/topic/95372-single-result-from-loop/#findComment-494702 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 Link to comment https://forums.phpfreaks.com/topic/95372-single-result-from-loop/#findComment-494725 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.