For each result in MySQL query, push to array (complicated)
- by Dylan Taylor
Okay, here's what I'm trying to do. I am running a MySQL query for the most recent posts. For each of the returned rows, I need to push the ID of the row to an array, then within that ID in the array, I need to add more data from the rows. A multi-dimensional array.
Here's my code thus far.
$query = "SELECT * FROM posts ORDER BY id DESC LIMIT 10";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$id = $row["id"];
$post_title = $row["title"];
$post_text = $row["text"];
$post_tags = $row["tags"];
$post_category = $row["category"];
$post_date = $row["date"];
}
As you can see I haven't done anything with arrays yet. Here's an ideal structure I'm looking for, just incase you're confused.
The master array I guess you could call it. We'll just call this array $posts.
Within this array, I have one array for each row returned in my MySQL query.
Within those arrays there is the $post_title, $post_text, etc.
How do I do this? I'm so confused.. an example would be really appreciated.
-Dylan