IF Statement an urgent help needed

[COLOR=“#0000CD”]What do I missing in my “If” statement?

[COLOR=“#000000”]There are two forms, and 3 statuses; booked, available and pending.

If in the Form 1 given statuses are either Pending or Booked the Form 2 should displayed status “Booked”
If in Form 1 given status is “Declined” the Form 2 should displayed status “Available”.

My If statement is working by giving the correct combinations as explained above, BUT if I have in the Form1 two users applying for the same product (and its always only one particular product), only one user will ever get the status “Booked” and others “Declined”, so the Form 2 should display status “Booked” in this case.
But in my case it showing “BookedAvailable”.
I am an absolute beginner in PHP and playing up with syntax trying different combinations, but both my versions giving me the same results… here it is what I have:[/COLOR][/COLOR]

Version 1:
echo “<br><br> <br><b>Assignment Status: </b>”;
// Shows if the project is assigned to another user or not
$assignstatus = mysql_query(“SELECT app_status FROM stu_applications WHERE sid =”.intval($row[‘sid’])." ");
if (mysql_num_rows($assignstatus) > 0)
{
while ($astat = mysql_fetch_assoc($assignstatus)) {

				if (($astat['app_status'] == 'Available') || ($astat['app_status'] == 'Declined'))
				{
					//while ($astat = mysql_fetch_assoc($assignstatus)){
						echo "Available";
					//}
				}
				else {
					echo "Booked";
				}
				
			}
		}

Version 2:

echo "&lt;br&gt;&lt;br&gt; &lt;br&gt;&lt;b&gt;Assignment Status: &lt;/b&gt;";	
		// Shows if the project is assigned to another user or not
		$assignstatus = mysql_query("SELECT app_status FROM stu_applications WHERE sid =".intval($row['sid'])." ");
		if (mysql_num_rows($assignstatus) &gt; 0)
		{
			while ($astat = mysql_fetch_assoc($assignstatus)) {
			
				if ($astat['app_status'] == 'Booked')
				{
					//while ($astat = mysql_fetch_assoc($assignstatus)){
						echo $astat['app_status'];
					//}
				}
				else {
					echo "Available";
				}
				
			}
		}
		else {
			echo "Available";
		}

Please help is needed…I have to fix this by tomorrow :frowning:

If there is only one product and three applicants applying for it. One applicant is approved, two declined. How do you display status of the application by echo Approved instead ApprovedDeclinedDeclined, which is what I’m getting. All suggestions appreciated.Thanx

I think you need to do some filtering concerning the applicant (the IP for example) and the product.

Try this: SELECT app_status FROM stu_applications WHERE sid =“.intval($row[‘sid’]).” AND app_status = ‘Booked’

if there is a row returned, then the app has been booked by someone.
if there isnt, one of two things happened:
1: your sid is invalid (though presumably you’ve already checked for this)
2: noone has booked the assignment in question.

Hi StarLion :)The project is been booked, the code is working. but I don’t like the way it is displayed. So e.g. in results table next to the booked Project name it should read “booked” once the project is booked by user1. In my form in addition to that “booked” (which is correct) also reads “available” and that comes by the fact that user2 has been declined for the same project. Now, the code would work well in the scenario where one person apply for the one project;its based on:

  • if approve then result reads “booked”, so no one else can apply
  • if decline then results reads “available”…making it available to other users to apply
    So in nutshell , when is booked by user 1, but user2 has been declined, I want result just to read “booked” not “booked available”. Because if i have 5 users interested and all apply for the project, only one gets to book it, and the other would get declined the result will read: “booked available available available available”…Isn’t it a problem in my If statement…somewhere? so frustrating…i know i am couple of line of code away getting it right :frowning:

It’s because you’re looping through all of the results, when you’re only interested in one.

You could do it by pulling all of the statuses and then doing what we call a ‘flag loop’
IE:
$flag = false;
while(fetch row) {
if(row[app_status] == Booked) { $flag = true; }
}
if($flag) {
echo “Booked”;
} else {
echo “Available”;
}

imo, narrowing your query is better, but it’s up to you.

Thanks I will try…I’ve been doing php only for couple weeks so when code gets a bit more complex its get over my head :slight_smile:

Hi StarLion, I tried that code you gave me… its not working i wonder whether it is because I didn’t put it in the right place?! this is what I got:

echo “<b>Project Description: </b>”;
// Query to show the project description
$sql = mysql_query(“SELECT data FROM webform_data WHERE cid=13 AND nid=7 AND sid=”.intval($row[‘sid’])." ");

		// If statement to see if there is a result eg more than 0 results
		if ( mysql_num_rows( $sql ) &gt; 0 )
		{
			// while statement to fetch the project description for each row
			while ($info = mysql_fetch_assoc( $sql )){
			// printing the project description on the screen
			echo $info['data'];
			}
		}
		else {
		echo '';
		}
		echo "&lt;br&gt;&lt;br&gt; &lt;br&gt;&lt;b&gt;Assignment Status: &lt;/b&gt;";	
		// Shows if the project is assigned to another student or not
		$assignstatus = mysql_query("SELECT app_status FROM stu_applications WHERE sid =".intval($row['sid'])." ");
		if (mysql_num_rows($assignstatus) &gt; 0)
		{
		
		$flag = false;
			while(fetch row) {
				if(row[app_status] == Booked) { $flag = true; }
			}
			if($flag) {
				echo "Booked";
			} else {
				echo "Available";
			}

a) This line


if(row[app_status] == Booked) { $flag = true; }

Should be correctly quoted:


if($row['app_status'] == 'Booked') { $flag = true; }

b) don’t just say “it does not work”, say why it does not work, what does not happen, messages that appear onscreen etc

c) surround any PHP code samples you post on here with the tags [ PHP ] [ /PHP ] (note: WITHOUT the spaces inside the square tags…)

Thanks for that…the single quotes removed the syntax error. thanks
However it not showing wanted result. On the list it might be several available projects to apply for, but now with flag loop only one project can be applied for. With flag loop the other two apply buttons were gone.

I got lots of code tonight,now need to crunch it all up :slight_smile: Haven’t yet checked whether that mail() code is working, but thanks a lot for respond. I am in southern hemisphere 4:30am…time to call the night off :slight_smile: cheers