stu1040 Posted January 8, 2012 Share Posted January 8, 2012 The first segment of code below displays 5 x DISTINCT cities that are ‘a href’ linked, when clicked will land on a page called locations.php. My problem is, no data displays on the locations page. I could create 5 x different pages, but what’s the point when the whole page format is the same. I’ve tried single quotes, and moving things about but I think it’s something to do with the landing page ‘id’. Any guidance, ideas appreciated. Thanks. LINKS PAGE $data = mysql_query("SELECT DISTINCT location FROM cities ") OR DIE (mysql_error()); while($info = mysql_fetch_array($data)) { Print '<p><a href=locations.php?id="'.$info['".$id."'].'">'.$info['location'] .'</a></p>'; } DATA LANDING PAGE $id = mysql_real_escape_string(trim($_GET['id'])); $data = mysql_query("SELECT location FROM cities WHERE id = '".$id."'") or die(mysql_error()); $info = mysql_fetch_array( $data ); { Print "".$info['location'].""; } Quote Link to comment https://forums.phpfreaks.com/topic/254616-distinct-data-not-displaying-from-links/ Share on other sites More sharing options...
Pikachu2000 Posted January 8, 2012 Share Posted January 8, 2012 In the first query, you only select location, so if you were to look at the HTML source, and even the arguments passed in the URL, I'm sure you'd see that id has no value. Quote Link to comment https://forums.phpfreaks.com/topic/254616-distinct-data-not-displaying-from-links/#findComment-1305617 Share on other sites More sharing options...
stu1040 Posted January 9, 2012 Author Share Posted January 9, 2012 Thanks for your input. I’m not even sure what I should be googling, searching for to solve this problem. If I remove the ‘id’ from the SELECT DISTINCT then I get one of each city, but no data on the landing page. If I add ‘id’ to the SELECT DISTINCT then I get duplicate cities and data shows on landing page. Anyone know which direction I should be going, or what I should be studying? Thanks in advance. $data = mysql_query("SELECT DISTINCT id,location FROM cities ") OR DIE (mysql_error()); while($info = mysql_fetch_array($data)) { Print "<a href=locations.php?id=".$info['id']."'>".$info['location'] ."</a>"; } $id = mysql_real_escape_string(trim($_GET['id'])); $data = mysql_query("SELECT location FROM cities WHERE id = '".$id."'") or die(mysql_error()); $info = mysql_fetch_array( $data ); { Print "".$info['location'].""; } Quote Link to comment https://forums.phpfreaks.com/topic/254616-distinct-data-not-displaying-from-links/#findComment-1305750 Share on other sites More sharing options...
Muddy_Funster Posted January 9, 2012 Share Posted January 9, 2012 how about if you use SELECT id, location FROM cities GROUP BY location ORDER BY id ASC" Quote Link to comment https://forums.phpfreaks.com/topic/254616-distinct-data-not-displaying-from-links/#findComment-1305770 Share on other sites More sharing options...
stu1040 Posted January 9, 2012 Author Share Posted January 9, 2012 Thanks for everyones input. It works. Stu. $data = mysql_query("SELECT DISTINCT id, location FROM cities GROUP BY location ORDER BY id ASC") OR DIE (mysql_error()); while($info = mysql_fetch_array($data)) { Print "<a href=locations.php?id=".$info['id']."'>".$info['location'] ."</a><br />"; } $id = mysql_real_escape_string(trim($_GET['id'])); $data = mysql_query("SELECT id, location FROM cities WHERE id = '$id' GROUP BY location ORDER BY id ASC") or die(mysql_error()); $info = mysql_fetch_array( $data ); { Print "".$info['location'] .""; } Quote Link to comment https://forums.phpfreaks.com/topic/254616-distinct-data-not-displaying-from-links/#findComment-1305780 Share on other sites More sharing options...
Muddy_Funster Posted January 9, 2012 Share Posted January 9, 2012 you shouldn't need the DISTINCT if your using the GROUP BY.... Quote Link to comment https://forums.phpfreaks.com/topic/254616-distinct-data-not-displaying-from-links/#findComment-1305782 Share on other sites More sharing options...
stu1040 Posted January 9, 2012 Author Share Posted January 9, 2012 Thanks. I removed distinct after playing around with it. What should I look for if I want to display everything to do with the link i’ve clicked. Such as clicked ‘london’ and everything related to london appeared, or ‘birmingham’ for example. So far the city appears once, but I require everything to do with that city to appear on the landing page, such as click birmingham and 3 x hotels appear in birmingham or london for example. Am I looking for a ‘while loop’ or similar? $id = mysql_real_escape_string(trim($_GET['id'])); $data = mysql_query("SELECT location, name FROM cities WHERE id = '$id' ") or die(mysql_error()); $info = mysql_fetch_array( $data ); { Print "".$info['location'] ." "; } Quote Link to comment https://forums.phpfreaks.com/topic/254616-distinct-data-not-displaying-from-links/#findComment-1305827 Share on other sites More sharing options...
Muddy_Funster Posted January 9, 2012 Share Posted January 9, 2012 depends entierly on the table structure - but I strongly suggest starting a new thread for that one Quote Link to comment https://forums.phpfreaks.com/topic/254616-distinct-data-not-displaying-from-links/#findComment-1305829 Share on other sites More sharing options...
stu1040 Posted January 9, 2012 Author Share Posted January 9, 2012 ok, thanks for you time. Quote Link to comment https://forums.phpfreaks.com/topic/254616-distinct-data-not-displaying-from-links/#findComment-1305838 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.