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]
4 1970 Atli 5,058
Expert 4TB
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.
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 =)
Atli 5,058
Expert 4TB
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: -
$arr = array();
-
$articles = mysqli_query($db, "SELECT id, title FROM articles");
-
while($article = $result->fetch_assoc())
-
{
-
$comments = $mysqli_query($db, "SELECT author, text FROM comments WHERE articleID = {$article['id']}");
-
while($comment = $comments->fetch_assoc())
-
{
-
// Add the comments to the $article array, rather then the $arr array.
-
$article['Comments'][] = $comment;
-
// This just added a new element to the 'Comments' element of
-
// the $article array.
-
}
-
-
// And now, add the $article array to the $arr array.
-
$arr[] = $article;
-
}
-
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: -
Array(
-
[0] = Array(
-
['id] = x,
-
['title'] = 'String',
-
['Comments'] = Array(
-
[0] = Array(
-
['author'] = 'String',
-
['text'] = 'String'
-
),
-
[1] = Array(
-
['author'] = 'String',
-
['text'] = 'String'
-
)
-
)
-
)
-
)
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Wihlelm Bierbaum |
last post by:
I'm trying to make use of a certain method of a COM object that takes a
multidimensional array as its sole parameter.
Here's an example of the construction of said array:
$cA = array();
$cA =...
|
by: Henrik |
last post by:
im reciving an error when i tries to read multidimensional XML data, into my
system.
I'm receving the same errors discriped at:
http://support.microsoft.com/default.aspx?scid=kb;en-us;325695...
|
by: Stephen Preston |
last post by:
Trying to load an multidimensional array into a MySQL table with columns as
follows,
level1,level2,level3,illust,item,description,partNo,qua,price,remarks,weight
,size,mass
the array first...
|
by: |
last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily
complicated than the other languages I know. I feel this is because there
aren't many good official resources out there to...
|
by: frizzle |
last post by:
Hi there!
I'd like to create a function which input is the result of a mySQL
query.
The output should be exactly the same, only not a mySQL result array,
but a 'real' array.
So it should also...
|
by: luftikus143 |
last post by:
Hi there,
I need to store three pieces of data - a result of a SQL query - in a multidimensional array, but don't really succeed. Sometimes it works, but then the output doesn't work accordingly....
|
by: oopsatwork |
last post by:
Ok...so, I have been outside of the C world for a _very_ long
time...but not so long as to remember how to do multidimensional
arrays. So, let me state that I know how to malloc pointers to...
|
by: rich |
last post by:
I'm having a tough time figuring out which of these two options are
best. This is a matter of processing my data in PHP, vs MySQL.
Usually that's a no brainer, but I have a couple gotchyas here...
|
by: mouac01 |
last post by:
Let's say I have this MySQL table.
Year Region Amount
2007 West 100
2007 East 200
2007 North 300
2007 South ...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| | |