Jump to content

Queries on Arrays


onlyican

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
Link to comment
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

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.