Reputation:
First I check if the current value is part of the array of already displayed values. If it's not, I display it. I still get duplicate values and I don't know why.. Here's my code:
while ($row = mysql_fetch_array($result))
{
$displayed = array();
if (!(in_array($row['Brand'],$displayed)))
{
array_push($displayed,$row['Brand']);
echo '<li>';
echo $row['Brand'];
echo '</li>';
}
}
Upvotes: 0
Views: 321
Reputation: 4607
Use array_unique($array)
to remove every repeated value easily .
Upvotes: 1
Reputation: 7251
You can try this code :
$displayed = array(); //move here
while ($row = mysql_fetch_array($result))
{
if (!(in_array($row['Brand'],$displayed)))
{
array_push($displayed,$row['Brand']);
echo '<li>';
echo $row['Brand'];
echo '</li>';
}
}
Upvotes: 0
Reputation: 76656
You are re-initializing the array on each iteration, so you are effectively checking if $row['Brand']
exists in an empty array. It doesn't and hence in_array
returns false. Move the array declaration outside the loop.
$displayed = array();
while ($row = mysql_fetch_array($result))
{
# code ...
}
Usual disclaimer: mysql_*
functions are deprecated and are soon to be removed. Please switch to MySQLi or PDO instead.
Upvotes: 0
Reputation: 10153
You are reinitializing $displayed = array();
on every iteration of the while
loop and essentially clearing any data in it. Move it outside, before the loop.
Upvotes: 4