Sorting in PHP?

0 like 0 dislike
25 views
There is such code:

$array = array(); for ($count = 1; (($head = mysql_fetch_array($output1)) and ($text = mysql_fetch_array($output2))); ++$count) { $array[$count] = $head[0]; printf($array[$count]); echo "
"; echo "
"; $array[$count] = $text[0]; printf($array[$count]); echo "
"; echo "
"; } return $array;



It outputs something like:

First title

Fitst text

Second title

Second text

Third title

Third text


...


You want to sort in reverse order:

Third title

Third text

Second title

Second text

First title

Fitst text


...


Know about the function array_reverse(), but where not tried here to stick, not otsortirovyvaya.
by | 25 views

6 Answers

0 like 0 dislike
When you get from the database:
\r
SELECT * FROM table_name WHERE (expression) [order by field_name [desc][asc]]
\r
ORDER — sorted there in ascending or descending (ASC or DESC)
\r
In your case, understand receive $output1 and $output2 do...
by
0 like 0 dislike
SQL is not the fate of all this stuff? It is easier
by
0 like 0 dislike
It's certainly not quite ideologically correct, but You need to replace the last line to:
\r
return array_reverse($array); 
by
0 like 0 dislike
Then once more ask the question. Every article must be id. For example, the first article I will indicate id=1. How can I take from the database the maximum element id, increase it by one and assign a new article? Sorting will be performed by id, and for this purpose, for each article with an id one greater
\r

That the field type is autoincrement in the database
by
0 like 0 dislike
and you can do not for incremental ($i++) and decrement ($i--)
example:
for($i=20,$>=0$i--){
// ololo
}
by
0 like 0 dislike
Show database queries that are made there. There is certainly because a single query can do...
by

Related questions

0 like 0 dislike
3 answers
0 like 0 dislike
3 answers
0 like 0 dislike
4 answers
asked Mar 24, 2019 by SharkyFLY
0 like 0 dislike
4 answers
asked Mar 22, 2019 by Lopar
0 like 0 dislike
6 answers
110,608 questions
257,186 answers
0 comments
28,648 users