Jump to content

Dropdown duplicate entries


viezure

Recommended Posts

Hi. I've made a dropdown which lists values from field "scadenta" from table "facturi". It works ok, but it also lists duplicate entries, and i want it to list only unique values.

 

Here is part of the code:

 

<select name="scadenta" type="text" value="<?php echo($_SESSION['scadenta']) ?>">

 

 

<?php

$cerereSQL = 'SELECT scadenta FROM facturi WHERE scadenta!=" " ORDER BY scadenta DESC';

$rezultat = mysql_query($cerereSQL);

 

while($rand = mysql_fetch_array($rezultat)) {

echo ("<option name='$rand[scadenta]' value='$rand[scadenta]'>$rand[scadenta]</option>");

 

What should i do?  :-\

Link to comment
https://forums.phpfreaks.com/topic/105745-dropdown-duplicate-entries/
Share on other sites

D'oh! Thanks a lot, it works.

 

But now i have another question (had it already, not a consequence of your response). The values from that dropdown are of char type and they look like a date, eg. 15/03/1986. I wonder if i could order them by "month", that is by what's in the middle of those //, eg. 03. Can i order them by it?

I would take the array, and create a new array.

 

(untested)

$obm = array();
while($rand = mysql_fetch_array($rezultat)) {
     list($d,$m,$y) = explode("/",$rand);
     $obm[] = $m.'/'.$d.'/'.$y;
}

sort($obm);

foreach($obm as $date){
     echo ("<option name='$date' value='$date'>$date</option>");
}

Now the code is this:

 

<?php

$cerereSQL = 'SELECT DISTINCT scadenta FROM facturi WHERE scadenta!=" " ORDER BY scadenta DESC';

$rezultat = mysql_query($cerereSQL);

 

$obm = array();

while($rand = mysql_fetch_array($rezultat)) {

    list($d,$m,$y) = explode("/",$rand);

    $obm[] = $m.'/'.$d.'/'.$y;

}

 

sort($obm);

 

foreach($obm as $date){

    echo ("<option name='$date' value='$date'>$date</option>");

}

 

And on the dropdown, i see as options

/Array/
$cerereSQL = 'SELECT DISTINCT scadenta FROM facturi WHERE scadenta!=" " ORDER BY scadenta DESC';
$rezultat = mysql_query($cerereSQL);

$obm = array();
$ran = mysql_fetch_array($rezultat);
foreach($ran as $rand) {
     list($d,$m,$y) = explode("/",$rand);
     $obm[] = $m.'/'.$d.'/'.$y;
}

sort($obm);

foreach($obm as $date){
     echo ("<option name='$date' value='$date'>$date</option>");
}

$cerereSQL = 'SELECT DISTINCT scadenta FROM facturi WHERE scadenta!=" " ORDER BY scadenta DESC';
$rezultat = mysql_query($cerereSQL);

$obm = array();
$ran = mysql_fetch_array($rezultat);
foreach($ran as $rand) {
     list($d,$m,$y) = explode("/",$rand);
     $obm[] = $m.'/'.$d.'/'.$y;
}

sort($obm);
$fList = array();
foreach($obm as $oblist) {
     list($m,$d,$y) = explode("/",$oblist);
     $fList[] = $d.'/'.$m.'/'.$y;
}
foreach($fList as $date){
     echo ("<option name='$date' value='$date'>$date</option>");
}

Hope this solves it!

$cerereSQL = 'SELECT DISTINCT scadenta FROM facturi WHERE scadenta!=" " ORDER BY scadenta DESC';
$rezultat = mysql_query($cerereSQL);

$obm = array();
$ran = mysql_fetch_array($rezultat);
foreach($ran as $rand) {
     list($d,$m,$y) = explode("/",$rand);
     $obm[] = $m.'/'.$d.'/'.$y;
}

sort($obm);
$fList = array();
foreach($obm as $oblist) {
     list($m,$d,$y) = explode("/",$oblist);
     $fList[] = $d.'/'.$m.'/'.$y;
}
$fList = array_unique($fList);
foreach($fList as $date){
     echo ("<option name='$date' value='$date'>$date</option>");
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.