A
A
Artqookie2010-11-25 16:01:18
PHP
Artqookie, 2010-11-25 16:01:18

Sorting in PHP?

There is this 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 "<br />";
  echo "<br />";

  $array[$count] = $text[0];
  printf($array[$count]);

  echo "<br />";
  echo "<br />";
}
return $array;

It outputs something like:
First title
Fitst text
Second title
Second text
Third title
Third text

You need to sort in reverse order:
Third title
Third text
Second title
Second text
First title
Fitst text

I know about the function array_reverse(), but where did not try to put it here , does not sort.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
D
Dmitry Koshelenko, 2010-11-25
@Artqookie

When you get from the database:
SELECT * FROM table_name WHERE (expression) [order by field_name [desc][asc]]
ORDER - there you sort in ascending or descending order (ASC or DESC)
In your case, as I understand it, getting $output1 and $output2 so you do...

A
adminimus, 2010-11-25
@adminimus

in SQL it is not destiny to shove all this? It's easier

S
slang, 2010-11-25
@slang

This is all of course not entirely ideologically correct, but you need to replace the last line with:

return array_reverse($array);

O
or10n, 2010-11-25
@or10n

Then I'll ask another question. Each article must have an id. Let's say for the first article I will specify id=1. How can I take the maximum id element from the database, increase it by one and assign it to a new article? I will sort by id, and for this it is necessary that each subsequent article has one more id.

For this, the autoincrement field type in the database is used

O
or10n, 2010-11-25
@or10n

and you can make for not incremental ($i++), but decremental ($i--)
example:
for($i=20,$>=0,$i--){
// ololo
}

N
nikita2206, 2010-11-25
@nikita2206

Show queries to the database that are made there. There, for sure, after all, one request can be enough ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question