Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to sort while loop by row named 'sort' or $udaj[5] My code below.

<?php

$query = "SELECT * FROM yees where category = 1";
$vysledek = mysqli_query($my_link, $query);

while ($udaj = mysqli_fetch_array($vysledek)):

    echo "<a href='detail.php?id=" . $udaj[0] . "' class='yee'>";
        echo "<img src='" . $udaj[3] . "' alt='' class='avatar'>";
        echo "<div class='basic-info'>";
            echo "<div class='name'>" . $udaj[1]."</div>";
            echo "<div class='tagline'>" . $udaj[6] . "</div>";
        echo "</div>";
        echo "<img src='" . $udaj[4] . "' alt='' class='cover'>";
        echo "<div class='clear'>&nbsp;</div>";
    echo "</a>";

endwhile;

?>

I tried this, but doesnt work.

$query = "SELECT * FROM yees where category = 1 ORDER BY sort ASC";

Could the problem be that my col is called "sort" which is not allowed since its a keyword??

share|improve this question
    
Define "doesn't work". Is there an error? –  pritaeas Dec 1 '13 at 13:41
    
no it just doesnt sort, its still the same –  user1505027 Dec 1 '13 at 14:08
    
If sort is a keyword, then you can use it if you surround it with backticks. –  pritaeas Dec 3 '13 at 11:14

1 Answer 1

You should go back to using the SQL to sort for you. I don't think there's anything wrong there.

It is also cleaner to use the proper row names, as mysqli_fetch_array will return the values in an associated array as well as a numbered index array.

Also print HTML out in heredoc format; its much easier to read than multiple echo statements

echo <<<EOF
<a href='detail.php?id={$udaj['col0']}' class='yee'>
<img src='{$udaj['col3']}' alt='' class='avatar'>
<div class='basic-info'>
<div class='name'>{$udaj['col1']}</div>
<div class='tagline'>{$udaj['col6']}</div>
</div>
<img src='{$udaj['col4']}' alt='' class='cover'>
<div class='clear'>&nbsp;</div>
</a>
EOF;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.