Thursday, 30 August 2018

PHP - Sort mysql_fetch_array based on number


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.

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"

0 comments:

Post a Comment