Page 1 of 1

Code executes even when if condition isn't true..

Posted: Tue May 21, 2002 12:04 am
by Benjamin
I believe the problem is in the code which I have commented out. For some reason it seems to run even when the if condition isn't true, causing the page to always load the default page.


<?php
$Alexa_Last_Image = 106;
$Alexia_Last_Image = 119;
$Allisin_Last_Image = 112;
$Annie_Last_Image = 154;
$Erica_Last_Image = 109;
$Kaylee_Last_Image = 107;
$Mel_Last_Image = 66;
$Monica_Last_Image = 103;
$Sydney_Last_Image = 106;
$total_all_images = $Alexa_Last_Image + $Alexia_Last_Image + $Allisin_Last_Image + $Annie_Last_Image + $Erica_Last_Image + $Kaylee_Last_Image + $Mel_Last_Image + $Monica_Last_Image + $Sydney_Last_Image;

if (!empty($model))
{
if ($model == "Alexa") $max = $Alexa_Last_Image;
elseif ($model == "Alexia and Tyler") $max = $Alexia_Last_Image;
elseif ($model == "Allisin") $max = $Allisin_Last_Image;
elseif ($model == "Annie and Hanna") $max = $Annie_Last_Image;
elseif ($model == "Erica") $max = $Erica_Last_Image;
elseif ($model == "Kaylee") $max = $Kaylee_Last_Image;
elseif ($model == "Monica") $max = $Monica_Last_Image;
elseif ($model == "Sydney") $max = $Sydney_Last_Image;
}

//if (isset($pic) && $pic > $max);
//{
//unset($model);
//unset($pic);
//}


if (empty($model))
{
$model = "Select A Model";
include 'header.php';
include 'firstvisit.php';
include 'footer.php';
}
else
{
if ($model == "Alexa") $path = "alexablkdrscch";
elseif ($model == "Alexia and Tyler") $path = "alextwgrsbg";
elseif ($model == "Allisin") $path = "aliovergr";
elseif ($model == "Annie and Hanna") $path = "anihangg";
elseif ($model == "Erica") $path = "erccampjungle";
elseif ($model == "Kaylee") $path = "kaybluelng";
elseif ($model == "Monica") $path = "moniroselng";
elseif ($model == "Sydney") $path = "sydfence";
include 'header.php';
include 'photos.php';
include 'footer.php';
}
?>

Posted: Tue May 21, 2002 5:37 am
by ILoveJackDaniels
I hate the empty() function. Try this instead - might work a little better:

Code: Select all

<?php 
$Alexa_Last_Image = 106; 
$Alexia_Last_Image = 119; 
$Allisin_Last_Image = 112; 
$Annie_Last_Image = 154; 
$Erica_Last_Image = 109; 
$Kaylee_Last_Image = 107; 
$Mel_Last_Image = 66; 
$Monica_Last_Image = 103; 
$Sydney_Last_Image = 106; 
$total_all_images = $Alexa_Last_Image + $Alexia_Last_Image + $Allisin_Last_Image + $Annie_Last_Image + $Erica_Last_Image + $Kaylee_Last_Image + $Mel_Last_Image + $Monica_Last_Image + $Sydney_Last_Image; 

if ($model!="") 
&#123; 
if ($model == "Alexa") $max = $Alexa_Last_Image; 
elseif ($model == "Alexia and Tyler") $max = $Alexia_Last_Image; 
elseif ($model == "Allisin") $max = $Allisin_Last_Image; 
elseif ($model == "Annie and Hanna") $max = $Annie_Last_Image; 
elseif ($model == "Erica") $max = $Erica_Last_Image; 
elseif ($model == "Kaylee") $max = $Kaylee_Last_Image; 
elseif ($model == "Monica") $max = $Monica_Last_Image; 
elseif ($model == "Sydney") $max = $Sydney_Last_Image; 
&#125; 

//if (isset($pic) && $pic > $max); 
//&#123; 
//unset($model); 
//unset($pic); 
//&#125; 


if ($model=="") 
&#123; 
$model = "Select A Model"; 
include 'header.php'; 
include 'firstvisit.php'; 
include 'footer.php'; 
&#125; 
else 
&#123; 
if ($model == "Alexa") $path = "alexablkdrscch"; 
elseif ($model == "Alexia and Tyler") $path = "alextwgrsbg"; 
elseif ($model == "Allisin") $path = "aliovergr"; 
elseif ($model == "Annie and Hanna") $path = "anihangg"; 
elseif ($model == "Erica") $path = "erccampjungle"; 
elseif ($model == "Kaylee") $path = "kaybluelng"; 
elseif ($model == "Monica") $path = "moniroselng"; 
elseif ($model == "Sydney") $path = "sydfence"; 
include 'header.php'; 
include 'photos.php'; 
include 'footer.php'; 
&#125; 
?>

Posted: Tue May 21, 2002 6:40 am
by jason
Do a var_dump() on the variable you are having a problem with, and see what it outputs.

Code: Select all

<?php 
$Alexa_Last_Image = 106; 
$Alexia_Last_Image = 119; 
$Allisin_Last_Image = 112; 
$Annie_Last_Image = 154; 
$Erica_Last_Image = 109; 
$Kaylee_Last_Image = 107; 
$Mel_Last_Image = 66; 
$Monica_Last_Image = 103; 
$Sydney_Last_Image = 106; 
$total_all_images = $Alexa_Last_Image + $Alexia_Last_Image + $Allisin_Last_Image + $Annie_Last_Image + $Erica_Last_Image + $Kaylee_Last_Image + $Mel_Last_Image + $Monica_Last_Image + $Sydney_Last_Image; 

var_dump($model);
if (!empty($model)) 
&#123; 
if ($model == "Alexa") $max = $Alexa_Last_Image; 
elseif ($model == "Alexia and Tyler") $max = $Alexia_Last_Image; 
elseif ($model == "Allisin") $max = $Allisin_Last_Image; 
elseif ($model == "Annie and Hanna") $max = $Annie_Last_Image; 
elseif ($model == "Erica") $max = $Erica_Last_Image; 
elseif ($model == "Kaylee") $max = $Kaylee_Last_Image; 
elseif ($model == "Monica") $max = $Monica_Last_Image; 
elseif ($model == "Sydney") $max = $Sydney_Last_Image; 
&#125; 

//if (isset($pic) && $pic > $max); 
//&#123; 
//unset($model); 
//unset($pic); 
//&#125; 

var_dump($model);
if (empty($model)) 
&#123; 
$model = "Select A Model"; 
include 'header.php'; 
include 'firstvisit.php'; 
include 'footer.php'; 
&#125; 
else 
&#123; 
if ($model == "Alexa") $path = "alexablkdrscch"; 
elseif ($model == "Alexia and Tyler") $path = "alextwgrsbg"; 
elseif ($model == "Allisin") $path = "aliovergr"; 
elseif ($model == "Annie and Hanna") $path = "anihangg"; 
elseif ($model == "Erica") $path = "erccampjungle"; 
elseif ($model == "Kaylee") $path = "kaybluelng"; 
elseif ($model == "Monica") $path = "moniroselng"; 
elseif ($model == "Sydney") $path = "sydfence"; 
include 'header.php'; 
include 'photos.php'; 
include 'footer.php'; 
&#125; 
?>

Re: Code executes even when if condition isn't true..

Posted: Tue May 21, 2002 6:50 am
by Benjamin
I replaced the code below with this...

if ($pic >= ($max + 1)) $pic = "001";

and that fixed the problem, although I still can't figure out why the code below wouldn't work.
agtlewis wrote:
if (isset($pic) && $pic > $max);
{
unset($model);
unset($pic);
}

Posted: Tue May 21, 2002 6:54 am
by Benjamin
ILoveJackDaniels wrote:

Code: Select all

if ($model!="")
Thanks for the advice but I don't think that will work because php will return an error if you compare a variable that isn't set yet.. I think.

Posted: Tue May 21, 2002 7:04 am
by jason
agtlewis wrote:
ILoveJackDaniels wrote:

Code: Select all

if ($model!="")
Thanks for the advice but I don't think that will work because php will return an error if you compare a variable that isn't set yet.. I think.
It will do that on empty() as well

Posted: Tue May 21, 2002 11:22 am
by leenoble_uk
if (isset($pic) && $pic > $max);
{
unset($model);
unset($pic);
}

I think it's just that ; at the end of the 'if' line. Take that out and it should work.

also to avoid confusion I always parenthesise (sic) each separate part of the condition.

if ((isset($pic)) && ($pic > $max))
{
unset($model);
unset($pic);
}

k

Posted: Tue May 21, 2002 1:28 pm
by sam
I'll assume that because you haven't set $pic from within your script that you are using it as a passed variable. If so and you are using php 4.1.x or grater than you have to add this to the top of your code. $pic = $_GET["pic"];

[edit]Damn I think leenobal found your error[/edit]

As for the comment of not liking empty(). That is hogwash, it does the same function as ($var == "" || $var == 0) in a lot fewer lines. But whatever you like man.

Cheers Sam

Posted: Tue May 21, 2002 11:36 pm
by Benjamin
Right On, that was the problem, I figured it myself after many long hours of scratching my head, but it seems that no one else I showed the code to caught it either till you. Good Call!
leenoble_uk wrote:if (isset($pic) && $pic > $max);
{
unset($model);
unset($pic);
}

I think it's just that ; at the end of the 'if' line. Take that out and it should work.

also to avoid confusion I always parenthesise (sic) each separate part of the condition.

if ((isset($pic)) && ($pic > $max))
{
unset($model);
unset($pic);
}

k