viezure Posted May 15, 2008 Share Posted May 15, 2008 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 More sharing options...
The Little Guy Posted May 15, 2008 Share Posted May 15, 2008 Try: $cerereSQL = 'SELECT DISTINCT scadenta FROM facturi WHERE scadenta!=" " ORDER BY scadenta DESC'; Link to comment https://forums.phpfreaks.com/topic/105745-dropdown-duplicate-entries/#findComment-541810 Share on other sites More sharing options...
viezure Posted May 15, 2008 Author Share Posted May 15, 2008 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 https://forums.phpfreaks.com/topic/105745-dropdown-duplicate-entries/#findComment-541851 Share on other sites More sharing options...
The Little Guy Posted May 15, 2008 Share Posted May 15, 2008 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 https://forums.phpfreaks.com/topic/105745-dropdown-duplicate-entries/#findComment-541865 Share on other sites More sharing options...
viezure Posted May 15, 2008 Author Share Posted May 15, 2008 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 https://forums.phpfreaks.com/topic/105745-dropdown-duplicate-entries/#findComment-541878 Share on other sites More sharing options...
The Little Guy Posted May 15, 2008 Share Posted May 15, 2008 $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 https://forums.phpfreaks.com/topic/105745-dropdown-duplicate-entries/#findComment-541895 Share on other sites More sharing options...
viezure Posted May 15, 2008 Author Share Posted May 15, 2008 I put your new code, and now it lists in dropdown 2 values, both: 09/14/2008 The values from field scadenta are: 11/03/2008, 14/09/2008, 14/04/2008, 12/05/2008, 14/04/2008 Link to comment https://forums.phpfreaks.com/topic/105745-dropdown-duplicate-entries/#findComment-541905 Share on other sites More sharing options...
The Little Guy Posted May 15, 2008 Share Posted May 15, 2008 $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 https://forums.phpfreaks.com/topic/105745-dropdown-duplicate-entries/#findComment-541908 Share on other sites More sharing options...
viezure Posted May 15, 2008 Author Share Posted May 15, 2008 It's the same as above, still 2 identical values, but now it shows in dropdown 14/09/2008 Which is good bacause it preserves the format, but there's still that problem. Link to comment https://forums.phpfreaks.com/topic/105745-dropdown-duplicate-entries/#findComment-541916 Share on other sites More sharing options...
The Little Guy Posted May 15, 2008 Share Posted May 15, 2008 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 https://forums.phpfreaks.com/topic/105745-dropdown-duplicate-entries/#findComment-541924 Share on other sites More sharing options...
viezure Posted May 15, 2008 Author Share Posted May 15, 2008 Now it shows only one value, 14/09/2008 Anyway thanks a lot for the help, i start to think it's too much work for this not so important thing.. Link to comment https://forums.phpfreaks.com/topic/105745-dropdown-duplicate-entries/#findComment-541926 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.