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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.