0

Trying to teach myself php and I am struggling with what I think is a scoping issue?

I have a query, while loop, if/else and a function.

I would like to echo the results of the function get_patronapi_data($id) within my while loop.

My code so far is as follows (stripped for legibility);

$stmt = $conn->prepare("SELECT... ...");  //QUERY here ...

if( $stmt->num_rows > 0 )
{
    while ($stmt->fetch()) {
       if (SomethingIsTrue){
             echo get_patronapi_data($id);
            }
       else {
             echo $somethingElse;
            }  
    }//end while
}//end if

define("APISERVER", "http://mydomain:4500");

function get_patronapi_data($id) {
    $apiurl = "APISERVER" . "/PATRONAPI/$id/dump";//line 135
    $api_contents = get_api_contents($apiurl);
    $api_array_lines = explode("\n", $api_contents);
        foreach($api_array_lines as $line){ 
            if(strpos($line, "EXP DATE") !== false){ 
                $data = explode("=", $line); 
                return $data[1]; 
            }//end if
        }//end foreach
 }//end function

function get_api_contents($apiurl) {
    $api_contents = file_get_contents($apiurl);//line 154
    $api_contents = trim(strip_tags($api_contents));
    return $api_contents;
}//end function

echo get_patronapi_data($id); // this works and echoes $data on screen

Whenever I echo get_patronapi_data($id); outside of the functions I successfully see the data on screen. However when I echo get_patronapi_data($id); within the loop I receive the following errors;

Notice: Use of undefined constant APISERVER - assumed 'APISERVER' in C:\xampp...search_1.php on line 135

Warning: file_get_contents(APISERVER/PATRONAPI/3047468/dump): failed to open stream: No such file or directory in C:\xampp...search_1.php on line 154

I'm sure I am doing something very silly however any help is appreciated.

17
  • Unless I'm mistaken, you don't define $data anywhere in the while loop. (maybe you meant while ($data = $stmt->fetch())?) Commented Oct 8, 2015 at 13:56
  • 1
    as per your edit; there you go. You didn't define APISERVER. It's treated as a constant php.net/manual/en/function.constant.php. Either you define it as a constant, or wrap it in quotes. Commented Oct 8, 2015 at 14:12
  • 1
    @Fred he says he got it to work by calling the function outside of the while-loop though. Commented Oct 8, 2015 at 14:14
  • 1
    define("APISERVER", "http://xxxxxxxxxxxx"); as your very first line. Commented Oct 8, 2015 at 14:26
  • 1
    then do this function get_patronapi_data($conn, $id) { and apply the same when calling the function. and possibly for function get_api_contents($apiurl) { also. or make it available within the global scope. Commented Oct 8, 2015 at 14:30

4 Answers 4

2

Putting some of my comments to an answer.

You need to remove the quotes from:

$apiurl = "APISERVER" . "/PATRONAPI/$id/dump";
          ^         ^ <<< remove those

since that's treated as a string literal, rather than a constant.

It is declared in:

define("APISERVER", "http://mydomain:4500");

Reference:

Then your connection is out of scope and should be used inside your function(s).

I.e.:

function get_patronapi_data($conn, $id)

and do that for all your functions requiring a connection for them.

You will need to make sure that all folders/files have proper permissions to be written to.

Sign up to request clarification or add additional context in comments.

Comments

2

The missing link is the statement:

$data = get_patronapi_data($id);

you should put before echo $data;.

It calls the function get_patronapi_data() passing the value of variable $id as argument and stores the values the function returns in the variable $data.

The variable $data you echo() is not the same as the one used by the function. They use the same name but they are different (I hope you don't use the keyword global in the function).

Read more about functions in the PHP documentation.

The updated (fragment of) code looks like:

    if (SomethingIsTrue) {
        $data = get_patronapi_data($id);
        echo $data;
    } else {
        echo $somethingElse;
    }  

If you don't do further processing on the value returned by the function, you can, as well, remove the temporary variable $data from the code fragment above and just use:

    if (SomethingIsTrue) {
        echo get_patronapi_data($id);
    } else {
        echo $somethingElse;
    }  

1 Comment

it's a variable scope issue then; which was what I was thinking also earlier.
1

Assuming you want to print the data from the get_patronapi_data() every iteration in the while loop, you need to actually call the method instead of using $data. Right now you're trying to print something called $data, though you never set a value to it. The $data you return in the function cannot be used outside of that function, so it does not exist in the while loop.

What you could do in your while loop is $data = get_patronapi_data();, and then echo $data. However, you can just echo get_patronapi_data(); first.

If you only want to call your function once, you need to set a variable before you start your while loop (i.e. $variable = get_patronapi_data()), and then use that variable in your while loop (i.e. echo $variable);

1 Comment

Thanks for this - that makes a lot more sens to me now!
0

I'm not sure I fully understand your question. What happens if you just put

echo get_patronapi_data($id);

in the loop instead of

echo $data;

? As written, you're not calling the function in the loop.

3 Comments

Could you tell me how to do that please?
Well when you say "Whenever I echo get_patronapi_data($id); I successfully see the data on screen" what do you mean exactly? Does the data returned by the function change with every iteration of the loop or is it constant? If constant, then above the while loop just add a line "$data = get_patronapi_data($id);" if it changes, you could just put that line above your in-loop echo.
This does not provide an answer, just another question and on top of that it's not formatted proper as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.