Jump to content

Recommended Posts

I am having trouble setting a variable with a ternary operation.  I keep getting a

 

"Notice: Undefined variable: StaffName in /var/www/html/StaffNameFunc.php on line 18"

 

even though I have coded the variable to have a value.  The line before works, but not this one.???

 

My Data:

   

       

PNCID DTG AB AssistingStaff

   

   

       

143 2007-05-22 17:30 26 0

   

 

function StaffName($tStaffID)
{
    include "Connection.php";

    $staffquery = "Select StaffID, Last, left(First,1) as FI, left(Middle,1) as MI, RankID From Staff_be.Staff where Staffid = " . $tStaffID .";";
    $staffresult = mysql_query($staffquery,$link) or die("\n<p>Could not run Staff Query: " . $staffquery . " : " . mysql_error());
    while($staff = mysql_fetch_array($staffresult, MYSQL_ASSOC))
    {
        $rankquery = "Select RankID,Rank from Staff_be.StaffRank where RankID =" . $staff["RankID"].";";
        $rankresult = mysql_query($rankquery,$link) or die("\n<p>Could not run Rank Query: ". mysql_error());
        while($rnk = mysql_fetch_array($rankresult,MYSQL_ASSOC))
        {
            $Rank = $rnk["Rank"];
        }
        $StaffName = $Rank . " " . $staff["Last"] . ", " . $staff["FI"] . " " . $staff["MI"]; //  ***** line 18 *****
    }
    return $StaffName;
}
function StaffPRTCount($tStaffID)
{
    include "Connection.php";
    //include "CadetNameFunc.php";
    
    $PRTQuery = "Select PNCID, DTG, AB, AssistingStaff from PNC_be.PRT Where AB = " . $tStaffID ." or AssistingStaff = " . $tStaffID .";";
    $PRTResults = mysql_query($PRTQuery, $link) or die("\n<p> Could not run PRTQuery : " . $PRTQuery ." : " . mysql_error());
    if(mysql_num_rows($PRTResults)==0)
    {
        $PRT[] = array("PNCID" => 0, "DTG" => "N/A", "Title" =>"Unknown");
    }
    else
    {
        while($PRTRow = mysql_fetch_array($PRTResults, MYSQL_ASSOC))
        {
            $PNCQuery = "Select CadetID From PNC_be.PNC Where PNCID = " . $PRTRow["PNCID"] .";";
            $PNCResults = mysql_query($PNCQuery, $link) or die("\n<p>Could not run PNCQuery : " . $PNCQuery ." : " . mysql_error());
            if(mysql_num_rows($PNCResults)==0)
            {    $CadetID = 107;}
            else
            {
                $PNCRow = mysql_fetch_array($PNCResults, MYSQL_ASSOC);
                $CadetID = $PNCRow["CadetID"];
            }

            $CadetName = CadetName($CadetID);
            $AB = ($PRTRow["AB"]==0) ? "Unknown" : StaffName($PRTRow["AB"]);

/* this line */            
           $Assist = ($PRTRow["AssistingStaff"]==0) ? "Unknown" : StaffName($PRTRow["AssistingStaff"]);
/* Goes to function StaffName anyways  */

            $PRT[] = array("PNCID" => (int)$PRTRow["PNCID"],
                           "DTG" => strtotime($PRTRow["DTG"]),
                           "Title" => "PNCID " . $PRTRow["PNCID"] . "; " . date("d M Y", strtotime($PRTRow["DTG"])) ."; " . $CadetName . "; Applied By:" . StaffName($PRTRow["AB"]) . "; Assisted By:" . StaffName($PRTRow["AssistingStaff"])
                           );
        }
    }
    //sort($PRT);
    return $PRT;
}


 

 

 

Link to comment
https://forums.phpfreaks.com/topic/120726-solved-undefined-variable/
Share on other sites

Your $StaffName variable is created inside a loop from a DB query meaning that if the query produces 0 results then the variable is not defined. If you are returning a variable in a function you should always define it near the top of the function:

 

$StaffName = "";

 

// run query

 

 

return $StaffName;

seems the if statement isnt functioning as you would expect.

 

try using

$Assist = ($PRTRow["AssistingStaff"]=="0") ? "Unknown" : StaffName($PRTRow["AssistingStaff"]);

 

also try this to debug:

 

        while($PRTRow = mysql_fetch_array($PRTResults, MYSQL_ASSOC))
        {
            $PNCQuery = "Select CadetID From PNC_be.PNC Where PNCID = " . $PRTRow["PNCID"] .";";
            $PNCResults = mysql_query($PNCQuery, $link) or die("\n<p>Could not run PNCQuery : " . $PNCQuery ." : " . mysql_error());
            if(mysql_num_rows($PNCResults)==0)
            {    $CadetID = 107;}
            else
            {
                $PNCRow = mysql_fetch_array($PNCResults, MYSQL_ASSOC);
                $CadetID = $PNCRow["CadetID"];
            }

            $CadetName = CadetName($CadetID);
            $AB = ($PRTRow["AB"]==0) ? "Unknown" : StaffName($PRTRow["AB"]);

/* this line */
           print_r($PRTRow);
           $Assist = ($PRTRow["AssistingStaff"]==0) ? "Unknown" : StaffName($PRTRow["AssistingStaff"]);
/* Goes to function StaffName anyways  */

            $PRT[] = array("PNCID" => (int)$PRTRow["PNCID"],
                           "DTG" => strtotime($PRTRow["DTG"]),
                           "Title" => "PNCID " . $PRTRow["PNCID"] . "; " . date("d M Y", strtotime($PRTRow["DTG"])) ."; " . $CadetName . "; Applied By:" . StaffName($PRTRow["AB"]) . "; Assisted By:" . StaffName($PRTRow["AssistingStaff"])
                           );
        }
        exit();

 

At a glance i would guess the problem is that the value of $PRTRow["AssistingStaff"] is a string instead of a integer, so the string "0" should be true.

 

Try my examples, the second you will need to view the source for the answers, it will output the array neatly but it uses \n newline and not < br > html so it's easier to read in the html source file.

 

hope this helps,

No, that still did not work and I also type cast it to force it to a (int), but still it did not work.  So I bypast the while loop in StaffName() by adding a if then clause counting the mysql_num _rows() from the mysql_query. I don't like it because it did not solve the problem, but it is a work around and I don't see that message anymore.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.