SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Function not returning value

    I'm fairly new to creating functions and am wondering if this shouldn't actually be written some other way. Basically what I am trying to do is create a list of items to populate a drop down list from a database. This list is used in multiple locations on a single form (and in multiple forms for that matter) so I thought it would be best to write this as it's own function. I am not returning any value, and I am not sure if it is in my code for the function itself, or someplace else.

    Here is the code for the function:
    PHP Code:
    // Build the ALL Jobs drop down list
    function GetAllJobs()
    {
        
    // First clear the returned variable of old data
        
    $AllJobs '';
        
    // Connect to DB and retrieve records
        
    include_once('../inc_dbconnect.inc');
        
    $GetSQL 'SELECT Job_Number, Company_Name FROM Jobs ORDER BY Job_Number';
        
    $GetResult mysqli_query($db_server$GetSQL) or die("Unable to run query: " mysqli_error());
        
    // Set the first/default value
        
    $AllJobs "<option value=\"None\">None</option>";
        
    // Now loop through the results and populate the variable
        
    while ($GetRow mysqli_fetch_array($GetResult))
        {
            
    $JobNum $GetRow['Job_Number'];
            
    $DropInfo $JobNum " ~ " $GetRow['Company_Name'];
            
    $AllJobs .= "<option value=\"$JobNum\">$DropInfo</option>";
        }
        return 
    $AllJobs;

    In my form I make a call to the function at the top of the file, then echo $AllJobs where I want the results to be displayed (i.e. between the <select> statements. I know the database calls are working, and the proper records are being pulled from the db.

    Any guidance is greatly appreciated, even if that guidance starts with "What the heck are you thinking?!?!?!)

    Greg
    Greg Hicks
    Tekamba Computers, LLC
    greg@tekamba.com
    http://tekamba.com

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hard to tell. I would suggest you temporarily add print_r($GetRow); statements inside your loop to see if $GetRow has any data in it.
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,121
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by GHicks View Post
    In my form I make a call to the function at the top of the file, then echo $AllJobs where I want the results to be displayed (i.e. between the <select> statements.
    Show me this code, because if I had to guess you have something like this:
    PHP Code:
    <?php GetAllJobs(); ?>
    <select>
    <?php echo $AllJobs?>
    </select>
    Where you really need to do either of the following:
    PHP Code:
    <?php $AllJobs GetAllJobs(); ?>
    <select>
    <?php echo $AllJobs?>
    </select>
    OR

    PHP Code:
    <select>
    <?php echo GetAllJobs(); ?>
    </select>

  4. #4
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cpradio,

    Your solution was spot on, I wasn't properly calling the function. But then it caused another problem with my connect or die command for the database link. I commented out the "or die" part and it functions properly (using your first suggestions ($AllJobs = GetAllJobs(); then echo $AllJobs)

    Now to figure out how to deal with an error if the database is not properly connected to. I'm considering rewriting this slightly in OOP, but I'm even less versed there and that would require quite a bit of other "re-thinking" for the rest of this project so I'm not exactly sure how I will proceed just yet.

    Thanks for the fix!!!

    Greg
    Greg Hicks
    Tekamba Computers, LLC
    greg@tekamba.com
    http://tekamba.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •