| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
a MySql SELECT question
hey. i know php pretty well, i've been working with it for a while and even built a profile/messageboard website with it, but one thing has been bothering me.
i have a "show" section where users can post shows. to make it easier, i wanted to list the cities the shows are in so if they click on the link of the city, it only displays the shows in that city. so that all worked, i just have 1 thing to fix. how ever many shows are in that city, that's how many times the cities' link shows. here's the link: http://www.imoptical.com/ontario/in...p?a=shows&shows i made a loop to collect all the city names. i just don't know how to say "if this city has been posted, don't show it" in the loop, or "only select this city once" in the query. please help. i've tried so many things and have given up. if you can help, then thank you very much |
|
#2
|
|||
|
|||
|
im sorry i didnt really understand your question, if you could post your query, and what it is doing, and then what you want it to do it would help.
But from what ive seen, if you query cities, and then all shows in that cities, you can either use GROUP BY city_id to find out how many shows are in each city. Or you could do SELECT DISTINCT city_id which would stop the query returning duplicate city id's. If you are joining the shows table onto the city table, then you may be using the wrong join, left join usually works well for me in such circumstances. As i said this is just speculation so if you could give me more details ill be glad to help!! |
|
#3
|
|||
|
|||
|
basically this is what is going on:
i have a bunch of shows that are happening in ontario. so naturally, a lot of shows ahve different cities. now, there are over 100 shows stored in the DB. it'd be hard to look through all those shows when viewing the website, so i wanted to make it easier. i wanted links for each city. so if i click "Hamilton", all the hamilton shows pop up instead of seeing every show from various cities. the onyl problem was: i wanted the links to be dynamic, because of course, i cannot list every city in my area code, so i wanted to take the city name and list it, but i couldn't figure out how to list the city and not have the same name pop up again. if my code was: Code:
$result = mysql_query("SELECT cityID FROM shows ORDER BY city ASC");
while ($r = mysql_fetch_array($result))
{
$city = $r['cityID'];
echo"$city
";
}
it would list every city in the database. i wanted to say: "if the city has already been listed, ignore it and move on" so after days of messing around with stuff that just got me frustrated, i finally came up with this. Code:
$result = mysql_query("SELECT cityID FROM shows ORDER BY city ASC");
while ($r = mysql_fetch_array($result))
{
$city = $r['cityID'];
if ($city != $found[$cityID]){
echo"$city
";
}
$found[$cityID] = $city;
}
what this does is it makes a list for example like: $found[hamilton]; $found[toronto]; $found[ottawa]; $found[barrie]; so if the city comes up again while it's printing out names, it ignores it. is there an even easier way around this? |
|
#4
|
|||
|
|||
|
i tried the SELECT DISTINCT and the GROUP BY and they both worked nicely. thanks. the word GROUP could have saved me days figuring this out. thanks a lot.
|
![]() |
| Viewing: Tutorialized Forums > Databases > MySQL > a MySql SELECT question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|