Page 1 of 1

[SOLVED] simple if else problem

Posted: Mon Mar 14, 2005 2:12 am
by reiqwan
Hi all;

I seem to have a simple if else problem, It does not showmy formerdropdown which it should; I have reffered to\ my php bible and the code ayout seems to be correct, I cannot think of anything else I am performing incorrectly:

Code: Select all

if {
blah blah
} 
elseif ($field_name2 == "Status") //if we find this field give user the following dropdown with these options
						if   ($totalRows_Recordset2 <= $row_Recordset1['MaxSeats']) //if the maximum user number has not been reached show user dropdown {
		  				echo("<tr><td><strong>Attend :</strong></td><td>	<select name='Attend'>
	    <option selected>Choose...</option>
	    <option value='Yes'>Yes</option>
	    <option value='No'>No</option>
	    </select></tr>");
		}
	  else //dont allow user to see dropdown atend code
{ blah blah 
} 
}
Thanks all for any assiatance !

Posted: Mon Mar 14, 2005 2:56 am
by JayBird
how are you setting $fiel_name2.

Does it equal "Status"?

Mark

Posted: Mon Mar 14, 2005 3:06 am
by reiqwan
yes it does equal "Status"

Otherwise this is the actual code I used; dont worrry about the preceding if else code:

Code: Select all

elseif ($field_name2 == "Status") //if we find this field give user the following dropdown with these options
						if   ($totalRows_Recordset2 <= $row_Recordset1['MaxSeats']) {
		  				echo("<tr><td><strong>Attend :</strong></td><td>	<select name='Attend'>
	    <option selected>Choose...</option>
	    <option value='Yes'>Yes</option>
	    <option value='No'>No</option>
	    </select></tr>");
}
		else { //dont allow user to see dropdown atend code
						$Attend = "Wait";
						}
	  else 
{
     echo("<tr><td><strong>$field_name2 :</strong></td><td><input type='text' name='field[]' id='field[]' value=''></td></tr>");
	  }
    }

Posted: Mon Mar 14, 2005 3:13 am
by JayBird
does this line equate to true?

Code: Select all

if   ($totalRows_Recordset2 <= $row_Recordset1['MaxSeats']) {
Dont presume it does....echo the variables to be sure

Posted: Mon Mar 14, 2005 3:30 am
by reiqwan
I have also echoed my variables and I get 1 <= 89 or 0 <= 89 or whatever but it still doesn't show the dropdown ?????


yes most of thetime but it does not show me the dropdown ever which iswhy I am confused as to whether or not my code is working

Posted: Mon Mar 14, 2005 4:09 am
by infolock
reiqwan wrote:yes it does equal "Status"

Otherwise this is the actual code I used; dont worrry about the preceding if else code:

there is all kinds of things that worries me about that statement that i must ask and point out...

if $field_name2 is already set to status, therefore isn't it always going to be true? meaning that your "elseif ($field_name2 == "Status")" statement is useless because if you have declared it to be status before, why are you checking to see if it's still set to status?

that makes me believe that either a) you didn't understand his quesiton correctly, or b) you really don't know.

secondly, what is your first IF statement suposed to be for? what does it check that when/if it fails it resorts to checking if field_name2 is set to status or not?

3rd, how are you echoing your variables out to ensure that they show the values you are wanting?

and 4th, where is the rest of your code?

Posted: Mon Mar 14, 2005 4:48 am
by reiqwan
hi infolock;

Answered in point form (hopefully I can assit you in easing your growing irritation to my post which seems to be currently, quite tremendous)

1) $field_name2 is not always set to status that was my mistake

2) i do understand his question

3) check my code to see my first if statement there are quite a few and have nothing to do witrh the result I am trying to meticulously obtain

4) I am echoing my varibles as follows, if this is incorrect please be a sweetheart and do tell:

Code: Select all

echo $totalRows_Recordset2 . $row_Recordset1['MaxSeats'];
This is the way that section of my code worksand I must apologize for saying that It also equals status bcos that was incorrect. It only says status sometimes, by that I mean when the mysql_fields function runs through my table and picks up a column called 'status' it will execute the if statement within that 'status if statement'

Code: Select all

($field_name2 == &quote;Status&quote;)
.

What it should then dois find out if the maximum amount of people have been reached

Code: Select all

($totalRows_Recordset2 &lt;= $row_Recordset1&#1111;'MaxSeats'])
if not it should then just assign the 'Wait' string to the attend variable.

The following is the complete code for that piece of codeI am running:

Code: Select all

for($y=0; $y < $dbfield_num2; $y++) {
      $field_name2 = mysql_field_name($dbfields2, $y);
     
	 if ($field_name2 == "ID") {
echo "";
}
elseif ($field_name2 == "Title") {//if we find this field give user the following dropdown with these options
    echo("<tr><td><strong>Title :</strong></td><td><select name='field[]' id='field[]'>
                          <option value='' selected>Title</option>
						  <option value='Mr.'>Mr.</option>
                          <option value='Mrs.'>Mrs.</option>
						   <option value='Ms.'>Ms.</option>
						    <option value='Dr.'>Dr.</option>
						    <option value='Prof.'>Prof.</option>
                    </select></tr>");
	  }
	  elseif ($field_name2 == "Transport") {//if we find this field give user the following dropdown with these options
    echo("<tr><td><strong>Transport :</strong></td><td><select name='field[]' id='field[]'>
                          <option value='' selected>Select</option>
						  <option value='Yes'>Yes</option>
                          <option value='No'>No</option>
                    </select></tr>");
	  }
	  elseif ($field_name2 == "Meal") {//if we find this field give user the following dropdown with these options
    echo("<tr><td><strong>Meal Preference :</strong></td><td><select name='field[]' id='field[]'>
                          <option value='' selected>Type</option>
						  <option value='Vegeterian'>Vegeterian</option>
                          <option value='Kosher'>Kosher</option>
						  <option value='Halaal'>Halaal</option>
                    </select></tr>");
	  }
	  elseif ($field_name2 == "Accom") {//if we find this field give user the following dropdown with these options
    echo("<tr><td><strong>Accomodation :</strong></td><td><select name='field[]' id='field[]'>
                          <option value='' selected>Select</option>
						  <option value='Yes'>Yes</option>
                          <option value='No'>No</option>
                    </select></tr>");
	  }
	    elseif ($field_name2 == "Shirt") {//if we find this field give user the following dropdown with these options
    echo("<tr><td><strong>Shirt Size :</strong></td><td><select name='field[]' id='field[]'>
                          <option value='' selected>Size</option>
						  <option value='S'>S</option>
                          <option value='M'>M</option>
						   <option value='L'>L</option>
						    <option value='XL'>XL</option>
						    <option value='XXL'>XXL</option>
                    </select></tr>");
	  }
	     elseif ($field_name2 == "Status") //if we find this field give user the following dropdown with these options
						if   ($totalRows_Recordset2 <= $row_Recordset1['MaxSeats']) {
		  				echo("<tr><td><strong>Attend :</strong></td><td>	<select name='Attend'>
	    <option selected>Choose...</option>
	    <option value='Yes'>Yes</option>
	    <option value='No'>No</option>
	    </select></tr>");
		}
		else { //dont allow user to see dropdown atend code
						$Attend = "Wait";
						}		
	  else 
{
     echo("<tr><td><strong>$field_name2 :</strong></td><td><input type='text' name='field[]' id='field[]' value=''></td></tr>");
	  }
    }

Posted: Mon Mar 14, 2005 5:34 am
by infolock
first, i think you got the wrong impression of my response. it *is* only like 6:30am here (and i've yet to go to sleep) so i may have come off as rude or something... so if that's the way you took it, my opologies. my response, however, was the way it is because there was holes in your responses to key questions that were aimed at trying to figure out the core of the problem...

anyways, second thing. reason i asked for code is a good one... you had a few errors
(
2 of which was a missing } and an unneeded {
)

so, i've edited your code and made comments on where i altered it. that's all i did though. i'll go through the rest of it now, but for now, try this and see what happens. while you do that, i'll go through the rest of it and see what i can see

Code: Select all

for($y=0; $y < $dbfield_num2; $y++)
{
	$field_name2 = mysql_field_name($dbfields2, $y);
	if ($field_name2 == "ID")
	{
		echo "";
	}
	elseif
	(
		$field_name2 == "Title")
//removed a {  here.  it was uneeded..and is what was probably causing your problems 
			//if we find this field give user the following dropdown with these options
//this was changed.  You didn't close a TD here and i removed the ()'s from echo's.
//not that it matters how you echo, to each his own.  i just prefer this method...
			echo "<tr>
					<td><strong>Title :</strong></td>
					<td>
						<select name='field[]' id='field[]'>
							<option value='' selected>Title</option>
							<option value='Mr.'>Mr.</option>
							<option value='Mrs.'>Mrs.</option>
							<option value='Ms.'>Ms.</option>
							<option value='Dr.'>Dr.</option>
							<option value='Prof.'>Prof.</option>
						</select>
					</td>
					</tr>";
	}
	elseif ($field_name2 == "Transport")
	{
		//if we find this field give user the following dropdown with these options
		//again, removed the ()'s from echo.  you can readd... just did it for my own debugging purposes...
		//and again, ya missed another </td>....
		echo "<tr>
				<td><strong>Transport :</strong></td>
				<td>
					<select name='field[]' id='field[]'>
						<option value='' selected>Select</option>
						<option value='Yes'>Yes</option>
						<option value='No'>No</option>
					</select>
				</td>
			</tr>";
	}
	elseif ($field_name2 == "Meal")
	{
		//if we find this field give user the following dropdown with these options..missed another </td> here too...
		echo "<tr>
				<td><strong>Meal Preference :</strong></td>
				<td>
					<select name='field[]' id='field[]'>
						<option value='' selected>Type</option>
						<option value='Vegeterian'>Vegeterian</option>
						<option value='Kosher'>Kosher</option>
						<option value='Halaal'>Halaal</option>
					</select>
                           </td></tr>";
	}
	elseif ($field_name2 == "Accom")
	{
		//if we find this field give user the following dropdown with these options
		//Missed another </td> here....
		echo "<tr>
				<td><strong>Accomodation :</strong></td>
				<td>
					<select name='field[]' id='field[]'>
						<option value='' selected>Select</option>
						<option value='Yes'>Yes</option>             		
	<option value='No'>No</option>
					</select>
				</td>
			</tr>";
	}
	elseif ($field_name2 == "Shirt")
	{
		//if we find this field give user the following dropdown with these options
		//Missed another </td>   Assuming you must have copy/pasted.  happens to everyone...
		echo "<tr>
				<td><strong>Shirt Size :</strong></td>
				<td>
					<select name='field[]' id='field[]'>
						<option value='' selected>Size</option>
						<option value='S'>S</option>
						<option value='M'>M</option>
						<option value='L'>L</option>
						<option value='XL'>XL</option>
						<option value='XXL'>XXL</option>
					</select>
				</td>
			</tr>";
	}
	elseif ($field_name2 == "Status")
	{  //ya missed the { here...added.
		//if we find this field give user the following dropdown with these options
		//Missed another </td>
		if ($totalRows_Recordset2 <= $row_Recordset1['MaxSeats'])
		{
			echo "<tr>
					<td><strong>Attend :</strong></td>
					<td>
						<select name='Attend'>
							<option selected>Choose...</option>
							<option value='Yes'>Yes</option>
							<option value='No'>No</option>
						</select>
					</td>
				</tr>";
		}
		else
		{
			//dont allow user to see dropdown atend code
			$Attend = "Wait";
		}
	}
	else
	{
		echo "<tr>
				<td><strong>$field_name2 :</strong></td>
				<td><input type='text' name='field[]' id='field[]' value=''></td>
			</tr>";
	}
}

thanks for the help

Posted: Mon Mar 14, 2005 6:16 am
by reiqwan
no worries, i just appreciate you helping me ou anyway I tried your code out and apparently I do need this -> { <- on the following line
//removed a { here. it was uneeded..and is what was probably causing your problems ;)
as i was getting a funky
unexpected T_ELSEIF in C:\Program Files\Apache Group\Apache2\htdocs\respondnow\client\invite2.php on line 166
error on that line for not having it there

anywayz I still seem to be getting no drop down after I replaced the curly bracket, this statement isbecomingmore of a problem than it's worth

another thing is that the user creates dynamic tables and I have to pre-empt his decisions based on dropdowns and other funny input types based on the columns he's chosen.(by the way is there a better way of checking the column names he uses, if so I'd appreciate if you'd inform)

and iam puttingit into a php array to insert the form variables into the dynamic table.

thanks for your help so far

Re: thanks for the help

Posted: Mon Mar 14, 2005 6:21 am
by infolock
reiqwan wrote:no worries, i just appreciate you helping me ou anyway I tried your code out and apparently I do need this -> { <- on the following line
//removed a { here. it was uneeded..and is what was probably causing your problems ;)
as i was getting a funky
unexpected T_ELSEIF in C:\Program Files\Apache Group\Apache2\htdocs\respondnow\client\invite2.php on line 166
error on that line for not having it there
that's because i missed the ; hehe...

This :

Code: Select all

for($y=0; $y < $dbfield_num2; $y++)
{
    $field_name2 = mysql_field_name($dbfields2, $y);
    if ($field_name2 == "ID")
    {
        echo "";
	}
    elseif
    (
        $field_name2 == "Title")
//......
should actually be this :

Code: Select all

for($y=0; $y < $dbfield_num2; $y++)
{
    $field_name2 = mysql_field_name($dbfields2, $y);
    if ($field_name2 == "ID")
    {
        echo "";
	}
    elseif
    (
        $field_name2 == "Title");  //<--- notice i missed the ;.... so you do NOT need the { as it will mess your code up...
//......
sorry about that.. :oops:


edit :

just a suggestion too... instead of all those IF and IFELSE's ... why not use a switch?

Code: Select all

switch($field_name2)
	{
		case 'ID' :
			echo "";
		break;
		case 'Title':
				echo "<tr>
						<td><strong>Title :</strong></td>
						<td>
							<select name='field[]' id='field[]'>
								<option value='' selected>Title</option>
								<option value='Mr.'>Mr.</option>
								<option value='Mrs.'>Mrs.</option>
								<option value='Ms.'>Ms.</option>
								<option value='Dr.'>Dr.</option>
								<option value='Prof.'>Prof.</option>
							</select>
						</td>
						</tr>";
		break;
		case 'Transport':
			echo "<tr>
					<td><strong>Transport :</strong></td>
					<td>
						<select name='field[]' id='field[]'>
							<option value='' selected>Select</option>
							<option value='Yes'>Yes</option>
							<option value='No'>No</option>
						</select>
					</td>
				</tr>";
		break;
		case 'Meal':
			echo "<tr>
					<td><strong>Meal Preference :</strong></td>
					<td>
						<select name='field[]' id='field[]'>
							<option value='' selected>Type</option>
							<option value='Vegeterian'>Vegeterian</option>
							<option value='Kosher'>Kosher</option>
							<option value='Halaal'>Halaal</option>
						</select>
	                           </td></tr>";
		break;
		case 'Accom':
			echo "<tr>
					<td><strong>Accomodation :</strong></td>
					<td>
						<select name='field[]' id='field[]'>
							<option value='' selected>Select</option>
							<option value='Yes'>Yes</option>             		
		<option value='No'>No</option>
						</select>
					</td>
				</tr>";
		break;
		case 'Shirt':
			echo "<tr>
					<td><strong>Shirt Size :</strong></td>
					<td>
						<select name='field[]' id='field[]'>
							<option value='' selected>Size</option>
							<option value='S'>S</option>
							<option value='M'>M</option>
							<option value='L'>L</option>
							<option value='XL'>XL</option>
							<option value='XXL'>XXL</option>
						</select>
					</td>
				</tr>";
		break;
		case 'Status':
			if ($totalRows_Recordset2 <= $row_Recordset1['MaxSeats'])
			{
				echo "<tr>
						<td><strong>Attend :</strong></td>
						<td>
							<select name='Attend'>
								<option selected>Choose...</option>
								<option value='Yes'>Yes</option>
								<option value='No'>No</option>
							</select>
						</td>
					</tr>";
			}
			else
			{
				//dont allow user to see dropdown atend code
				$Attend = "Wait";
			}
		break;
		default:
			echo "<tr>
					<td><strong>$field_name2 :</strong></td>
					<td><input type='text' name='field[]' id='field[]' value=''></td>
				</tr>";
	}

great

Posted: Mon Mar 14, 2005 7:05 am
by reiqwan
thanks dude

thats a good idea; I'm trying it out now

and it works and looks much neatger thanks again 4 all ur help :D

Posted: Mon Mar 14, 2005 7:23 am
by infolock
np man glad to be of service =)