Post it here and I'll look at it.
If you want to have a var like $cities["statename"]="city1 city2 city3" then I have to ask where this data is going and what you're doing with it? Is this going to interface to code someone else wrote, or are you coding to a spec someone wrote without thinking through the easier ways to toss data around?
First, I would say that the array $cities["statename"] part makes sense to me. It makes it fairly easy to refer to a state. It's easy to code for if you have to do a lot of it by hand, but more awkward for loops. I might make two arrays, one that was a numeric to txt key of ids to states, and one that was a numeric id of state to cities for machine work.
But the part of putting the states into a single long string you'll pull apart later seems like an extra step. Why not pull them apart now and build a multidimensional array right now.
One of the beauties of PHP is you can build arrays any damned way you want. One of the horrific design flaws in PHP is that you can build arrays any damned way you want. :-)
Now's the time to use that. Add your favorite error handling code to this if you want to... This is just straight code, not funny tricks.
$conn=connect("connectstringgoeshere);
$query = "select * from cities ordered by state, cityname";
$res=exec($query);
$rows=rows($res);
$startstate="";
for ($outer=0;$outer<$rows;$outer++){
$state = pg_result($res,$i,"state");
$city = pg_result($res,$i,"city");
$cities["$state"][]=$city;
}
That code will build an array that is like this:
$cities["Alabama"][0]="Aardvark Hills";
$cities["Alabama"][2]="Alligator Pit";
$cities["Alabama"][3]="Antelope Park";
...
$cities["Alabama"][299]="Zebra Pines";
$cities["Arizona"][0]="...
You get the idea.
Now a simple:
print implode(" ",$cities["Alabama"]) will give us the space seperated list you wanted, or you could build controls with the array much easier than if you just build the array to start with.
But this brings up another common problem. where is the "Alabama" gonna come from?
If it comes from user input (i.e. a form on a web page, a text file, etc...) you have to make sure they can't slip a '(select * from pg_access)' and hope they can see part of the database.
All information used to input to your program should be something that is a key to a finite array that they can't get out of.
If a form has states as 0,1,2,3 for their keys, and you look up a 0 to find out which state it is. Like this:
$states=array("Alabama","Arkansas","Arizona",...);
foreach($states as $state){
print "<option value=\"$state"\">";
}
is ok if we're gonna take the statename that comes back and hit an array with it like this:
$stat = $states["$statename"]
But not ok for $query = "select * from db where state='$statename'";
People can build a where clause if you're not checking for it.
Instead, doing
$stat=$states["statename"] THEN doing
$query="select * from db where state = '$stat'";
Makes sure we aren't using user data to access your db, especially if it's a publically accessable site, but even on an intranet, there's no excuse for sloppy coding.