Jump to content

Storing "X" ammount of variables


mattyvx

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/188099-storing-x-ammount-of-variables/
Share on other sites

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

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);

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.