Search code examples
phpmysqlcodeignitercodeigniter-2

retrieving array as comma separated values in codeigniter


I am using the below code to get the ids from the pr_users table and store it in pr_notification_table,but unable to store the values separated by comma into pr_notifications table. I want to store $notification_data['show_users'] as 1,2,3,4 etc so that notifications are sent to these ids. Its inserting NULL on executing this , I have attached table images also,enter image description here

pr_notifications table is as below:

enter image description here

My controller code is:

if($data['rows'][0]['last_status'] == 'Accepted')
			{
				$ids= '22';
			$data['success_message'] = $this->exit_common->send_notification_to_all_roles($ids);
			echo "Success";
			
			}

My model code is:

function send_notification_to_all_roles($ids)
		{
			
			global $USER;
			$post_arr = $this->input->post();
			 $this->db->select('g.*,id');
		$this->db->from('pr_users as g'); 
		 $this->db->where('userroleid', $ids); 
		//$this->db->join($this->myTables['pr_users_details'].' as ud','ud.userid = g.userid');
		//$this->db->join('pr_users_details as ud','ud.userid = g.userids');
		
	/*	$this->db->join($this->myTables['users_details'].' as ud','ud.userid = g.userid');
		$this->db->join('pr_resignation_type as gt','gt.id = g.sr_type');*/
		$query=$this->db->get();	
		
		$return	= $query->result_array();
		$arr = explode(',',$return);
		foreach($arr as $num)
		{
			echo $num."<br>";
			}
		print_r($num);
		die;
		
			$manager_id = $this->get_value_by_id('managerid','users',$this->session->userdata('admin_id'));
			$user_id='1';
					$v_memberid = $manager_id . "," . $user_id;
						//$manager_id = $this->get_value_by_id('managerid','users',$this->session->userdata('admin_id'));
					$notification_data['ref_table']			=	'pr_resignation_requests';	
					$notification_data['ref_id']			=	'1';
					$notification_data['modifier_id']		=	$USER->id;
					$notification_data['show_users']		=	$num;
					$notification_data['notification_descr']=	"A new Job has been created" ;//$manager_id;
					$notification_data['notification_text']	=	"A new Job has been created";
					$notification_data['added_on']			=	date("Y-m-d H:i:s");
					$notification_data['url']				=	'exits';
					$notification_data['uurl']				=	'exits';
					$this->db->insert($this->myTables['notifications'],$notification_data);
					return 'Resignation Request submitted successfully';
		}


Solution

  • I think you have to get notification_id from pr_users table, and then use the following code for get notification_id comma seprated.Assume than your notification id array is :- $user_notification_ids_info Now go with this code.

    $ids = ''; $notification_ids = '';
    for($i=0; $i<count($user_notification_ids_info); $i++)
    { 
    $ids = $user_notification_ids_info[$i]['notification_id'];
    $notification_ids.= $ids.", ";
    }
    $notification_ids = substr(trim($notification_ids), 0, -1);
    

    Now simply echo $notification_ids; it will return your comma seprated notification id. It will helps you try this one.