Jump to content

Archived

This topic is now archived and is closed to further replies.

onlyican

Queries on Arrays

Recommended Posts

Hey

I am building a Advanced Search form for my site.
One of the opetions is to get them to search for members from x country
Now all the countries are stored in the database to show on the registration form
Therefor
Countries should not have mulitple variations
(Example UK, United Kingdom, England....)

But the ORigininal Database holds ALL countries. And I dont have members from every country (yet)
So What I want to do is run a query against the members, to get there country,
Simple
SELECT * FROM members GROUP BY country

But also on this search form, I want to do simular things, for other fields

I do not wants to have 6 queries running
So I was thinking of running one query, Selecting everything

Then store the result in an array

Is there a way, I can GROUP before putting in the array
or run a query against the array to select unique entries

I was thinking of doing something like

[code]
<?php
$query = "SELECT * FROM members";
$result = mysql_query($query);
$countries = array();
$local = array();

while($row = mysql_fetch_assoc($result)){
if($row["country"] != $country_check){
$countries[] = $row["country"];
}
$country_check = $row["country"];

if($row["local"] != $local_check){
$local[] = $row["local"];
}
$local_check = $row["local"];
}
?>
[/code]

Now I know that would work
But is there a better way to do it

NOTE: There are more than 2 things I will run, this is just a mini example


EDIT//:
Sorry the above wont work cos they are not in order
So
Spain
USA
UK
USA
will show like that

Share this post


Link to post
Share on other sites
If all you want is list of countries with members

SELECT DISTINCT country FROM members ORDER BY country

Share this post


Link to post
Share on other sites
But the thing is I want to select other things as well
Instead of running 10 queries

The way I am currently doing it is

[code]
<?php
$query1 = "SELECT * FROM members WHERE pro_status = 'a'";
$result1 = mysql_query($query1);

while($row = mysql_fetch_assoc($result1)){
if(!in_array($row1["country"], $countries)){
$countries[] = $row1["country"];
}

if(!in_array($row1["local"], $locals)){
$locals[] = $row1["local"];
}

}

sort($countries)
sort($locals);

//The form which will display these

?>
[/code]

Is that the best way, or can anyone recommend a better way

Share this post


Link to post
Share on other sites

×

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.