0

I'm trying to display unique value in for loop per below. The data is retrieved from mysql. Currently, it's displaying all the values in this column (Book Name) but just want to show just unique value and remove any duplicated values from displaying.

<?php
$i=0;
while($rows=mysql_fetch_array($result))
{
$roll1[$i]=$rows['bkname'];
$i++;
}
$total_elmt=count($roll1);
?>
<form method="POST" action=" ">
<select name="sel1" id="sel1">
<option value="Select Book Name">Book Name</option>
<?php 
for($j=0;$j<$total_elmt;$j++)
{
?><option><?php 
echo $roll1[$j];
?></option><?php
}
?>
</select>
</form>
Kevin
  • 41,694
  • 12
  • 53
  • 70
Ykstate
  • 35
  • 1
  • 4
  • 1
    you could just use a `DISTINCT` in your query instead of doing that in PHP – Kevin Dec 03 '14 at 00:54
  • Thanks but I would like to still show unique value in this drop down since this page is being used to retrieve data, can you assist? – Ykstate Dec 03 '14 at 00:57

3 Answers3

1

Instead of removing duplicates in the PHP, you could just use a DISTINCT clause in your query:

<?php

$db = new mysqli('localhost', 'username', 'password', 'database_name');
// $sql = 'SELECT DISTINCT(bkname) FROM your_table_name';
$sql = 'SELECT * FROM your_table_name GROUP BY bkname';
$query = mysqli_query($db, $sql);

?>

<form method="POST" action="">
    <select name="sel1" id="sel1">
        <option disabled selected>Book Name</option>
        <?php while($row = mysqli_fetch_assoc($query)): ?>
            <option value="<?php echo $row['bkname']; ?>"><?php echo $row['bkname']; ?></option>
        <?php endwhile; ?>
    </select>
</form>

Obligatory Note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Ref: https://stackoverflow.com/a/12860140/3859027

Community
  • 1
  • 1
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • thanks, I totally got confused but it worked. One more question, since I have multiple columns, how could i include in the DISTINCT query? – Ykstate Dec 03 '14 at 01:17
  • @Ykstate you can use the other variation using `GROUP BY` if you want to have other columns as well, check out my revision above – Kevin Dec 03 '14 at 01:23
0

How about PHPs array_unique function?

<?php
$input = array("Tale of two cities", "Tale of two cities", "A brief history of time", "Hop on Pop");
$noduplicates = array_unique($input);
var_dump($noduplicates);
?>
Todd
  • 5,314
  • 3
  • 28
  • 45
0

This can be handled in your query easiest most likely by using the DISTINCT keyword. If your query is something like

SELECT bkname FROM Books;

Instead you would do

SELECT DISTINCT bkname FROM Books;

If you don't want to modify your dataset though, and want an array of unique values from just a column in the dataset you can insert those values into an array utilizing it as a hashmap. The key will ensure uniqueness and eliminate duplicates.

while($rows=mysql_fetch_array($result)){
    $roll1[$rows['bkname']] = $rows['bkname'];
}
scotty
  • 559
  • 4
  • 6