By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,888 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,888 IT Pros & Developers. It's quick & easy.

mysql and php, multidimensional result

P: 42
Hello everyone,
I have a bit of a problem here, lets me explain the database part first:
I want to select pictures from the database and each picture is connected to multi comments.

normally I would do this with a foreach loop and then a foreach loop for each comment in each picture like this:
[PHP]$pic = new pictureClass();
foreach($pic->getPictures() as $row) {
echo $row['name'];
foreach($pic->getPictureComment($row['id']) as $comment) {
echo $comment;
}
}[/PHP]

however this wont be possible for me because I have to get all the data in the first foreach loop because I dont want to call the class more then once (because Im doing it in flash and it takes longer time to call a php function in flash)

So like I said, I have to get all the data in one array and this is where I'm totally lost. I have no idea how to populate the multidimensional array when it comes to this.
This is what I have written but Its wrong:
[PHP] public function getPictures() {
$sql = "SELECT id, name FROM pictures ORDER BY added DESC";
$result = mysqli_query($this->db,$sql);
if(!$result) throw new Exception("Error .....");

$arr = array();
while($row = $result->fetch_assoc()) {
$arr[] = $row;
$comment = "SELECT comment FROM comments WHERE picture_id = '" . $row['id'] . "'";
$innerResult = mysqli_query($this->db,$comment);
if(!$innerResult) throw new Exception("Error ...");
while($innerRow = $innerResult->fetch_assoc()) {
$arr[count($arr)-1]['comment'][] = $innerRow;
}
mysqli_free_result($innerResult);
}
mysqli_free_result($result);

return $arr;
}[/PHP]
Sep 30 '08 #1
Share this Question
Share on Google+
4 Replies


Atli
Expert 5K+
P: 5,058
Hi.

How is the code not working?
Are you getting any error messages?

It looks fine to me at first glance, except for one thing.
In your second SQL query you enclose your $row['id'] in single-quotes, which you shouldn't. (Assuming that this ID is an integer).

In MySQL numbers should never be quoted, or they will be treated as a string, and the string "1" will not be equal to the integer 1.
Sep 30 '08 #2

P: 42
Thanks for the reply Atli, I will remove the single quotes around the int value.

However this is the part thats a bit messed up:
[PHP]
$arr[] = $row;
....
while($innerRow = $innerResult->fetch_assoc()) {
$arr[]['comment'][] = $innerRow;
}[/PHP]

I want to select the latest array value and insert an array called "comment".
Im unsure if $arr[] creates a new array value in php and im also unsure if $arr[]['comment'] will create another new value of $arr instead of inserting a value to the last $arr value.
I probably confusing you right now, but its a bit confusing for me as well =)
Oct 1 '08 #3

Atli
Expert 5K+
P: 5,058
This can be a bit confusing at first, I know :)

Whenever you do $arr[] = ... a new array element will be added to that array.
You can always specify the last element (count($arr)-1) to insert into the last element rather than create a new one.

What you did in your getPictures example in your first post should work fine.

But you don't really need to add the $row to the $arr array until at the end of the loop. You can use the $row array in your loop, rather than adding it to the $arr and messing with that.

For example:
Expand|Select|Wrap|Line Numbers
  1. $arr = array();
  2. $articles = mysqli_query($db, "SELECT id, title FROM articles");
  3. while($article = $result->fetch_assoc())
  4. {
  5.   $comments = $mysqli_query($db, "SELECT author, text FROM comments WHERE articleID = {$article['id']}");
  6.   while($comment = $comments->fetch_assoc())
  7.   {
  8.     // Add the comments to the $article array, rather then the $arr array.
  9.     $article['Comments'][] = $comment;
  10.     // This just added a new element to the 'Comments' element of 
  11.     // the $article array.
  12.   }
  13.  
  14.   // And now, add the $article array to the $arr array.
  15.   $arr[] = $article;
  16. }
  17.  
This way you don't have to worry about whether your using the correct element in the real array.

That would procude and array that look something like:
Expand|Select|Wrap|Line Numbers
  1. Array(
  2.   [0] = Array(
  3.     ['id] = x,
  4.     ['title'] = 'String',
  5.     ['Comments'] = Array(
  6.       [0] = Array(
  7.         ['author'] = 'String',
  8.         ['text'] = 'String'
  9.       ),
  10.       [1] = Array(
  11.         ['author'] = 'String',
  12.         ['text'] = 'String'
  13.       )
  14.     )
  15.   )
  16. )
  17.  
Oct 1 '08 #4

P: 42
Thanks a lot Atli, your example worked good :)

Although Is it possible to get the comments in the comment array, let me show you what I mean:
$arr['comments'][0] // this will output "array" right now, to get the comment I have to type:
$arr['comments'][0][0] // heres the comment.

This would somehow be easier to work with for this purpose:
$arr['comments'][0] // output comment.

The funny thing is if I remove the [] after $row['comments'] the 'comments' wont act as an array, like this:

$row['comments'] = $innerRow; // gives me a single value instead of an array once I output all the results
$row['comments'][] = $innerRow; // gives me an array with arrays for each value
Oct 2 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.