Solarpitch Posted November 15, 2007 Share Posted November 15, 2007 Hey guys, I've wrote this script that will pull the last 6 uploaded results in the database and return them via the function recentlyuploaded() When I call this function it will contain the data from 6 results but the way I have it coded I am only assigning the first value of that array returned. How can I loop through the array that i returned and assign the 6 results to their respective variables? // Script to get the last 6 uploaded properties to the site 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; } // This is the code on the other page to retrieve the results $result = recentlyuploaded(); foreach($result as $e) { $property_id = $e[0]; $getarea = $e[3]; $location2= $e[2]; } Quote Link to comment Share on other sites More sharing options...
s0c0 Posted November 15, 2007 Share Posted November 15, 2007 I can't see anything immediately wrong in the code, but I could be missing something. Have you tried running that query on its own to verify it returns 6 records? Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 15, 2007 Author Share Posted November 15, 2007 Hi, Yeah ot returns the six records alright. So if for example I echo $location2= $e[2]; -> thi swill print out the 6 locations. But I want to assign these 6 location to seperate variables if ya know what I mean. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 15, 2007 Share Posted November 15, 2007 // within foreach loop... $location2[] = $e[2]; ? Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 15, 2007 Author Share Posted November 15, 2007 Sorry... dont know what ya mean? ??? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 15, 2007 Share Posted November 15, 2007 If you do that instead of overwriting it then you will make an array containing the locations. Note that it will results in an E_NOTICE if you don't initialize the array before doing that. Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 15, 2007 Author Share Posted November 15, 2007 So by doing this... $location2[] = $e[2]; // I can then... $L1 = $location2[0] $L2 = $location2[1] $L3 = $location2[2] $L4 = $location2[3] $L5 = $location2[4] $L6 = $location2[5] Is that correct? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 15, 2007 Share Posted November 15, 2007 Why use another variable? Just reference the array when you need to. Ken Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 15, 2007 Author Share Posted November 15, 2007 Your right. Cheers guys!! Quote Link to comment 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.