Solved

What am I doing wrong with this function?

Posted on 2014-03-06
4
157 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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 110

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

734 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