Solved

What am I doing wrong with this function?

Posted on 2014-03-06
4
148 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Great!  Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

17 Experts available now in Live!

Get 1:1 Help Now