|
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."¶m=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."¶m=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,
|