Solved

What am I doing wrong with this function?

Posted on 2014-03-06
4
150 Views
Last Modified: 2014-03-07
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
Comment
Question by:brucegust
  • 2
4 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 100 total points
ID: 39911054
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 400 total points
ID: 39912199
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
 

Author Closing Comment

by:brucegust
ID: 39912817
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39912999
Great!  Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now