dubc07 Posted November 3, 2009 Share Posted November 3, 2009 I have a database with several rows on it with subjects like so. latest news | name | subject latest news | name | subject headline news | name | subject latest news | name | subject I'm using a dropdown menu onSelect via javascript and ajax to list the news. However i'm currently using the group query to only pull (latest news) once. The group function will only select rows on the database that have multiple listings. This query will not pull headline news as there is only one in the database. my query is like so: <?php $query = "SELECT * FROM news WHERE title = '$group' GROUP BY title"; $result = mysql_query($query) or die('Error, query failed'); ?> Does anyone have a solution to this problem. I have tried doing else loops and so forth.. any help is appreciated. Link to comment https://forums.phpfreaks.com/topic/180117-while-loop-to-pull-single-output-for-each/ Share on other sites More sharing options...
dubc07 Posted November 3, 2009 Author Share Posted November 3, 2009 Basically i was just wanting to see if there is another method to do a while loops to grab all the news but if there are more than one with the same title it would just out put it once. Anyone? Link to comment https://forums.phpfreaks.com/topic/180117-while-loop-to-pull-single-output-for-each/#findComment-950287 Share on other sites More sharing options...
dubc07 Posted November 3, 2009 Author Share Posted November 3, 2009 Can anyone help me with this problem? Link to comment https://forums.phpfreaks.com/topic/180117-while-loop-to-pull-single-output-for-each/#findComment-950551 Share on other sites More sharing options...
mrMarcus Posted November 3, 2009 Share Posted November 3, 2009 for the life of me, and after reading this over 2-3 times, i can't figure out what the problem is. what is the "group query" and the "group function"? what do they do? where did they come from? i'm going to take a stab at it anyways: if $group = 'latest news', then having WHERE title = '$group' in your query, obviously returns only records with the title 'latest news'. it will not return ALL instances of title .. why would it? you're telling it not to. if you drop your WHERE clause from the query, and add a while() loop post-query, you will get your desired result .. maybe, as i'm still not 100% sure on what you want. <?PHP $query = "SELECT * FROM `news` GROUP BY `title`"; $result = mysql_query($query) or die('Error:' . mysql_error()); while ($res = mysql_fetch_array ($result)) { echo $res['title'].'<br />'; } ?> Link to comment https://forums.phpfreaks.com/topic/180117-while-loop-to-pull-single-output-for-each/#findComment-950558 Share on other sites More sharing options...
dubc07 Posted November 3, 2009 Author Share Posted November 3, 2009 Ok basically i have a database with news titles on it. The Group By Function works if i have at least 2 of every type of title In the post above you will notice that there are multiple records of latest news, The group by function does what it needs to do, however it does not pull the row headline news from the database because Group by does not allow that. the out put im looking for is the following. <option value="latest news">latest news</option> <option value="headline news">headline news</option> Just once for each. if i run a regular loop it it will continue to spit out latest news however many i have on the database. so this is why i tried the Group by. But it will not pull a single listing in the database. Link to comment https://forums.phpfreaks.com/topic/180117-while-loop-to-pull-single-output-for-each/#findComment-950570 Share on other sites More sharing options...
rossmurphy Posted November 3, 2009 Share Posted November 3, 2009 I think you are trying to populate your dropdown with out having multiple values, right? You could use MySQL DISTINCT on the first field. This way you wouldn't retrieve duplicates. Link to comment https://forums.phpfreaks.com/topic/180117-while-loop-to-pull-single-output-for-each/#findComment-950590 Share on other sites More sharing options...
mrMarcus Posted November 4, 2009 Share Posted November 4, 2009 Ok basically i have a database with news titles on it. The Group By Function works if i have at least 2 of every type of title In the post above you will notice that there are multiple records of latest news, The group by function does what it needs to do, however it does not pull the row headline news from the database because Group by does not allow that. the out put im looking for is the following. <option value="latest news">latest news</option> <option value="headline news">headline news</option> Just once for each. if i run a regular loop it it will continue to spit out latest news however many i have on the database. so this is why i tried the Group by. But it will not pull a single listing in the database. did you try my code? <?php $query = "SELECT * FROM `news` GROUP BY `title`"; $result = mysql_query($query) or die('Error:' . mysql_error()); echo '<select name="name">'; while ($res = mysql_fetch_array ($result)) { echo '<option value="'.$res['title'].'">'.$res['title'].'</option>'; } echo '</select>'; ?> this will output a dropdown select box with unique values (if you have 3 of one title, it only shows one, and if you only have 1 title, it still displays that 1 too). that's what the GROUP BY does in the query. it groups them. try the code and see if that gives you what you wanted. Link to comment https://forums.phpfreaks.com/topic/180117-while-loop-to-pull-single-output-for-each/#findComment-950622 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.