1

I have this snippet of a function inside a larger php class. I also get some simple data from my database which I have trouble getting into the array when fetching the variables.

$json = array();
    $params = array(
        'receiver_name'         => $var,
        'receiver_address1'     => 'this works'
    );

$ch = curl_init();      
$query = http_build_query($params);
        curl_setopt($ch, CURLOPT_URL, self::API_ENDPOINT . '/' . 'shipments/imported_shipment');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $query);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            $output = curl_exec ($ch);
            $http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
            curl_close ($ch);

            $output = json_decode($output, true);

I've tried to assign the variable before the array starts, like this:

$var = $row['field_1'];

But it doesn't work when I try to insert this code. So I then tried:

$var2 = "'$var'"; // but quickly went back.

Since I thought it might have something to do with the quotes. I've tried several other ways of getting the variable to pass properly into the array. But when I don't know exactly what to read up on, I get stuck like this.

  $params = array(
    'receiver_name'         => ".$var.", //this prints ..
    'receiver_name'         => "'.$var.'", //this prints '..' 

Perhaps someone on here can tell me what terminology I should use for searching for more information related to this subject, as I seemingly have alot to read up on?

Update

I think everything should be cut out and simplified nicely now?

<?php
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    $p_name = $row['input_name'];
}

echo $p_name; // echo's correctly

class labels {
public function myFunction() {
    $json = array();
        $params = array(
                'token'                 => $this->_token,
                'name'                  => $p_name,
                'hardcoded'             => 'works fine'
    );
}
}       
echo $p_name; // echo's correctly       
?>
Mark
  • 33
  • 10

1 Answers1

1

You're trying to use a variable that is declared outside of the local scope of the class. You need to pass that variable as parameter for the class method.

class labels {
public function myFunction($p_name) {
    $json = array();
        $params = array(
                'token'                 => $this->_token,
                'name'                  => $p_name,
                'hardcoded'             => 'works fine'
    );
}
}

And when you call your method

$labels = new labels();
$labels->myFunction($p_name);

Then it should work.

David Wyly
  • 1,671
  • 1
  • 11
  • 19
  • Thanks, I tried to change the code to include the variable $p_name in both locations, but nothing changed, still drawing blanks. – Mark Apr 15 '15 at 21:16