Jump to content

Single result from Loop


Comdemned

Recommended Posts

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.