This is my first time posting here in stackoverflow. I am hoping someone can help me with my issue with mongoDB PHP. I am more of a PHP mySql guy but we suddenly needed to switch to mongoDB. Below is the problem situation in the most explanation that I can give.
Imagine in mysql, I have a table named "photos" with 3 fields (img_id, owner_id, and file_path). And with record below:
Record #1: 1---1---image01.jpg
Record #2: 2---2---image02.jpg
Record #3: 3---2---image03.jpg
Then I have another table named "users" with 2 fields (user_id, fullname) with records below:
Record #1: 1---Jhon Doe
Record #2: 2---Mark Kane
All I need is to display results similar to below:
1.) John Doe has 1 image and the image id is 1 . - image01.jpg
2.) Mark Kane has 2 images and the image ids are 2 and 3 . - image01.jpg, image02.jpg
Please let me know how this should be done in mongoDB PHP. Thanks in advance.
$m = new Mongo();
$collection = $m->users;
$collection1 = $m->photos;
$cursor = $collection->find();
$cursor1 = $collection1->find();
$count = 0;
$result = array();
$path = array();
$str = "";
$str1 = "";
foreach($cursor as $obj){
foreach($cursor1 as $obj1){
if($obj['user_id'] == $obj1['owner_id']){
$result[$count] = $obj1['img_id'];
$path[$count] = $obj1['file_path'];
$count++;
}
}
if($count > 1){
for($i = 0; $i < $count; $i++){
$str .= $result[$count]." and ";
$str1 .= $path[$count]." , ";
}
}
else{
$str = $result[0];
$str1 = $path[0];
}
print "$obj['fullname'] has $count image and the image id is $str - $str1 .";
}
Hope the above code block gives you the desired result, the code is not tested, so some compiler or syntactical errors may be there, but I hope you can solve them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.