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
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?

Link to comment
Share on other sites

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>");
}

Link to comment
Share on other sites

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/
Link to comment
Share on other sites

$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>");
}

Link to comment
Share on other sites

$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>");
}

Link to comment
Share on other sites

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>");
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.