0

I am grabbing data from the database, and sorting it in an array.

The data from the query (which is displayed in the proper order):

id    task_id    note    date_created    user_name    document_name    document_type
238   131        3g      1385563455      Admin        doc3             jpg
238   131        3g      1385563455      Admin        doc2             png
238   131        3g      1385563455      Admin        doc1             png
240   131        sd      1385563536      Admin        NULL             NULL
241   131        sd      1385563565      Admin        NULL             NULL
242   131        qw      1385563612      Admin        NULL             NULL

I then grab the data and store it in an array:

$all_notes = array();
foreach($notes as $note) {
    $all_notes[$note["id"]]["text"] = $note["note"];
    $all_notes[$note["id"]]["user_name"] = $note["user_name"];
    $all_notes[$note["id"]]["date"] = $note["date_created"];
    $all_notes[$note["id"]]["task_id"] = $_POST['task_id'];
    $all_notes[$note["id"]]["docs"]["document_name"][] = $note["document_name"];
    $all_notes[$note["id"]]["docs"]["document_type"][] = $note["document_type"];
}

When I echo the $all_notes array, the sort order is now completely opposite. Instead of the first row of data being id 238, it is now 242. Even when I change the query to sort the opposite way, the array is still sorting the data from 238 to 242 (and not 242 to 238, which is what it should be).

Your help will be greatly appreciated!

2 Answers 2

1

What you do in your code is assigning $id element of array with values from db, sure it'll get automatically filled up incrementally.

all_notes[239] = value1; all_notes[238] = value2; etc...

but if you print_r(all_notes), you get 238, 239,... etc, which is ok, as PHP arrays are filled up in incremental order by default;

try it like this:

$i=0;

$all_notes = array();

foreach($notes as $note) {

    $all_notes[$i]["id"] = $note["id"];
    $all_notes[$i]["text"] = $note["note"];
    $all_notes[$i]["user_name"] = $note["user_name"];
    $all_notes[$i]["date"] = $note["date_created"];
    $all_notes[$i]["task_id"] = $_POST['task_id'];
    $all_notes[$i]["docs"]["document_name"][] = $note["document_name"];
    $all_notes[$i]["docs"]["document_type"][] = $note["document_type"];

    $i++;
}

Now your array is filled up in the order it got from DB query.

Sign up to request clarification or add additional context in comments.

Comments

1

Please see the array_flip() function. This will allow you to flip the order of the array.

http://us1.php.net/array_flip

The reason is, if you really think, the 242 values is going into the array first - its the first value coming off the $notes array.

Orrr... try adding ORDER BY DESC to the end of your SQL query. You did not specify how you previously attempted to change the sorting, btw.

Darius

7 Comments

In the query, whether or not I add "ORDER BY notes.date_created DESC" or "ORDER BY notes.date_created ASC", the array will always output in the same reverse order.
Why not order by the ID if you truly want the results flipped... ? You can sort by more than one column you know - you can sort by ID first, then by date_created.
Same problem. This is my query: "SELECT notes.*, users.name as user_name, documents.name as document_name, documents.ext as document_type FROM users, notes LEFT JOIN documents ON notes.id = documents.note_id WHERE task_id=? AND notes.assigned_user_id = users.id ORDER BY id ASC"
The query returns the correct order, but once I place it into the array, the order gets reversed. No matter what I do to the query, the array is always returning that data flipped. I tried using the array_flip() function, but now my array won't display any data.
Did you do $notes = array_flip($notes); before the foreach() ?
|

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.