Jump to content

how to remove second while loop to avoid double data


dragon_sa

Recommended Posts

I have a query that gets staff on a particular day then performs a while loop selecting each member that works on that day and the times they work between.

I have a for loop in that while loop that lists apointment times for each staff member every 15mins between the times they work, this works fine.

I then have in the for loop a while loop that checks for existing appontments in the appointment table and changes the appointment slot for times that are booked to booked.

I have found though that the second while loops causes doubling of results when an appointment is found, so I need to work out how to restructure this code so that i dont get the doubling effect of result.

 

you can see the result of the script here

 

http://justrelax.net.au/datetest.php

 

and the code that causes the problem is

 

// convert to US format
$input="$month/$day/$year";
// readable date
$readDATE="$day/$month/$year";
// convert for mysql date
$sqlDATE="$year-$month-$day";
// get day of the week
$weekday=date(l, strtotime($input));
echo $weekday."<br/>";
// get working staff from staff table
$staffSQL=mysql_query("SELECT * FROM staff WHERE ".$weekday." != 'off' AND onLEAVE='no' AND ACTIVE='yes' ORDER BY staffID ASC");
// return results
putenv("TZ=Australia/Adelaide");
while ($staff=mysql_fetch_array($staffSQL)) {
	$name=$staff['staffNAME'];
	$avail=$staff[$weekday];
	$staffID=$staff['staffID'];
	list($first, $last)=explode(" ", $name);
	list($in, $out)=explode("-", $avail);
	$firstTIME="$sqlDATE $in:00";
	$lastTIME="$sqlDATE $out:00";
	echo "$first is available between $avail on $weekday<br/>";
	$in2=strtotime($firstTIME);
	$out2=strtotime($lastTIME);
	$stop=$out2-900;
		echo $in2." - time start - $firstTIME<br/>"; 
		echo $out2." - time finish - $lastTIME<br/>";
	for ($inc=$in2;$inc<=$stop;$inc+=900) {
		$existSQL=mysql_query("SELECT * FROM appointment WHERE date='$sqlDATE' AND staffID='$staffID'");
		if (mysql_num_rows($existSQL)>=1) {
			while ($appointV=mysql_fetch_array($existSQL)) {
				$resSTART=$appointV['dateTIME'];
				$length=$appointV['totalTIME'];
				$resEND=$resSTART+($length*60);
				if ($inc>=$resSTART && $inc<=$resEND) {
					echo "Appointment time is ".date("g.i a", $inc)." <font color='red'><< $first BOOKED</font><br/>";
				} else if ($inc<$resSTART || $inc>$resEND) {
					echo "Appointment time is ".date("g.i a", $inc)." <font color='green'><< $first AVAILABLE</font><br/>";
				}
			}
		}
		if (mysql_num_rows($existSQL)==0) {
			echo "Appointment time is ".date("g.i a", $inc)." <font color='green'><< $first AVAILABLE</font><br/>";
		}
	}
}

 

could really use some help on this I have been trying different things for a few days now.

Link to comment
Share on other sites

CREATE TABLE IF NOT EXISTS `appointment` (
  `appointmentID` int(10) NOT NULL auto_increment,
  `services` varchar(100) NOT NULL,
  `totalTIME` int(10) NOT NULL,
  `date` varchar(10) NOT NULL,
  `dateTIME` int(100) NOT NULL,
  `staffCONFIRM` varchar(3) NOT NULL default 'no',
  `clientCONFIRM` varchar(3) NOT NULL default 'no',
  `reminderSENT` varchar(3) NOT NULL default 'no',
  `clientID` int(10) NOT NULL,
  `staffID` int(10) NOT NULL,
  PRIMARY KEY  (`appointmentID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

Link to comment
Share on other sites

sorry I can't understand your code. I don't know where onLEAVE or ACTIVE has come from to be put in your SQL query, there is no row called those names.

 

Also, the date stuff at the start didn't work at all for me. It's too hard to follow your code, sorry.

 

Denno

Link to comment
Share on other sites

I finnally got it to work  :D

I put the booked appointment times into an array then checked if the current time slot of the appointment time 15min loop was in the array like this

 

// get working staff from staff table
putenv("TZ=Australia/Adelaide");
$staffSQL=mysql_query("SELECT * FROM staff WHERE ".$weekday." != 'off' AND onLEAVE='no' AND ACTIVE='yes' ORDER BY staffID ASC");
if (mysql_num_rows($staffSQL)>=1) {
while ($staff=mysql_fetch_array($staffSQL)) {
	$name=$staff['staffNAME'];
	$avail=$staff[$weekday];
	$staffID=$staff['staffID'];
	list($first, $last)=explode(" ", $name);
	list($in, $out)=explode("-", $avail);
	$firstTIME="$sqlDATE $in:00";
	$lastTIME="$sqlDATE $out:00";
	echo "$first is available between $avail on $weekday<br/>";
	$in2=strtotime($firstTIME);
	$out2=strtotime($lastTIME);
	$stop=$out2-900;
		echo $in2." - time start - $firstTIME<br/>"; 
		echo $out2." - time finish - $lastTIME<br/>";
// get appointments for staff
$existSQL=mysql_query("SELECT * FROM appointment WHERE date='$sqlDATE' AND staffID='$staffID'");
if (mysql_num_rows($existSQL)>=1) {
	$booked="booked";
	${$booked.$staffID}=array();
	while ($appointV=mysql_fetch_array($existSQL)) {
		$resSTART=$appointV['dateTIME'];
		$length=$appointV['totalTIME'];
		$resEND=$resSTART+($length*60);
		for ($book=$resSTART;$book<=$resEND;$book+=900) {
			${$booked.$staffID}[]=$book;
		}
	}
}
	for ($inc=$in2;$inc<=$stop;$inc+=900) {
		if (isset(${$booked.$staffID}) && in_array($inc, ${$booked.$staffID})) {
			echo "Appointment time is ".date("g.i a", $inc)." <font color='red'><< $first BOOKED</font><br/>";
		} else if (!isset(${$booked.$staffID}) || !in_array($inc, ${$booked.$staffID})) {
			echo "Appointment time is ".date("g.i a", $inc)." <font color='green'><< $first AVAILABLE</font><br/>";
		}
	}
}
} else { echo "No staff availablen today"; }

Link to comment
Share on other sites

onLEAVE and ACTIVE come from the staff table not the appointment table

 

and the date comes from an input variable at the top of the page which will be from a calendar selection but just declared for now and then formatted the way I want it

 

// get requested date
$input="17/1/2011";
// convert date to variables
list($day, $month, $year)=explode("/", $input);
// date fix for single digit day or month
if (strlen($day)=='1') $day="0".$day;
if (strlen($month)=='1') $month="0".$month;

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.