Jump to content

While loop to pull single output for each


dubc07

Recommended Posts

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.

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 />'; }
?>

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.