Solarpitch Posted November 21, 2007 Share Posted November 21, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/78228-retrieving-multiple-results-in-an-array/ Share on other sites More sharing options...
Psycho Posted November 21, 2007 Share Posted November 21, 2007 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/78228-retrieving-multiple-results-in-an-array/#findComment-395880 Share on other sites More sharing options...
Solarpitch Posted November 21, 2007 Author Share Posted November 21, 2007 Thanks for that.. I am just trying to implement it now but seems to be a syntax issue. I'll keep trying anyway and see how I get on. Quote Link to comment https://forums.phpfreaks.com/topic/78228-retrieving-multiple-results-in-an-array/#findComment-395893 Share on other sites More sharing options...
Psycho Posted November 21, 2007 Share Posted November 21, 2007 Change the echo line in the foreach loop to this: echo "pausecontent[$i]= '".$result[$i][0]."', '".$result[$i][2]."', '".$result[$i][2]."';\n"; Quote Link to comment https://forums.phpfreaks.com/topic/78228-retrieving-multiple-results-in-an-array/#findComment-395971 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.