1

So I have a tabel with 10 Rows in it,

They look like this :

+++++++++++++++++++++++++++++++++++++++
+ ID + ACTIVE + NAME + DESCRIPTION    +
+++++++++++++++++++++++++++++++++++++++
+ 1  + 1      + ONE  + O-N-E          +
+++++++++++++++++++++++++++++++++++++++
+ 2  + 2      + TWO  + T-W-O          +
+++++++++++++++++++++++++++++++++++++++
+ 3  + 0      + THREE+ T-H-R-E-E      +
+++++++++++++++++++++++++++++++++++++++
+ 4  + 4      + FOUR + F-O-U-R        +
+++++++++++++++++++++++++++++++++++++++
+ 5  + 5      + FIVE + F-I-V-E        +
+++++++++++++++++++++++++++++++++++++++
+ 6  + 3      + SIX  + S-I-X          +
+++++++++++++++++++++++++++++++++++++++

I'm getting the values of this table by using

$result = mysql_query("SELECT * FROM Table WHERE ACTIVE != 0") or die (mysql_error());

And then I'm fetching them with :

$values = mysql_fetch_array($result) or die (mysql_error());

But now the problem is, that the come out this way : 1 2 4 5 3

And I would like to order this while keeping the NAME and DESCRIPTION that belong to the ACTIVE .

I've read a little bit about sorting 2D array's but I'm not getting anywhere, so if anyone has a clear solution or clear article that would help me alot! Thanks for reading.

3

4 Answers 4

3

Yeah, switch to mysqli or whatever, but the answer to your question is: Add ORDER BY ID to your query:

"SELECT * FROM Table WHERE ACTIVE != 0 ORDER BY ID"
Sign up to request clarification or add additional context in comments.

Comments

0

You need use

while ($values = mysql_fetch_assoc($result))
{
echo $values['NAME']." ".$values['DESCRIPTION'];
}

Comments

0

use mysql_fetch_assoc and access your values by using $values['nameOfValue']

Comments

0

What about using ORDER BY directly in mysql?

$result = mysql_query("SELECT * FROM Table WHERE ACTIVE != 0 ORDER BY ACTIVE ASC") or die (mysql_error());

Then I would like you to remember that mysql_* functions are deprecated so i would advise you to switch to mysqli or PDO

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.