0

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>';
    }
}
0

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Use array_unique($array) to remove every repeated value easily .

Refrence

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.