tomhoad Posted May 20, 2009 Share Posted May 20, 2009 <?php //display the competitions $query = "SELECT comp_name FROM comps"; $result = mysql_query($query) or die('Error : ' . mysql_error()); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $comp_name = $row['comp_name']; ?> <p><?php echo $comp_name;?> - <a href="comp_view.php?comp_name=<?php echo(urlencode($comp_name))?>">View Entrants</a></p> <?php } ?> This code works fine, it displays the comp_name. However, it does it for every record, e.g. if there were three records: Great Competition - View Entrants Great Competition - View Entrants Great Competition - View Entrants What I'd like to do is just list the different comp_names, e.g. Great Competition - View Entrants Brilliant Competition - View Entrants Regardless of how many people have entered - that's on the next page (comp_view.php, which works fine). Sorry if this isn't clear, I'm struggling to put into words what I want! Thanks Link to comment https://forums.phpfreaks.com/topic/158988-solved-listing-the-categories/ Share on other sites More sharing options...
Masna Posted May 20, 2009 Share Posted May 20, 2009 <?php //display the competitions $query = "SELECT comp_name FROM comps"; $result = mysql_query($query) or die('Error : ' . mysql_error()); $comp_names = array(); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $comp_names[] = $row['comp_name']; } $comp_names = array_unique($comp_names); foreach($comp_names as $comp_name){ ?> <p><?php echo $comp_name;?> - <a href="comp_view.php?comp_name=<?php echo(urlencode($comp_name))?>">View Entrants</a></p> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/158988-solved-listing-the-categories/#findComment-838475 Share on other sites More sharing options...
GingerRobot Posted May 20, 2009 Share Posted May 20, 2009 Not sure i follow...are you after a group by clause? SELECT comp_name FROM comp GROUP BY comp_name Link to comment https://forums.phpfreaks.com/topic/158988-solved-listing-the-categories/#findComment-838476 Share on other sites More sharing options...
tomhoad Posted May 20, 2009 Author Share Posted May 20, 2009 Masna, cheers! Works fine. Sorry for not being as clear as I'd like - somethings stuck between my brain and keyboard! Thanks guys. Link to comment https://forums.phpfreaks.com/topic/158988-solved-listing-the-categories/#findComment-838484 Share on other sites More sharing options...
GingerRobot Posted May 20, 2009 Share Posted May 20, 2009 If all you're looking to do is select unique entries, using GROUP BY would be more efficient. Not to mention less code. Link to comment https://forums.phpfreaks.com/topic/158988-solved-listing-the-categories/#findComment-838485 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.