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->getPictureComm ent($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 multidimensiona l 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($t his->db,$sql);
if(!$result) throw new Exception("Erro r .....");
$arr = array();
while($row = $result->fetch_assoc( )) {
$arr[] = $row;
$comment = "SELECT comment FROM comments WHERE picture_id = '" . $row['id'] . "'";
$innerResult = mysqli_query($t his->db,$comment) ;
if(!$innerResul t) throw new Exception("Erro r ...");
while($innerRow = $innerResult->fetch_assoc( )) {
$arr[count($arr)-1]['comment'][] = $innerRow;
}
mysqli_free_res ult($innerResul t);
}
mysqli_free_res ult($result);
return $arr;
}[/PHP]
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->getPictureComm ent($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 multidimensiona l 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($t his->db,$sql);
if(!$result) throw new Exception("Erro r .....");
$arr = array();
while($row = $result->fetch_assoc( )) {
$arr[] = $row;
$comment = "SELECT comment FROM comments WHERE picture_id = '" . $row['id'] . "'";
$innerResult = mysqli_query($t his->db,$comment) ;
if(!$innerResul t) throw new Exception("Erro r ...");
while($innerRow = $innerResult->fetch_assoc( )) {
$arr[count($arr)-1]['comment'][] = $innerRow;
}
mysqli_free_res ult($innerResul t);
}
mysqli_free_res ult($result);
return $arr;
}[/PHP]
Comment