Jump to content

Using Json_Decode To Create An Array


Xtremer360

Recommended Posts

I'm trying to figure out why when it shows the listing of attachments when its not NULL that it displays them as a string and NOT as an array. That way after it checks to see if its a file and returns TRUE for the function then it makes it array after all the attachments have been gone through.

 

$attachments = json_decode($attachments, TRUE);
for ($x = 0; $x < count($attachments); $x++)
{
   $file_name = $attachments[$x];
   if ($this->functions_model->is_file('assets/downloads/'.$file_name, FALSE) === TRUE)
   {
    $message_data->attachments = $file_name;           	
   }  
}

Link to comment
https://forums.phpfreaks.com/topic/269345-using-json_decode-to-create-an-array/
Share on other sites

I've also tried this however its not adding the second key is_file regardless of if its TRUE or FALSE

$attachments = json_decode($attachments, TRUE);
               for ($x = 0; $x < count($attachments); $x++)
               {
                   $file_name = $attachments[$x];
                   if ($this->functions_model->is_file('assets/downloads/'.$file_name, FALSE) === TRUE)
                   {
                       $attachments[$x]['is_file'] = TRUE;
                   }
                   else
                   {
                       $attachments[$x]['is_file'] = FALSE;
                   }
               }

I have a better question that deals with this sort of issue. I'm getting an error that consists of...

 

 

A PHP Error was encountered

 

Severity: Notice

Message: Undefined property: stdClass::$datetime_sent

Filename: models/messages_model.php

Line Number: 72

 

A PHP Error was encountered

 

Severity: Notice

Message: Undefined property: stdClass::$sender_avatar

Filename: models/messages_model.php

Line Number: 74

 

There's a var_dump of an array at the bottom of my code. This is also included next.

 

array(4) {

[0]=>

object(stdClass)#29 (8) {

["message_id"]=>

string(1) "1"

["subject"]=>

string(12) "Test Message"

["datetime_sent"]=>

string(6) "1 week"

["attachments"]=>

NULL

["message_content"]=>

string(446) "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

["sender_name"]=>

string(16) "Jeffrey Davidson"

["sender_email_address"]=>

string(20) "[email protected]"

["sender_avatar"]=>

string(82) "http://dev.kansasoutlawwrestling.com/assets/themes/supr/images/avatars/avatar5.jpg"

}

[1]=>

object(stdClass)#30 (8) {

["message_id"]=>

string(1) "2"

["subject"]=>

string(18) "Testing PM Message"

["datetime_sent"]=>

string(19) "2012-09-22 18:27:25"

["attachments"]=>

string(37) "["file1.jpg","file2.jpg","file3.jpg"]"

["message_content"]=>

string(51) "This is jsut a test of the personal message system!"

["sender_name"]=>

string(11) "Frank Scott"

["sender_email_address"]=>

string(24) "[email protected]"

["sender_avatar"]=>

NULL

}

[2]=>

object(stdClass)#31 (8) {

["message_id"]=>

string(1) "3"

["subject"]=>

string(16) "Testing Whatever"

["datetime_sent"]=>

string(19) "2012-10-04 05:03:09"

["attachments"]=>

NULL

["message_content"]=>

string(11) "dak;fdaf;ld"

["sender_name"]=>

string(11) "Frank Scott"

["sender_email_address"]=>

string(24) "[email protected]"

["sender_avatar"]=>

NULL

}

[3]=>

object(stdClass)#32 (3) {

["attachments"]=>

array(3) {

[0]=>

array(2) {

["file_name"]=>

string(9) "file1.jpg"

["is_file"]=>

bool(false)

}

[1]=>

array(2) {

["file_name"]=>

string(9) "file2.jpg"

["is_file"]=>

bool(false)

}

[2]=>

array(2) {

["file_name"]=>

string(9) "file3.jpg"

["is_file"]=>

bool(false)

}

}

["datetime_sent"]=>

string(8) "42 years"

["sender_avatar"]=>

string(81) "http://dev.kansasoutlawwrestling.com/assets/themes/supr/images/avatars/avatar.jpg"

}

}

 

 

/**
    * Gets all or last $x number of personal messages of the specified user
    *
    * @param integer $user_id User ID of the user specified
    * @param integer $limit Limit of how many messages to retrieve
    * @return object/NULL
    */
   public function get_personal_messages($user_id, $limit = NULL, $timezone)
   {
       $this->db->select('personal_messages.message_id');
       $this->db->select('personal_messages.subject');
       $this->db->select('personal_messages.datetime_sent');
       $this->db->select('personal_messages.attachments');
       $this->db->select('personal_messages.message_content');
       $this->db->select('CONCAT(users.first_name, " ", users.last_name) AS sender_name', FALSE);
       $this->db->select('users.email_address AS sender_email_address');
       $this->db->select('user_profiles.user_avatar AS sender_avatar');
       $this->db->from('personal_messages');
       $this->db->join('users', 'users.user_id = personal_messages.from_user_id');
       $this->db->join('user_profiles', 'users.user_id = user_profiles.user_id');
       $this->db->where('personal_messages.to_user_id', $user_id);
       if ($limit != NULL)
       {
           if (is_numeric($limit))
           {
               $this->db->limit($limit);
           }
       }
       $query = $this->db->get();
       $personal_messages = $query->result();
       if (count($personal_messages) > 0)
       {
           for ($x = 0; $x < count($personal_messages); $x++)
           {
               $attachments = $personal_messages[$x]->attachments;
               if ($this->functions_model->null_check($attachments) === FALSE)
               {
                   $attachments = json_decode($attachments, TRUE);
                   for ($x = 0; $x < count($attachments); $x++)
                   {
                       $file_name = $attachments[$x];
                       if ($this->functions_model->is_file('assets/downloads/'.$file_name, FALSE) === TRUE)
                       {
                           $attachments[$x] = array('file_name' => $file_name, 'is_file' => TRUE);   				     
                       }
                       else
                       {
      			         $attachments[$x] = array('file_name' => $file_name, 'is_file' => FALSE);    
      				 }
      			 }
      			 $personal_messages[$x]->attachments = $attachments;
      		 }
               $personal_messages[$x]->datetime_sent = $this->functions_model->actual_time('d F Y g:i a', $timezone, strtotime($personal_messages[$x]->datetime_sent));
               $avatar = $this->functions_model->site_url().'assets/themes/'.$this->config->item('default_theme').'/images/avatars/avatar.jpg';
               if ($this->functions_model->null_check($personal_messages[$x]->sender_avatar) === FALSE)
               {
                   if ($this->functions_model->is_file('assets/themes/supr/images/avatars/'.$personal_messages[$x]->sender_avatar, FALSE) === TRUE)
                   {
                       $avatar = $this->functions_model->site_url().'assets/themes/'.$this->config->item('default_theme').'/images/avatars/'.$personal_messages[$x]->sender_avatar;
                   }
               }
               $personal_messages[$x]->datetime_sent = $this->functions_model->time_since(strtotime($personal_messages[$x]->datetime_sent));
               $personal_messages[$x]->sender_avatar = $avatar;
           }
       }
       echo '<pre>';
       var_dump($personal_messages);
       echo '</pre>';
       die();
       return $personal_messages;
   }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.