retrieving array as comma separated values in codeigniter - php

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,
pr_notifications table is as below:
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';
}

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.

You want to store $ids comma separated? then use implode().
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(",",$arr);
I hope this will help.

Related

insert batch in codeigniter, if no duplicates in specific column

I have a table in my database. The table structure is as follows
I need to insert data to this table as a batch, but avoid duplicate entry (here duplicate means same combination of intCampaignID and intMobileNumber).
The following is the insert function in my model
function addToCampaign($data){
$voiceengine = $this->load->database('voiceengine', TRUE);
$scheduleData = array();
$mobiles = explode(',', $data['destinations']);
$mobilesData = array();
foreach($mobiles as $mobile){
if (trim(strlen($mobile)) > 10)
$mobile = substr(trim($mobile), -10);
else
$mobile = trim($mobile);
if(!in_array($mobile, $mobilesData)){
array_push($mobilesData, $mobile);
}
}
foreach($mobilesData as $mobile){
$campaignData = array(
'intCampaignID' => $data['campaign'],
'intMobileNumber' => $mobile);
array_push($scheduleData, $campaignData);
}
$voiceengine->insert_batch('Voice.CampaignNumbers', $scheduleData);
}
I am not sure how to do this. I searched for a solution, but all the solutions are avoiding duplicates based on key. But these fields are not key fields and I have no privilege to change the structure. Someone please help me to do this
UPDATE
I have updated my function in model
function addToCampaign($data){
$voiceengine = $this->load->database('voiceengine', TRUE);
$scheduleData = array();
$mobiles = explode(',', $data['destinations']);
$mobilesData = array();
foreach($mobiles as $mobile){
if (trim(strlen($mobile)) > 10)
$mobile = substr(trim($mobile), -10);
else
$mobile = trim($mobile);
if(!in_array($mobile, $mobilesData)){
array_push($mobilesData, $mobile);
}
}
foreach($mobilesData as $mobile){
$voiceengine->where('intCampaignID', $data['campaign']);
$voiceengine->where('intMobileNumber', $mobile);
$count = $voiceengine->count_all_results('Voice.CampaignNumbers'); // need a simple method
if($count <= 0) {
$campaignData = array(
'intCampaignID' => $data['campaign'],
'intMobileNumber' => $mobile);
array_push($scheduleData, $campaignData);
}
}
$voiceengine->insert_batch('Voice.CampaignNumbers', $scheduleData);
}
The updated function will work. But I am looking for a simple solution
Before inserting remove the duplicate values :
$scheduleData = array_map("unserialize", array_unique(array_map("serialize", $scheduleData)));
and then run the insert_batch function

explode function in codeigniter

I am new to programming , i have a problem while using explode function ie, after i explode the value and pass those value to a model to retrive records from db only the last value from the exploded data is showing.
ex i have impoded data as (test,test1,test2) after explode the data is like(testtest1test2) here i pass these value to model to take records from a table where a field name is equal to the exploded value.I dont't know how to explain this properly please forgive me.
public function location($id) {
$query = $this->gt_pav_model->select($id);
$data['selectdata'] = $query->result();
foreach( $data['selectdata'] as $s) {
$explode = $s->package_id; // this is to get imploded data from db
}
$t = explode(",", $explode);
foreach( $t as $tt) {
$query = $this->gt_pav_model->select_pa($tt);
$data['dataa'] = $query->result();
$this->load->view('pav/pavdetails',$data);
}
}
public function location($id) {
$final_result = array();
$result= $this->gt_pav_model->select($id)->result();
foreach( $result as $key=> $re) {
$explode = $re->package_id;
$t = explode(",", $explode);
$param = $t[1];
$datas = $this->gt_pav_model->select_pa($param)->result();
$final_result[$key] => $datas;
}
$this->load->view('pav/pavdetails',array('datas'=>$final_result));
}
You can print result in your view page <?php print_r($datas); ?>

Remove Duplicated Entries From an Array

For removing the duplicated entries I know I have to use array_unique() but unfortunately by using this I got unexpected result(s). My PHP code is this:
$query = $db->query("
SELECT sid,father_contact,residential_contact
FROM ".TABLE_PREFIX."student_list
{$where_clause}
ORDER BY sid ASC");
while ($s = $db->fetch_array($query))
{
if (!empty($s['father_contact']))
{
$father_contact = $s['father_contact'].', ';
}
else
{
$father_contact = '';
}
if (!empty($s['residential_contact']))
{
$residential_contact = $s['residential_contact'].', ';
}
else
{
$residential_contact = '';
}
$phone_nums_bit .= $father_contact.$residential_contact;
}
This grabs all phone numbers from the database tables and print the result like this:
03334523675, 03124237009, 03134237002, 03124237009, 03217832173, 03134237002, 3134237002, 03124237009,
Notice that few numbers like 03134237002 is repeating thrice. I want to remove duplicate entries from this result. Please help me to sort it out. Thanks.
You can try to do it via array structures. Just to give you an idea:
$phones = [];
while ($s = $db->fetch_array($query))
{
if (!empty($s['father_contact']))
{
$phones[] = $s['father_contact'];
}
if (!empty($s['residential_contact']))
{
$phones[] = $s['residential_contact'];
}
}
$phones = array_unique($phones);
// and then combine into string
$phone_nums_bit = implode(', ', $phones);

How to create custom CSV files using PHP?

In the $csv_array_attributes[0] represents every column for my csv file. sku is the product id. In $csv_array_attributes[0] represents all of the product ids for my csv(which is the rows value form column sku). In the query below I extract all of the products ids, attributes names and attributes values. My problem is that I have to compute my $csv array in order to assign for each attribute name(columns) the attribute values. In $final_string somehow I want to memorize all of the attributes values for a products (see the commented part). Thx in advance for the help. Im really stuck with this :(
$csv_array_attributes[0] = "sku%%".$header;
//GETTING ATTRIBUTES VALUES
$i = 1;
foreach($xml->children() as $content){
$id_produs = $content->ProductCode;
$csv_array_attributes[$i] = $id_produs;
$i++;
}
$select = mysql_query("SELECT id_prod.id_produs, GROUP_CONCAT('^^',nume_attr) AS nume_at, GROUP_CONCAT('^^',val_attr) AS val_at FROM attributes
INNER JOIN id_prod
ON id_prod.id_id_produs = attributes.id_produs
GROUP BY id_prod.id_produs");
$i = 0; $id_prod = array();
while ($row = mysql_fetch_array($select)){
$id_produs = $row['id_produs'];
$nume_attr = explode(",^^",substr($row['nume_at'],2));
$val_attr = explode(",^^",substr($row['val_attr'],2));
$id_prod[$id_produs] = $nume_attr;
$i++;
}
// var_dump(count($id_prod));
$nume = explode("%%", $csv_array_attributes[0]);
$csv[0] = $csv_array_attributes[0];
$i = 1;
foreach ($id_prod as $key => $value) {
// comment part
//$final_string = "";
//if (attr_name has attribute_value for product i ){
// $final_string.= attribute_value."%%";
//}else{
// $final_string.= "%%";
//}
//end comment part
$csv[$i] = $csv_array_attributes[$i]."%%".$final_string;
$i++;
}
//var_dump($csv_array_attributes[0])
//CREARE CSV
$file = fopen("attributes.csv","w+");
foreach ($csv as $line){
fputcsv($file,explode('%%',$line),"^","`");
}
To see the current result please click HERE
Actually I only can give you a hint how I would create a array for csv.
// header
$csv[0][0] = "one";
$csv[0][1] = "two";
$csv[0][2] = "three";
$csv[0][3] = "four";
$csv[0][4] = "five";
// body
$csv[1][0] = "some";
$csv[1][1] = "values";
$csv[1][2] = "that";
$csv[1][3] = "could";
$csv[1][4] = "be";
$csv[2][0] = "here";
$csv[2][1] = "in";
$csv[2][2] = "this";
$csv[2][3] = "little";
$csv[2][4] = "array";
This is only a sample to view the array setup.
If you have set up the header, lets say by a for you can fill the 2 demensional array.
If this is set up you can use a foreach to build your CSV
Sample
$seperator = ";";
$ln = "\r\n";
$csv_output = "";
foreach($csv as $c){
foreach($c as $value){
$csv_output .= $value . $seperator;
}
$csv_output . $ln;
}
csv output
one;two;three;four;five;
some;values;that;could;be;
here;in;this;little;array;
a row;with an;;empty;field;
empty fields are no problem since you seperate them all with a ;

php for each in while loop

I am trying to get specific data from a while loop and loop it x number of times.
I'm selecting this data:
$r=mysql_query("SELECT ac6, ac5, ac4, ac3, ac2, ac1, ac0 FROM advertisements WHERE token = '".$_GET['token']."'");
while ($adData = mysql_fetch_array($r, MYSQL_NUM))
{
$data = $adData;
$ac0 = $data['ac0'];
$ac1 = $data['ac0'];
print $ac0;
print $ac1;
}
This doesn't work. Nothing gets printed out.
What I want to do is to get ac6 to ac0 value for that specific advertisement (where token).
How can I do that?
Change your numeric fetch to an associative one, then add a foreach loop to process the result.
$r = mysql_query("SELECT ac6, ac5, ac4, ac3, ac2, ac1, ac0
FROM advertisements WHERE token = '" . $_GET['token'] . "'");
while ($adData = mysql_fetch_assoc($r))
{
foreach ($adData as $key => $value)
{
$nubmer = (int)substr($key, 2);
print $value; // or whatever you actually want to do
}
}
Also I hope you're validating $_GET['token'] against possible mischief in your code.
You can create an arrays to add the values from the result query
1st is to create an array:
$advert_AC0 = array();
$advert_AC1 = array();
$advert_AC2 = array();
$advert_AC3 = array();
$advert_AC4 = array();
$advert_AC5 = array();
$advert_AC6 = array();
Now, to add content to array
$r = mysql_query("SELECT ac6, ac5, ac4, ac3, ac2, ac1, ac0
FROM advertisements WHERE token = '" . $_GET['token'] . "'");
if(mysql_num_rows($r)){ //check 1st if there is num of rows from the result
while ($adData = mysql_fetch_assoc($r))
{
array_push($advert_AC0, $dData['ac0']);
array_push($advert_AC1, $dData['ac1']);
array_push($advert_AC2, $dData['ac2']);
array_push($advert_AC3, $dData['ac3']);
array_push($advert_AC4, $dData['ac4']);
array_push($advert_AC5, $dData['ac5']);
array_push($advert_AC6, $dData['ac6']);
}
}else{
echo "NO RESULT.";
}
to call for the array values, 1 by 1
$count_array = count($advert_AC0);
$i = $count_array;
while(1 <= $i){
echo "AC0: $advert_AC0[$i]<br>";
echo "AC1: $advert_AC1[$i]<br>";
echo "AC2: $advert_AC2[$i]<br>";
echo "AC3: $advert_AC3[$i]<br>";
echo "AC4: $advert_AC4[$i]<br>";
echo "AC5: $advert_AC5[$i]<br>";
echo "AC6: $advert_AC6[$i]<br>";
$i++;
}
I don't know if my answer solved your question, please comment if not.

Categories