View Full Version : removing items from an array - mySQL/PHP
Bobafart
12-09-2007, 03:35 AM
I am using mySQL for my news tables.. some of the headlines field in my table contain the exact same content (but they are from a different source so I need to keep them in the table).
Unfortnately, I don't want to display the articles with the same headlines.. so how do I remove rows from my query array that have the identical headlines?
$sql = "SELECT id, headline FROM newsTable WHERE headline LIKE '%$userChannel%' OR body LIKE '%$userChannel%' LIMIT 10";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
echo '<p><a href="http://www.foo.com/thread.php?id='.$row[id].'">'.$row[headline].'</a></p>';
}
I tried SELECT DISTINCT in mySQL but it doesn't work.. so there must be a PHP solution to compare the headline field for each row and not display like headlines...
can anyone help me out please?
Fumigator
12-09-2007, 07:08 AM
array_unique()
Bobafart
12-11-2007, 01:29 AM
ok so after reading php.net about this function I did this:
$sql = "SELECT id, headline FROM newsTable WHERE headline LIKE '%$userChannel%' OR body LIKE '%$userChannel%' LIMIT 10";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
$row = array_unique($row);
echo '<p><a href="http://www.foo.com/thread.php?id='.$row[id].'">'.$row[headline].'</a></p>';
}
not surprisingly it still doesn't work.. how is this done? thanks
_Aerospace_Eng_
12-11-2007, 01:32 AM
Do you have a link to the site? Have you checked the html to see if the link is even there? Why aren't you using any error checking? For all you know the query could be failing.
$result = mysql_query($sql) or die(mysql_error());
Bobafart
12-11-2007, 01:34 AM
I checked the query.. it works fine :)
the browser renders the SELECT query nicely..
_Aerospace_Eng_
12-11-2007, 01:42 AM
You said SELECT DISTINCT didn't work. Can you post the query you tried? It should work.
$sql = "SELECT DISTINCT id, headline FROM newsTable WHERE headline LIKE '%$userChannel%' OR body LIKE '%$userChannel%' LIMIT 10";
Bobafart
12-11-2007, 02:06 AM
You said SELECT DISTINCT didn't work. Can you post the query you tried? It should work.
$sql = "SELECT DISTINCT id, headline FROM newsTable WHERE headline LIKE '%$userChannel%' OR body LIKE '%$userChannel%' LIMIT 10";
SELECT distinct id, headline FROM anews2 WHERE headline LIKE '%Iraq%' OR body LIKE '%Iraq%' LIMIT 10
a query like this will return two identical headlines (identical in every sense, even the number of spaces.. there are no trailing spaces)
Fumigator
12-11-2007, 02:37 AM
The problem with using DISTINCT is it determines distinctness using the entire SELECT column list. So if you have two headlines that are the same, but the ids are different, you'll still get both rows in your resultset.
So, either select only the headline, or use a GROUP BY on the headline column and then select MAX(id) or MIN(id). You can continue to select id with a GROUP BY headline, but it's undetermined which id you'll get back.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.