Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

What am I doing wrong with this function?

Here's my code:

 function cer_capxsavebutton($mID) {
         global $edb, $_SESSION;
         
         $data=array();
         $count=0;
         $ticker=0;
         $sql_data = "exec cer_CapxSaveButton $mID";
          if ($_SESSION["portaladmin"] == 1 && $_SESSION["showsql"] != "no")
            echo display_detail("cer/cer_functions.php",$sql_data);
        $result_data = odbc_exec($edb, $sql_data);
        while (odbc_fetch_row($result_data))
            {
            $count++;
            $data["allocatedamount"] = odbc_result($result_data, "allocatedamount");
            $data["totalcost"] = odbc_result($result_data, "totalcost");
                if($data["totalcost"]>$data["allocatedamount"])
                {
                $ticker++;
                }
             } 
        $data["ticker"]= $ticker;
        $data["count"]=$count;  
        return $data;
    }

Open in new window


When I do the following:

if($capxsavebutton["allocatedamount"]>$capxsavebutton["totalcost"])
echo "yes";

I get a "yes."

But, I can't get my "ticker" to register that by incrementing. The value is always zero, even though I've got criteria that should trigger it.

What am I doing wrong?
0
brucegust
Asked:
brucegust
  • 2
2 Solutions
 
GaryCommented:
How are you referencing the $data array outside the function - you know its not available outside the scope of a function, unless you have made it global, but would be held in the variable you use to call the function
0
 
Ray PaseurCommented:
This appears to be a data-dependent problem, and for that you need to be able to see the data.  Without seeing the data, all anyone can do is guess, and that tends to waste time.  You can save time if you learn about the PHP var_dump() function.  It's a great tool for test-driven development.  Another great tool is error_reporting(E_ALL) at the top of every script.

Here is the code with the control structures lined up and some lines of comments added.  I'm with Gary - I think you might be getting the code right inside the function.

Here is one way to see what comes back from the function (assuming that $mID value of 3 is reasonable):

var_dump( cer_capxsavebutton(3) );

function cer_capxsavebutton($mID) {

    // DO NOT PUT $_SESSION INTO A GLOBAL STATEMENT
    global $edb, $_SESSION;
    
    // INITIALIZE SOME VALUES
    $data   = array();
    $count  = 0;
    $ticker = 0;
    
    // ASSIGN A STRING INCLUDING THE FUNCTION ARGUMENT
    $sql_data = "exec cer_CapxSaveButton $mID";
    
    // IF SOME THINGS ARE SET CERTAIN WAYS, CALL A FUNCTION AND ECHO THE RESULTS?  
    // ARE THESE PARENTHESES RIGHT?
    if ($_SESSION["portaladmin"] == 1 && $_SESSION["showsql"] != "no")
        echo display_detail("cer/cer_functions.php",$sql_data);
    
    // RUN AN ODBC QUERY OF SOME SORT
    $result_data = odbc_exec($edb, $sql_data);
    
    // USE AN ITERATOR TO ACCESS ROWS FROM THE QUERY RESULT SET
    while (odbc_fetch_row($result_data))
    {
        // ADD ONE TO THE COUNT
        $count++;
        
        // COPY FILEDS FROM THE RESULT SET TO AN ARRAY
        $data["allocatedamount"] = odbc_result($result_data, "allocatedamount");
        $data["totalcost"]       = odbc_result($result_data, "totalcost");
        
        // IF COST GREATER THAN AMOUNT ADD ONE TO TICKER
        if($data["totalcost"] > $data["allocatedamount"])
        {
            $ticker++;
        }
    }
    
    // AT END OF ALL THE ROWS, COPY TICKER AND COUNT INTO ARRAY
    $data["ticker"] = $ticker;
    $data["count"]  = $count;  
    
    // RETURN THE ARRAY WITH DATA FROM THE LAST ROW THAT WAS FETCHED.  BUT WHY ONLY THIS DATA?
    return $data;
}

Open in new window

Similarly, a good way to find out what's going on inside the function is to sprinkle var_dump() statements so that you can visualize the data.
0
 
brucegustPHP DeveloperAuthor Commented:
Gentlemen, I was able to figure it out based on the logic prompted by your council.

Ray, on line 43 of your post, you asked "Why only this data?" which made me realize that I was only retrieving one row as opposed to, what in this case was going to be, multiple rows.

I restructure my query in a way that gave me a two dimensional array (if I'm using that term correctly) and now I had the capacity to make some comparisons whereas before I was trying to compare a value with...nothing.

So, we got it working and, as always, Ray, Gary - I appreciate your time and insight.
0
 
Ray PaseurCommented:
Great!  Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now