0

I've write this functions file:

    <?php

  function get_num_discos()
  {
      include ('connection.php');

      $result = mysql_query("SELECT * FROM disco", $connect);
      $num_rows = mysql_num_rows($result);

      return $num_rows;
  }  

  function get_disco_name_from_id($id)
  {
    include ('connection.php');

    $query = 'SELECT name FROM disco WHERE id = '.$id;
    $result = mysql_query($query,$connect);

    while ($reg = mysql_fetch_array($result))
        $name = $reg['name'];

    return $name;
  }


  function get_lat_long_from_adress($adress)
  {

    $prepAddr = str_replace(' ','+',$address); 
    $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false'); 
    $output= json_decode($geocode);

    $lat = $output->results[0]->geometry->location->lat;
    $long = $output->results[0]->geometry->location->lng;

    return coords($lat,$long);
  }

  function get_closest_disco($client_adress)
  {
    include ('connection.php');

    $num_discos = get_num_discos;
    $client_adress = "C/ Sant Pau, 70, Lloret de Mar";
    $client_coords($client_lat,$client_long) = get_lat_long_from_adress($client_adress);

    $query = 'SELECT adress FROM disco';
    $result = mysql_query($query,$connect);

    while ($row = mysql_fetch_assoc($result))
    {
        $disco_adress[] = $row; 
    }

    for ($i = 0; i<$num_discos; $i++)
    {
        $disco_coords($disco_lat,$disco_long) = get_lat_long_from_adress($disco_adress[$i]);

    }
  }
?>

But I'm getting the following error which I can't solve:

Fatal error: Can't use function return value in write context in C:\xampp\htdocs\discos_test\functions.php on line 47

The error points at: $client_coords($client_lat,$client_long) = get_lat_long_from_adress($client_adress); (this is inside get_closest_disco function)

So is a problem related with the array it returns the function get_lat_long_from_adress($adress). I've just followed this link to make this function.

What's wrong? Any ideas? Thank you.

Community
  • 1
  • 1
b-fg
  • 3,959
  • 2
  • 28
  • 44
  • 1
    What are you trying to accomplish with this line: `$client_coords($client_lat,$client_long) = get_lat_long_from_adress($client_adress);`? – Amal Murali Nov 19 '13 at 15:33
  • 1
    The left side of the assignment is not a type you can assign a value to. It is a function _call_. You probably want to assign to an array instead? `$client_coords[$client_lat][$client_long]` – arkascha Nov 19 '13 at 15:35
  • I wanted client_coords to be a list and assign the elements value returned by the get_lat_long_from_adress function to the variable inside that list ($client_lat,$client_long) – b-fg Nov 19 '13 at 15:50

3 Answers3

0

This looks like you are using the wrong syntax to address array elements:

Instead of $client_coords($client_lat,$client_long) simply use $client_coords[$client_lat][$client_long].

And by the way you try to assign an array it might be that you try to assign the elements of that array. In that case take a look at phps list() expression too.

arkascha
  • 41,620
  • 7
  • 58
  • 90
0

Based on this code:

$client_coords($client_lat,$client_long) = get_lat_long_from_adress($client_adress);

PHP is making the assumption that $client_coords stores the name of a function, and it thinks you're trying to call it with parameter list ($client_lat,$client_long). It then thinks you're trying to assign the result of get_lat_long_from_adress() to the result of that function call.

I suspect this is what you meant to do instead:

list($client_lat,$client_long) = get_lat_long_from_adress($client_adress);

Using list() lets you assign an array to a group of individual variables.

Peter Bloomfield
  • 5,578
  • 26
  • 37
  • I thought that I could assign the name "client_coords" to that list, and that was not necessary to use the list() as "list". Thank you. – b-fg Nov 19 '13 at 15:45
0

I think you are trying to get the lat and long by addr ,so you can change your function like below

function get_lat_long_from_adress($adress)
      {

        $prepAddr = str_replace(' ','+',$address); 
        $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false'); 
        $output= json_decode($geocode);

        $lat = $output->results[0]->geometry->location->lat;
        $long = $output->results[0]->geometry->location->lng;

        return array('lat'=>$lat,'long'=>$long);
      }

and do this to get the result

$client_coords= get_lat_long_from_adress($client_adress);
//do sth about $client_coords['lat']
//do sth about $client_coords['long']
Patato
  • 1,464
  • 10
  • 12