Jump to content

Retrieving multiple results in an array?


Solarpitch

Recommended Posts

Hi,

 

In the following script, I am trying to get the last 6 results that were uploaded to the database and display these 6 results in a "recently uploaded section". The first part retrieves the 6 results from the database. This is then called in the required page using $result1 = recentlyuploaded();

 

Al this is fine, but I want to now call these 6 results into the JS script at the end but all that seems to happen is the first result is being pulled from the function and not the rest. Hope this makes sense.  ;)

 



//GET THE 6 MOST RECENTLY UPLOADED RESULTS

function recentlyuploaded()
{
$row2 = array();
dbconnect();

$sql = "SELECT * FROM property_info WHERE DATE_SUB(NOW(), INTERVAL 7 DAY) < uploaded ORDER BY uploaded DESC LIMIT 6";

$result = mysql_query($sql) or die(mysql_error());
while(($row = mysql_fetch_row($result)) != false) {
	$row2[] = $row;
}
return $row2;

}



//CALL THE ABOVE FUNCTION ON THE REQUIRED PAGE

$result = recentlyuploaded();

foreach($result as $e)
{
	$property_id = $e[0];
	$getarea = $e[2];
 	$prop_type = $e[3];
	$location= $e[2]; 
	$price = $e[4];

}

//NOW I WANT TO PUT THE 6 RESULTS INTO EACH JS SLOT BELOW

<script type="text/javascript">

var pausecontent=new Array()

pausecontent[0]= '$property_id', '$getarea', '$location'; 
pausecontent[1]= '$property_id', '$getarea', '$location';
pausecontent[2]= '$property_id', '$getarea', '$location';
pausecontent[3]= '$property_id', '$getarea', '$location';
pausecontent[4]= '$property_id', '$getarea', '$location';
pausecontent[5]= '$property_id', '$getarea', '$location';

</script>

'$property_id', '$getarea', '$location' --> I know these should be different for each call. $property_id[0], $property_id[1] or something but I am not sure how to implement.

Link to comment
https://forums.phpfreaks.com/topic/78228-retrieving-multiple-results-in-an-array/
Share on other sites

You are reassigning the values of the variables on each iteration, thus only the last one will stay:

 

$result = recentlyuploaded();

foreach($result as $e)
{
	$property_id = $e[0];
	$getarea = $e[2];
	$prop_type = $e[3];
	$location= $e[2]; 
	$price = $e[4];

}

 

Also your javascript looks like it is not being echo'd to the page but is inluded in the PHP code.

 

<?php

$result = recentlyuploaded();

echo "<script type=\"text/javascript\">\n";
echo "var pausecontent=new Array();\n";

foreach($i=0; $i<count($result); $i++)
{
    echo "pausecontent[$i]= '".$result[$i][0]."', '$result[$i][2]', '$result[$i][2]';\n";
}

echo "</script>\n";

?>

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.