0

My query returns from my database rows of data containing arrays of years. For example:

1 > 2004,2005,2006,2007 
2 > 2003,2004,2005 
3 > 2004,2005,2006,2007.

When I select this data I combine it into an array then use array_unique to filter out the duplicates, however this does not work. I am returned with a complete array containing all values.

mysql_query('select * from stock where Make LIKE "%'.$_REQUEST['Make'].'%" AND Model LIKE "%'.$_REQUEST['model'].'%" group by YearRange order by YearRange DESC');

while($row=mysql_fetch_array($Year)){
    $a = explode(',', $row['YearRange']);
    $unique_array = array_unique($a);
    foreach ($unique_array as $key => $value)
    {
        echo "<option>".$value."</option>";
    }
}

So in the above example where I would only want the following values to show: 2003,2004,2005,2006,2007 in each <option>

I get:

2004,2005,2006,2007,2003,2004,2005,2004,2005,2006,2007 in each <option>

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
tomantford
  • 182
  • 22
  • can you show any $row['YearRange'] values by printing it and put that here in code? – Alive to die - Anant May 13 '15 at 11:55
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 13 '15 at 12:05
  • @anantkumarsingh if I echo YearRange I get: 1> 2004,2005,2006,2007 2> 2003,2004,2005 3> 2004,2005,2006,2007. – tomantford May 13 '15 at 12:05
  • You need to add each value to a new array after the explode and then unique the new array. – Jay Blanchard May 13 '15 at 12:08
  • @JayBlanchard How would I do this? Two foreach loops? – tomantford May 13 '15 at 12:15

1 Answers1

2

There are a couple of ways to attack the problem. In my example I show a pretty verbose way, with two foreach() loops -

while($row=mysql_fetch_array($Year)){
    $a = explode(',', $row['YearRange']);
    foreach($a as $year){
        $allYears[] = $year; // each row contributes to one large array
    }
}

// now work with $allYears
$unique = array_unique($allYears);
sort($unique);

// generate your output
foreach ($unique as $key => $value){
   echo "<option>".$value."</option>";
}

Part of the problem in your original code is that you're overwriting your unique array each time instead of creating one array ($allYears in this case), then getting the unique values from that one array. Your foreach() in the while loop is echoing out the unique values from the row and then moving to the next row to echo out the next set of values. Moving the output outside of the while loop allows you to gather all of the data, manipulate it, then perform the output of unique values.

In addition, please stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and consider using PDO, it's not as hard as you think.

Community
  • 1
  • 1
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119