2

I have a row performanta_cpu in my table modele and that row contain diferent numeric cpu's frecv. ex: 800, 1200, 1500, etc and i want to foreach all frecv. but if 4 devices have same cpu speed, shows me 4th times same frecv.

Is there posible to get just once a frecv in html <select> ex: 800, 1000, 1200, 1400, etc ?

Here is my code:

$sql = "SELECT * FROM $tbl_name ORDER BY performanta_cpu DESC";
    foreach ($dbh->query($sql) as $linie)
        {

    print '<option value="'.$linie["performanta_cpu"].'">'.$linie["performanta_cpu"].'</option>'; }?>

and this is what i get:

           <option value="800">800</option>
           <option value="800">800</option>
           <option value="2000">2000</option>
           <option value="2000">2000</option>
           <option value="1600">1600</option>
           <option value="1500">1500</option>
           <option value="1500">1500</option>
           <option value="1500">1500</option>
           <option value="1400">1400</option>
           <option value="1000">1000</option>
           <option value="1000">1000</option>
           <option value="1000">1000</option>
           <option value=""></option>            

The correct will be like this:

           <option value="1600">1600</option>
           <option value="1500">1500</option>
           <option value="1400">1400</option>
           <option value="1300">1300</option>
           <option value="1200">1200</option>
           <option value="1100">1100</option>
           and so...

4 Answers 4

4

Change the query to

SELECT DISTINCT(performanta_cpu) FROM $tbl_name ORDER BY performanta_cpu DESC
Sign up to request clarification or add additional context in comments.

3 Comments

DISTINCT is a keyword, not a function
gerardo, is working but not as well, first is 800, 2000, 1600, 1500, 1400, etc. Why is there 800?
800 is only one device in database with him, is unique, but in future it will be populated...
3

You could SELECT DISTINCT(performanta_cpu) FROM $tbl_name ORDER BY CAST(performanta_cpu AS UNSIGNED) DESC

3 Comments

I get wrong first result like: 800, 1600, 1500, 1400, etc. First must be 1600 because is te biggest. Any Ideeas?
Sounds like you aren't using an integer field. Answer updated.
@Domuta Marcel 800, 1600, 1500 is sorted as string data, not integer, so 8 is a larger characeter than 1 and hence is first in the list.
1

If you will add DISTINCT, its must work.

SELECT DISTINCT(performanta_cpu) FROM $tbl_name ORDER BY performanta_cpu

4 Comments

I get wrong first result like: 800, 1600, 1500, 1400, etc. First must be 1600 because is te biggest. Ideeas?
Please, explain the meaning of DISTINCT option.
@Domuta Marcel 800, 1600, 1500 is sorted as string data, not integer, so 8 is a larger characeter than 1 and hence is first in the list. See Rob's answer.
@asclepix In context: If the query fetches '800' multiple times, DISTINCT only gives 1 instance of '800' back.
0

Try this

$sql = "SELECT * FROM $tbl_name ORDER BY performanta_cpu DESC";
$cpu = array();
foreach ($dbh->query($sql) as $linie)
    $cpu[] = $linie["performanta_cpu"];

$cpu = array_unique($cpu);

foreach($cpu as $val)
    print '<option value="'.$val.'">'.$val.'</option>'; }?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.