Click here to get back home

else or elseif?

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
else or elseif? JRough 07-28-2008
Posted by JRough on July 28, 2008, 12:04 pm
Please log in for more thread options
I'm trying to get error proof code.
I have this code which seems to work but now I look at it I think it
should be elseif not else and I wonder why it works. It is in the
block:
if($_POST('assign']='Open in Excel')]...{
}elseif($_POST['assign']=='Open in Excel')]...
?
shouldn't it be else instead of elseif?
or should it be

if....
elseif.....
else
($_POST['redirect']&&$_POST['redirect'}!=$_SERVER['PHP_SELF'}){
Header)"LOCATION: ".$_POST[redirect'}".php?);

this would get a default else. I am not sure but I think this would
redirect the page back to the starting point?

thanks,

if(empty($id)){
$vars = "type=".$type."&param=".$param;
$th = "<th><a href='idle_cars.php?".$vars."&order_by=lease'>Route</
a></th>";
$TPL_carnumbers = GetHeaders($th,$vars);
$result = GetCars(0,$type,$param,CLM_order_by($order_by));
$MSG_carlist = "IDLE CARS - NO MOVEMENT ".GetHeading($type,$param);
}else{
$vars = "id=".$id."&type=".$type."&param=".$param;
$TPL_carnumbers = GetHeaders('',$vars);
if($_SESSION["LMS_USER_DESC"]=='customer'){
$result = GetCustomerCars($id,'',$param,CLM_order_by($order_by));
$MSG_carlist = "IDLE CARS - NO MOVEMENT ".GetHeading($type,
$param);
}else{
$result = GetCars($id,'leased',$param,CLM_order_by($order_by));
$MSG_carlist = "IDLE CARS - NO MOVEMENT ".GetHeading($type,
$param)." ".GetLeaseCompName($id);
}
}

if ($_POST['assign']!='Open in Excel'){

        if(mysql_numrows($result)==0){
        $TPL_carnumbers.= GetNoCarsMsg($th);

        }        else{
        while ($row = mysql_fetch_assoc($result)){
        $TPL_carnumbers.=MakeSighting($id,$row);
}
}

$TPL_carnumbers.="</table>";

include "header.php";
include $template_path."template_carlist.html";
include "footer.php";
}elseif ($_POST['assign']=='Open in Excel'){
         while ($row = mysql_fetch_assoc($result)){

        Header("Location: idle_carsXL.php");
exit;
        }

}

Posted by Erwin Moller on July 28, 2008, 12:22 pm
Please log in for more thread options
JRough schreef:
> I'm trying to get error proof code.
> I have this code which seems to work but now I look at it I think it
> should be elseif not else and I wonder why it works. It is in the
> block:
> if($_POST('assign']='Open in Excel')]...{
> }elseif($_POST['assign']=='Open in Excel')]...
> ?
> shouldn't it be else instead of elseif?

Hi,

Probably yes, that code above doesn't make sense. It has more problems.

Look:
if($_POST('assign']='Open in Excel')

is strange: It doesn't test for $_POST('assign'] being the same as 'Open
in Excel', but ASSIGN 'Open in Excel' to $_POST('assign'].
I think it was ment to say == instead of =.

Since the current code if($_POST('assign']='Open in Excel') ALWAYS
evaluate to true (because this assignment always do that), it needs fixing.

Even if you fix this, PHP will NEVER arive at the code inside the elseif.

http://nl2.php.net/manual/en/control-structures.elseif.php

I didn't look through the rest of the code, but maybe this helps you
untangle the issue: elseif is NEVER needed. It is just a shorthand.
The following codefragments are equivalent:

if (..expresion1..){
// code if expresion1 is true.
elseif (..expresion2..){
// code if expresion1 is false and expresion2 is true.
} else {
// code if expresion 1 is false, and expresion2 is false.
}

The above executes the same as:

if (..expresion1..){
// code if expresion1 is true.
} else {
if (..expresion2..){
// code if expresion1 is false and expresion2 is true.
} else {
// code if expresion 1 is false, and expresion2 is false.
}
}

Personally I prefer the second over the first because it is clearer how
the blocks {} work.

Hope that helps.
And be sure you check for more = instead of == in that code when you see
an if. It seems like a VB 'programmer' made that code. ;-)

Regards,
Erwin Moller


> or should it be
>
> if....
> elseif.....
> else
> ($_POST['redirect']&&$_POST['redirect'}!=$_SERVER['PHP_SELF'}){
> Header)"LOCATION: ".$_POST[redirect'}".php?);
>
> this would get a default else. I am not sure but I think this would
> redirect the page back to the starting point?
>
> thanks,
>
> if(empty($id)){
> $vars = "type=".$type."&param=".$param;
> $th = "<th><a href='idle_cars.php?".$vars."&order_by=lease'>Route</
> a></th>";
> $TPL_carnumbers = GetHeaders($th,$vars);
> $result = GetCars(0,$type,$param,CLM_order_by($order_by));
> $MSG_carlist = "IDLE CARS - NO MOVEMENT ".GetHeading($type,$param);
> }else{
> $vars = "id=".$id."&type=".$type."&param=".$param;
> $TPL_carnumbers = GetHeaders('',$vars);
> if($_SESSION["LMS_USER_DESC"]=='customer'){
> $result = GetCustomerCars($id,'',$param,CLM_order_by($order_by));
> $MSG_carlist = "IDLE CARS - NO MOVEMENT ".GetHeading($type,
> $param);
> }else{
> $result = GetCars($id,'leased',$param,CLM_order_by($order_by));
> $MSG_carlist = "IDLE CARS - NO MOVEMENT ".GetHeading($type,
> $param)." ".GetLeaseCompName($id);
> }
> }
>
> if ($_POST['assign']!='Open in Excel'){
>
>         if(mysql_numrows($result)==0){
>         $TPL_carnumbers.= GetNoCarsMsg($th);
>
>         }        else{
>         while ($row = mysql_fetch_assoc($result)){
>         $TPL_carnumbers.=MakeSighting($id,$row);
> }
> }
>
> $TPL_carnumbers.="</table>";
>
> include "header.php";
> include $template_path."template_carlist.html";
> include "footer.php";
> }elseif ($_POST['assign']=='Open in Excel'){
>          while ($row = mysql_fetch_assoc($result)){
>
>         Header("Location: idle_carsXL.php");
> exit;
>         }
>
> }

Posted by JRough on July 28, 2008, 4:33 pm
Please log in for more thread options
On Jul 28, 9:22=A0am, Erwin Moller
> JRough schreef:
>
> > I'm trying to get error proof code.
> > I have this code which seems to work but now I look at it I think it
> > should be elseif not else and I wonder why it works. It is in =A0the
> > block:
> > if($_POST('assign']=3D'Open in Excel')]...{
> > }elseif($_POST['assign']=3D=3D'Open in Excel')]...
> > ?
> > shouldn't it be else instead of elseif?
>
> Hi,
>
> Probably yes, that code above doesn't make sense. It has more problems.
>
> Look:
> if($_POST('assign']=3D'Open in Excel')
>
> is strange: It doesn't test for $_POST('assign'] being the same as 'Open
> in Excel', but ASSIGN 'Open in Excel' to $_POST('assign'].
> I think it was ment to say =3D=3D instead of =3D.
>
> Since the current code if($_POST('assign']=3D'Open in Excel') ALWAYS
> evaluate to true (because this assignment always do that), it needs fixin=
g.
>
> Even if you fix this, PHP will NEVER arive at the code inside the elseif.
>
> http://nl2.php.net/manual/en/control-structures.elseif.php
>
> I didn't look through the rest of the code, but maybe this helps you
> untangle the issue: elseif is NEVER needed. It is just a shorthand.
> The following codefragments are equivalent:
>
> if (..expresion1..){
> =A0 =A0// code if expresion1 is true.
> elseif (..expresion2..){
> =A0 =A0// code if expresion1 is false and expresion2 is true.} else {
>
> =A0 =A0// code if expresion 1 is false, and expresion2 is false.
>
> }
>
> The above executes the same as:
>
> if (..expresion1..){
> =A0 =A0// code if expresion1 is true.} else {
>
> =A0 =A0if (..expresion2..){
> =A0 =A0 =A0// code if expresion1 is false and expresion2 is true.
> =A0 =A0} else {
> =A0 =A0 =A0// code if expresion 1 is false, and expresion2 is false.
> =A0 =A0}
>
> }
>
> Personally I prefer the second over the first because it is clearer how
> the blocks {} work.
>
> Hope that helps.
> And be sure you check for more =3D instead of =3D=3D in that code when yo=
u see
> an if. It seems like a VB 'programmer' made that code. ;-)
>
> Regards,
> Erwin Moller
>
> > or should it be
>
> > if....
> > elseif.....
> > else
> > ($_POST['redirect']&&$_POST['redirect'}!=3D$_SERVER['PHP_SELF'}){
> > Header)"LOCATION: =A0".$_POST[redirect'}".php?);
>
> > this would get a default else. =A0I am not sure but I think this would
> > redirect the page back to the starting point?
>
> > thanks,
>
> > if(empty($id)){
> > =A0 $vars =3D "type=3D".$type."&param=3D".$param;
> > =A0 $th =A0 =3D "<th><a href=3D'idle_cars.php?".$vars."&order_by=3Dleas=
e'>Route</
> > a></th>";
> > =A0 $TPL_carnumbers =3D GetHeaders($th,$vars);
> > =A0 $result =3D GetCars(0,$type,$param,CLM_order_by($order_by));
> > =A0 $MSG_carlist =3D "IDLE CARS - NO MOVEMENT ".GetHeading($type,$param=
);
> > }else{
> > =A0 $vars =3D "id=3D".$id."&type=3D".$type."&param=3D".$param;
> > =A0 $TPL_carnumbers =3D GetHeaders('',$vars);
> > =A0 if($_SESSION["LMS_USER_DESC"]=3D=3D'customer'){
> > =A0 =A0 $result =3D GetCustomerCars($id,'',$param,CLM_order_by($order_b=
y));
> > =A0 =A0 $MSG_carlist =3D "IDLE CARS - NO MOVEMENT ".GetHeading($type,
> > $param);
> > =A0 }else{
> > =A0 =A0 $result =3D GetCars($id,'leased',$param,CLM_order_by($order_by)=
);
> > =A0 =A0 $MSG_carlist =3D "IDLE CARS - NO MOVEMENT ".GetHeading($type,
> > $param)." ".GetLeaseCompName($id);
> > =A0 }
> > }
>
> > if ($_POST['assign']!=3D'Open in Excel'){
>
> > =A0 =A0if(mysql_numrows($result)=3D=3D0){
> > =A0 =A0$TPL_carnumbers.=3D GetNoCarsMsg($th);
>
> > =A0 =A0} =A0 =A0 =A0 else{
> > =A0 =A0while ($row =3D mysql_fetch_assoc($result)){
> > =A0 =A0 =A0 =A0 =A0 =A0$TPL_carnumbers.=3DMakeSighting($id,$row);
> > =A0 =A0 =A0 }
> > =A0 =A0 =A0}
>
> > =A0 =A0$TPL_carnumbers.=3D"</table>";
>
> > =A0 =A0 include "header.php";
> > =A0 =A0 include $template_path."template_carlist.html";
> > =A0 =A0 include "footer.php";
> > }elseif ($_POST['assign']=3D=3D'Open in Excel'){
> > =A0 =A0 while ($row =3D mysql_fetch_assoc($result)){
>
> > =A0 =A0Header("Location: idle_carsXL.php");
> > =A0 =A0 =A0exit;
> > =A0 =A0}
>
> > }
>
>

The button to be tested for is this :
<td align=3Dcenter>
        <form action =3D"<?=3D$_SERVER['PHP_SELF']?>" method=3Dpost>
<INPUT TYPE =3D "image" SRC=3D'<?=3D$SITEURL."images/xl2.jpg"?>'
VALUE =3D"Open in Excel" ALT=3D"Open in Excel" NAME=3D"assign"></
form>

How do you test for the button click? Maybe "assign" is fine?
if ($_POST('assign')=3D=3D 'Open in Excel'


I tried
if ($_POST['assign']!=3D'Open in Excel'){
and it doesn't work because it is an image not a button.

thanks,


Posted by Michael Fesser on July 28, 2008, 5:05 pm
Please log in for more thread options
.oO(JRough)

>The button to be tested for is this :
> <td align=center>
>         <form action ="<?=$_SERVER['PHP_SELF']?>" method=post>
> <INPUT TYPE = "image" SRC='<?=$SITEURL."images/xl2.jpg"?>'
> VALUE ="Open in Excel" ALT="Open in Excel" NAME="assign"></
>form>

Looks good so far, except for the short open tags (<?= ...?>). You
should avoid them and use <?php echo ...?> instead. Short open tags
are highly unreliable and will be turned off by default in PHP 6.

>How do you test for the button click? Maybe "assign" is fine?
>if ($_POST('assign')== 'Open in Excel'
>
>
>I tried
>if ($_POST['assign']!='Open in Excel'){
>and it doesn't work because it is an image not a button.

Now we're getting somewhere. Image buttons can be tricky. The browser
doesn't just submit its name or value, but the coordinates at which the
button was "hit" by the mouse or 0, 0 if the button was triggered some
other way.

In your case after the submit the $_POST array should contain the two
entries 'assign_x' and 'assign_y' (the browser sends them as 'assign.x'
and 'assign.y', but PHP turns the dots into underscores for technical
reasons). However, it would not contain an entry with the value "Open in
Excel", so you can't test for that. Instead you could test if 'assign_x'
is there to see if this particular button was pressed. This should work
reliable across various browsers.

HTH
Micha

Posted by Michael Fesser on July 28, 2008, 12:34 pm
Please log in for more thread options
.oO(JRough)

>I'm trying to get error proof code.
>I have this code which seems to work but now I look at it I think it
>should be elseif not else and I wonder why it works. It is in the
>block:
>if($_POST('assign']='Open in Excel')]...{
>}elseif($_POST['assign']=='Open in Excel')]...
>?
>shouldn't it be else instead of elseif?

"elseif" is used to check another condition than just the one used in
the "if" part. But the above is syntactically wrong in many aspects and
doesn't really make sense. In the snippet you attached it's like this:

if ($_POST['assign'] != 'Open in Excel') {
...
} elseif ($_POST['assign'] == 'Open in Excel') {
...
}

And there the "elseif" indeed is not really necessary. But doesn't hurt
either. I'm just wondering what this part is supposed to mean:

while ($row = mysql_fetch_assoc($result)) {
Header("Location: idle_carsXL.php");
exit;
}

The Location header is invalid and the entire block totally useless. Why
the loop?

>or should it be
>
>if....
>elseif.....
>else
>($_POST['redirect']&&$_POST['redirect'}!=$_SERVER['PHP_SELF'}){
>Header)"LOCATION: ".$_POST[redirect'}".php?);
>
>this would get a default else. I am not sure but I think this would
>redirect the page back to the starting point?

The above doesn't redirect anywhere, since it's just a syntactical mess.
It's not even clear to me what the code is supposed to do.

Micha

Similar ThreadsPosted
Re: if statement in an elseif block with a preceeding exit statement July 30, 2008, 6:59 pm
Re: if statement in an elseif block with a preceeding exit statement July 31, 2008, 3:00 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap