Welcome Guest, Not a member yet? Register   Sign In
Getting data in a big array codeigniter
#1

(This post was last modified: 09-27-2019, 01:34 PM by tp45.)

hello
Am doing friend suggestion in my system so am close but i cant figure out how to access this data
so what am doing getting users to suggest and their mutual friends but i only want suggested user and as for mutual friend i just want to count them 
Here is the data to see it correctly i had to view in in page source it displays nice that way:
Code:
          stdClass Object
(
    [suggestedUser] => Array
        (
            [0] => Array
                (
                    [user_id] => 11
                    [firstname] => Siphe
                    [lastname] => Jali
                    [username] => siphe4573
                    [email] => [email protected]
                    [dob] => 01/01/2001
                    [gender] => Male
                    [address] => 15 Western Road, Port Elizabeth Central, Port Elizabeth, South Africa
                    [cellno] =>
                    [hobby] => Drama
                    [cover_image] => Sipheeye-for-ebony-399310-unsplash.jpg
                    [image] => mink-mingle-1381133-unsplash.jpg
                    [video_descryption] =>
                    [validation_code] => 0
                    [is_activated] => 1
                    [acount_created_at] => 2019-08-11 15:00:01.000
                    [last_active] => 2019-09-25 15:19:31.000
                    [logout_time] => 2019-09-25 15:45:16.000
                    [password] => $2y$10$QjzY5VsfdPUqGGVSyDd63.CNlXHlZJsHPbUN16B0c6D0RY1G4i2vu
                    [job_name] =>
                    [job_title] =>
                    [bio] =>
                    [music] =>
                    [movies] =>
                    [books] =>
                    [animals] =>
                    [post_num] => 1
                    [blocked] => 0
                    [closed] => 0
                    [online] => 1
                    [friendArray] => ,bonga6367
                )

        )

    [mutualFriends] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [user_id] => 20021
                            [firstname] => Bongani
                            [lastname] => Phamela
                            [username] => bonga6367
                            [email] => [email protected]
                            [dob] => 02/12/1992
                            [gender] => Trans Person
                            [address] => 485 Govan Mbeki Avenue, North End, Port Elizabeth, South Africa
                            [cellno] =>
                            [hobby] => Coding, UI/UX designing and gaming..
                            [cover_image] => 382694-large-sniper-wallpaper-1920x1080-windows-xp.jpg
                            [image] => 281850-beautiful-gaming-wallpaper-2560x1440.jpg
                            [video_descryption] =>
                            [validation_code] => 0
                            [is_activated] => 1
                            [acount_created_at] => 2019-09-02 21:47:03.000
                            [last_active] => 2019-09-27 12:57:41.000
                            [logout_time] => 2019-09-27 19:00:09.000
                            [password] => $2y$10$fyPLhL0KPcg26gyAaGtiAuyGGnmWcxadM7vlOJaRKmoetNri8lSbe
                            [job_name] => Software Developer
                            [job_title] => Junior Software dev
                            [bio] => This is my bio #test @bonga6367 http://www.helo.com
                            [music] => Hip Hop, House, RnB
                            [movies] => Rocky, fast and firious
                            [books] => our lady of benony, red blanket
                            [animals] => Dog, Cat, Elephant, horse, lion
                            [post_num] =>
                            [blocked] => 0
                            [closed] => 0
                            [online] => 1
                            [friendArray] => ,topollo57,siphe4573,nthethe5933,kabelo3088
                        )

                )

        )

)
stdClass Object
(
    [suggestedUser] => Array
        (
            [0] => Array
                (
                    [user_id] => 13
                    [firstname] => Kabelo
                    [lastname] => Thalent
                    [username] => kabelo3088
                    [email] => [email protected]
                    [dob] => 02/02/1993
                    [gender] => Trans Female
                    [address] => Openbaring Crescent, Kamma Park, Port Elizabeth, South Africa
                    [cellno] =>
                    [hobby] =>
                    [cover_image] => Kabelocup_spray_drops_124059_1280x720.jpg
                    [image] => kal-visuals-644747-unsplash.jpg
                    [video_descryption] =>
                    [validation_code] => 0
                    [is_activated] => 1
                    [acount_created_at] => 2019-08-20 08:12:02.000
                    [last_active] => 2019-09-26 23:42:54.000
                    [logout_time] => 2019-09-26 17:23:47.000
                    [password] => $2y$10$EsrUNveU.vJcLkV0DExDnOYqPArnjCmThIhf1ZA3h7Bz515mw8.ti
                    [job_name] =>
                    [job_title] =>
                    [bio] =>
                    [music] =>
                    [movies] =>
                    [books] =>
                    [animals] =>
                    [post_num] => 2
                    [blocked] => 0
                    [closed] => 0
                    [online] => 1
                    [friendArray] => ,bonga6367
                )

        )

    [mutualFriends] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [user_id] => 20021
                            [firstname] => Bongani
                            [lastname] => Phamela
                            [username] => bonga6367
                            [email] => [email protected]
                            [dob] => 02/12/1992
                            [gender] => Trans Person
                            [address] => 485 Govan Mbeki Avenue, North End, Port Elizabeth, South Africa
                            [cellno] =>
                            [hobby] => Coding, UI/UX designing and gaming..
                            [cover_image] => 382694-large-sniper-wallpaper-1920x1080-windows-xp.jpg
                            [image] => 281850-beautiful-gaming-wallpaper-2560x1440.jpg
                            [video_descryption] =>
                            [validation_code] => 0
                            [is_activated] => 1
                            [acount_created_at] => 2019-09-02 21:47:03.000
                            [last_active] => 2019-09-27 12:57:41.000
                            [logout_time] => 2019-09-27 19:00:09.000
                            [password] => $2y$10$fyPLhL0KPcg26gyAaGtiAuyGGnmWcxadM7vlOJaRKmoetNri8lSbe
                            [job_name] => Software Developer
                            [job_title] => Junior Software dev
                            [bio] => This is my bio #test @bonga6367 http://www.helo.com
                            [music] => Hip Hop, House, RnB
                            [movies] => Rocky, fast and firious
                            [books] => our lady of benony, red blanket
                            [animals] => Dog, Cat, Elephant, horse, lion
                            [post_num] =>
                            [blocked] => 0
                            [closed] => 0
                            [online] => 1
                            [friendArray] => ,topollo57,siphe4573,nthethe5933,kabelo3088
                        )

                )

        )

)
i only want suggestedUser, and count mutual friend or just suggestedUser will be fine.
Please help
Thanks in advance.

Here is the code that produced this data:

PHP Code:
      public function getUser($uid){
    
    $this->db->where('user_id'$uid);
    
    $query $this->db->get('users');
    
    return $query->result_array();
    
  }

    
  public function getFriendSuggestions() {
    
    $userId $this->session->userdata('user_id');
    
    $friends $this->getFriends($userId);
    
    $suggestedFriends = [];
    
    
        
foreach ($friends as $friendId) {
    
      # Friends friends list.
    
      $ff_list $this->getFriends($friendId);
    
      
          
foreach ($ff_list as $ffriendId) {
    
        # If the friendsFriend(ff) is not us, and not our friend, he can be suggested
    
        if ($ffriendId != $userId && !in_array($ffriendId$friends)) {
    
          
              
# The key is the suggested friend
    
          $suggestedFriends[$ffriendId] = ['mutual_friends' => []];
    
          $ff_friends $this->getFriends($ffriendId);
    
          
              
foreach ($ff_friends as $ff_friendId) {
    
            if (in_array($ff_friendId$friends)) {
    
              # If he is a friend of the current user, he is a mutual friend
    
              $suggestedFriends[$ffriendId]['mutual_friends'][] = $ff_friendId;
    
            }
    
          }
    
          
            
}
    
      }
    
      
        
}
    
    
        
# Convert the friend id's to user objects.
    
    $suggestedFriendObjs = array();
    
    if (!empty($suggestedFriends)) {
    
      foreach ($suggestedFriends as $suggestedFriend => $mutualFriends) {

    
        $suggestedFriendObj = new stdClass();
    
        $suggestedFriendObj->suggestedUser $this->getUser($suggestedFriend);
    
        
            
if (!empty($mutualFriends)) {
    
          $mutualFriendObjs = [];
    
          foreach ($mutualFriends['mutual_friends'] as $mutualFriend) {
    
            $mutualFriendObjs[] =  $this->getUser($mutualFriend);
    
          }
    
        }
    
        
            $suggestedFriendObj
->mutualFriends $mutualFriendObjs;
    
        $suggestedFriendObjs[] = $suggestedFriendObj;
    
      }
    
    }
    
    
        
return $suggestedFriendObjs;
    
  }

    } 
Reply
#2

Ok i got this far 
$data['suggested'] = $this->User_model->getFriendSuggestions();

$data['suggest'] = array(
'suggestedUser' => $data['suggested'][0]->suggestedUser,
'mutualFriend'  => $data['suggested'][0]->mutualFriends,
);


in my view i did this
<?php
    foreach ($suggest['suggestedUser'] as $test) {
       print_r($test);
   }
?>


i get one user but i have 2 users in the array
any input please
Thank you
Reply
#3

hello, i am working on arrays couple months now and if i want to manage/change/delete/etc the easiest way it to encode it to json_encode("The array here"), and then its easy Tongue
Reply
#4

By default, arrays start with index 0. So 0 is the key of the first element in an array, 1 for the next element and so on.

In your code:
PHP Code:
$data['suggest'] = array(
'suggestedUser' => $data['suggested'][0]->suggestedUser,
'mutualFriend'  => $data['suggested'][0]->mutualFriends,
); 
you only put the first element [0] in the 'suggestedUser' and 'mutualFriend' elements. That's why you only get one user.
Reply
#5

(This post was last modified: 10-01-2019, 05:30 PM by tp45.)

(10-01-2019, 09:48 AM)Wouter60 Wrote: By default, arrays start with index 0. So 0 is the key of the first element in an array, 1 for the next element and so on.

In your code:
PHP Code:
$data['suggest'] = array(
'suggestedUser' => $data['suggested'][0]->suggestedUser,
'mutualFriend'  => $data['suggested'][0]->mutualFriends,
); 
you only put the first element [0] in the 'suggestedUser' and 'mutualFriend' elements. That's why you only get one user.
Ok i see but how to get 0 and 1, i tried to remove the 0s but i get an error 
anyway thanks for replying .

i will keep trying to figure it out.

(10-01-2019, 06:05 AM)mboufos Wrote: hello, i am working on arrays couple months now  and if i want to manage/change/delete/etc the easiest way it to encode it to json_encode("The array here"), and then its easy Tongue
Thanks @mboufos for replying.
let me try this way too
Reply
#6

I would do it like this:
Controller:
PHP Code:
$data['suggested'] = $this->User_model->getFriendSuggestions(); 

View:
PHP Code:
Foreach($suggested['suggestedUser'] as $s_user)
{
    Print_r($s_user);   //check the output of $s_user, is it an array?


Maybe you're making thinks over complicated by using the same name for the array and for elements in the array. One way to deal with this is using the plural form for the array itself, e.g. suggestedUsers. Then it's clear that there can be more than one.
Reply
#7

(This post was last modified: 10-02-2019, 05:20 AM by tp45.)

Ok it worked this way

PHP Code:
Controller
$data
['suggested'] = $this->User_model->getFriendSuggestions();
View
<?php foreach ($suggested as $users): ?>
            <?php print_r($users->suggestedUser); ?>
            <?php //print_r($users->mutualFriends); ?>
          <?php endforeach ?>
Thank you for helping me and by replying
now it gives me all users as it should, i just have display each user
Reply
#8

(10-02-2019, 05:16 AM)tp45 Wrote: Ok it worked this way

PHP Code:
Controller
$data
['suggested'] = $this->User_model->getFriendSuggestions();
View
<?php foreach ($suggested as $users): ?>
            <?php print_r($users->suggestedUser); ?>
            <?php //print_r($users->mutualFriends); ?>
          <?php endforeach ?>
Thank you for helping me and by replying
now it gives me all users as it should, i just have display each user

Just trying to offer advice to make your life easier by saying you don't have to enclose each line of the above in PHP tags. In other words, the following will work and might be easier to read (not to mention type).

PHP Code:
<?php 
foreach ($suggested as $users): 
    print_r($users->suggestedUser); 
    //print_r($users->mutualFriends); 
endforeach; ?>
Reply
#9

(10-02-2019, 11:48 AM)dave friend Wrote: Just trying to offer advice to make your life easier by saying you don't have to enclose each line of the above in PHP tags. In other words, the following will work and might be easier to read (not to mention type).

PHP Code:
<?php 
foreach ($suggested as $users): 
    print_r($users->suggestedUser); 
    //print_r($users->mutualFriends); 
endforeach; ?>
Thank you for the advice @Dave .
Reply




Theme © iAndrew 2016 - Forum software by © MyBB