My function does not work!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

My function does not work!

Post by cap2cap10 »

:banghead: Can anyone tell me why my code doesn't work?

Code: Select all

<?php
$acc_stat= $_SESSION['js_account']['acc_stat'];
$prof_stat = $_SESSION['js_profile']['prof_stat'];
$res_stat = $_SESSION['js_resume']['res_stat'];
$pic_stat = $_SESSION['js_resume']['pic_stat'];
$file_stat = $_SESSION['js_resume']['file_stat'];
 
function check_progress($acc_stat, $prof_stat, $res_stat, $pic_stat, $file_stat)
{
if ($acc_stat == "yes")
{
echo "<a href=upload_profile.php > Step 2:  Upload Profile NOW!</a>";
}
elseif ($acc_stat == "yes" && $prof_stat == "yes")
{
echo "<a href=upload_resume_1.php >Step 3:  Upload Resume NOW!</a>";
}
elseif ($acc_stat == "yes" && $prof_stat == "yes" && $res_stat == "yes")
{
echo "<a href=upload_pic.php ><BR>Step 4:  Upload Photo NOW!</a>";
}
elseif ($acc_stat == "yes" && $prof_stat == "yes" && $res_stat == "yes" && $pic_stat == "yes")
{
echo "<a href=upload_file.php >Step 5:  Upload MS Word files NOW!</a>";
}
elseif ($acc_stat == "yes" && $prof_stat == "yes" && $res_stat == "yes" && $pic_stat == "yes" && $file_stat == "yes")
{
echo "Please activate your Candidate Portfolio!";
}
 }
 
?>
I need it to ignore one condition when the other one is true.
it echos nothing at all.
Thanks in advance!

Batoe
tkj
Forum Newbie
Posts: 9
Joined: Sun Sep 28, 2008 5:55 am

Re: My function does not work!

Post by tkj »

Try this..

Code: Select all

<?php
$acc_stat= $_SESSION['js_account']['acc_stat'];
$prof_stat = $_SESSION['js_profile']['prof_stat'];
$res_stat = $_SESSION['js_resume']['res_stat'];
$pic_stat = $_SESSION['js_resume']['pic_stat'];
$file_stat = $_SESSION['js_resume']['file_stat'];
 
function check_progress($acc_stat, $prof_stat, $res_stat, $pic_stat, $file_stat) {
  if ($acc_stat == "yes") {
    echo "<a href=\"upload_profile.php\"> Step 2:  Upload Profile NOW!</a>";
  } elseif ($acc_stat == "yes" && $prof_stat == "yes") {
    echo "<a href=\"upload_resume_1.php\">Step 3:  Upload Resume NOW!</a>";
  } elseif ($acc_stat == "yes" && $prof_stat == "yes" && $res_stat == "yes") {
    echo "<a href=\"upload_pic.php\"><BR>Step 4:  Upload Photo NOW!</a>";
  } elseif ($acc_stat == "yes" && $prof_stat == "yes" && $res_stat == "yes" && $pic_stat == "yes") {
    echo "<a href=\"upload_file.php\">Step 5:  Upload MS Word files NOW!</a>";
  } elseif ($acc_stat == "yes" && $prof_stat == "yes" && $res_stat == "yes" && $pic_stat == "yes" && $file_stat == "yes") {
    echo "Please activate your Candidate Portfolio!";
  }
}
 
?>
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: My function does not work!

Post by Stryks »

With your stacking of if statements ... have you considered that for the second one to be true, the first one MUST be, and therefore the second would never be seen? Same with the third, etc etc.

Either reverse the if statements or do something like ...

Code: Select all

<?php
$acc_stat= $_SESSION['js_account']['acc_stat'];
$prof_stat = $_SESSION['js_profile']['prof_stat'];
$res_stat = $_SESSION['js_resume']['res_stat'];
$pic_stat = $_SESSION['js_resume']['pic_stat'];
$file_stat = $_SESSION['js_resume']['file_stat'];
 
function check_progress($acc_stat, $prof_stat, $res_stat, $pic_stat, $file_stat) {
    if ($acc_stat == "yes") {
        if($prof_stat == "yes") {
            if($res_stat == "yes") {
                if($pic_stat == "yes") {
                    if($file_stat == "yes") echo "Please activate your Candidate Portfolio!";
                } else echo "<a href=\"upload_file.php\">Step 5:  Upload MS Word files NOW!</a>";
            } else echo "<a href=\"upload_pic.php\"><BR>Step 4:  Upload Photo NOW!</a>"; 
        } else echo "<a href=\"upload_resume_1.php\">Step 3:  Upload Resume NOW!</a>";
    } else echo "<a href=\"upload_profile.php\"> Step 2:  Upload Profile NOW!</a>";     
}
    
?>
That way you don't evaluate the same thing over and over, and it stops processing at the earliest possible point.

I might have reversed the messages though, I'm not too sure about the logic you used.

Anyhow ... hope this helps.
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Re: My function does not work!

Post by cap2cap10 »

8O thanks Stryks. you were right! I reversed my code and it works perfectly. Hey, I am still a novice. Things aren't as apparent to me. :drunk: Thanks again for the help!

Batoe
Post Reply