Jump to content

returning array from function


petroz

Recommended Posts

Hey Guys,

 

I am hoping you can help me find out why a function of mine is only returning 1 record when I am asking for 10.

 

Here is the function I am using to get the data.

 

When I run the $sql that is printed in phpmyadmin.... I get then records. But when I print the $event_data I am only getting 1 record.

 

Any help would be greatly appreciated

 

function get_events($data)
{
	//print_r($data);
	$lat = $data['lat'];
	$long = $data['long'];
	$offset = $data['offset'];
	$radius = $data['radius'];
	$amount = $data['amount'];

	//the events query
	$sql = sprintf("SELECT *, ( 3959 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( latitude ) ) ) ) AS distance FROM events WHERE `status` = '0' HAVING distance < '%s'  ORDER BY distance LIMIT $amount OFFSET $offset",
	  mysql_real_escape_string($lat),
	  mysql_real_escape_string($long),
	  mysql_real_escape_string($lat),
	  mysql_real_escape_string($radius));

	//debugging
	print_r($sql);

	$event_data = array();
	$event_data = mysql_fetch_array(mysql_query($sql));

	//debugging
	print_r($event_data);

	return $event_data;

}

 

Link to comment
https://forums.phpfreaks.com/topic/216913-returning-array-from-function/
Share on other sites

NVM.. I just needed to loop through each row into a array...

//the events query
	$sql = sprintf("SELECT *, ( 3959 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( latitude ) ) ) ) AS distance FROM events WHERE `status` = '0' HAVING distance < '%s'  ORDER BY distance LIMIT $amount OFFSET $offset",
	  mysql_real_escape_string($lat),
	  mysql_real_escape_string($long),
	  mysql_real_escape_string($lat),
	  mysql_real_escape_string($radius));

	$query = mysql_query($sql);

	if (mysql_num_rows($query) > 0){
		foreach (mysql_fetch_array($query) as $row){
			$event_data[] = array(
			    "event" => $row['event'],
			    "event_id" => $row['event_id'],
			    "aid" => $row['aid'],
			    "creator" => $row['creator'],
			    "creatorname" => $row['creatorname'],
			    "email" => $row['email'],
			    "longitude" => $row['longitude'],
			    "latitude" => $row['latitude'],
			    "category" => $row['category'],
			    "rating" => $row['rating']
			);
		}
	}

 

You don't need to do all this:

foreach (mysql_fetch_array($query) as $row){
    $event_data[] = array(
        "event" => $row['event'],
        "event_id" => $row['event_id'],
        "aid" => $row['aid'],
        "creator" => $row['creator'],
        "creatorname" => $row['creatorname'],
        "email" => $row['email'],
        "longitude" => $row['longitude'],
        "latitude" => $row['latitude'],
        "category" => $row['category'],
        "rating" => $row['rating']
    );
}

 

Just do this:

foreach (mysql_fetch_array($query) as $row)
{
    $event_data[] = $row;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.