mattyvx Posted January 11, 2010 Share Posted January 11, 2010 Hi, I'm optimising my site by trying to reduce any unessesary code that's used and need some help. Members of my site have a profile page and on this page information is displayed about them. A member can be registered to X ammount of cities. There is a query I run within a while loop to fetch all the cities the member is registered to. $citylist = mysql_query("SELECT C.City FROM Cities AS C INNER JOIN Members_to_Cities AS m2c ON C.CityID = m2c.CityID WHERE m2c.ID = '$ID'"); if (!$citylist) die("Query to show fields from table failed."); while ($cityrow=mysql_fetch_array($citylist)) { $City=$cityrow['City']; echo $City; } Now thats great for displaying the list but what I want to do is store each $City in an array which I can print multiple times on one page. Currently I need to use the "City List" twice, this is achieved by running the above query twice. Not efficient. Please help! Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted January 11, 2010 Share Posted January 11, 2010 you can store it in an array like so $cities = array();//create an empty array while ($cityrow=mysql_fetch_array($citylist)) { $City=$cityrow['City']; //echo $City; $cities[] = $City;//push each city into the city array } that way you can also sort the cities, and do other things with that array that you normally couldn't have done without it Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 11, 2010 Share Posted January 11, 2010 So, just store the results in an array and reuse that array when you need the list of cities: $query = "SELECT C.City FROM Cities AS C INNER JOIN Members_to_Cities AS m2c ON C.CityID = m2c.CityID WHERE m2c.ID = '$ID'"; $citylist = mysql_query($query); if (!$citylist) die("Query to show fields from table failed."); $userCities = array(); while ($cityrow = mysql_fetch_array($citylist)) { $userCities[] = $cityrow['City']; } //Output the cities foreach($userCities as $city) { echo $city; } Additionally, if needed, you can store all the cities of multiple users into a multi-dimensional array and create a function for displaying them as needed. Assuming you need to show the cities for multiple users on a single page. Example: function showUserCities($userID, $cityArray) { foreach($cityArray[$userID] as $city) { echo $city; } } $query = "SELECT m2c.ID, C.City FROM Cities AS C INNER JOIN Members_to_Cities AS m2c ON C.CityID = m2c.CityID"; $citylist = mysql_query($query); if (!$citylist) die("Query to show fields from table failed."); $userCities = array(); while ($cityrow = mysql_fetch_array($citylist)) { $userCities[$cityrow['id']] = $cityrow['City']; } //output cities for user with id 9 showUserCities(9, $userCities); Quote Link to comment Share on other sites More sharing options...
mattyvx Posted January 11, 2010 Author Share Posted January 11, 2010 Thanks, Solved! 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.