Exlode in php for DataBase - php

I am passing the check box value in url like selected.php?aa=2,3, and i want get the 2 and 3 value from datebase when i explode its not working
$mainclass=$_GET['aa'];
$classarray = list($class) = explode(",", $mainclass);
$classa = implode(',', $classarray);
$makefeed = mysql_query("SELECT dbid FROM studentnew WHERE dbid IN ('".$classa."')");
while ($cc = mysql_fetch_array($makefeed)) {
// code
echo $cc['name'];
}

$mainclass = $_GET['aa'];
$classarray = explode(",", $mainclass);
$classarray = array_walk($classarray, 'intval');
$classa = implode(',', $classarray);
$makefeed = mysql_query("SELECT dbid FROM studentnew WHERE dbid IN (".$classa.")");

Related

How to extract data from bracket and replace in laravel

My string is
"invoice = [[invoice_no]]
customer = [[customer]]
item = [[item]]"
How can i do upper string to covert the following with php?
"invoice = " $data->invoice_no
"customer = " $data->customer
"item = " $data->item
I already created one function for same type of parsing, it replaces {{item}} by give data like $data['item']. here is example , you can change according to your requirement.
<?php
function putValuesInHTML($html_form,$reportData){
preg_match_all('/{{([A-Za-z0-9_ ]+?)}}/', $html_form, $fields);
$arrInHtml = array();
$valsForHtml = array();
foreach ($fields[1] as $key => $value) {
if(isset($reportData[$value])){
$arrInHtml[] = "{{".$value."}}";
$valsForHtml[] = $reportData[$value];
}
}
$result = str_replace($arrInHtml,$valsForHtml,$html_form);
/*Eval start*/
preg_match_all('/{{=([A-Za-z0-9+\-\/{}* ]+?)=}}/', $result, $evals);
$arrInHtml = array();
$valsForHtml = array();
foreach ($evals[1] as $key => $value) {
$eval = "00";
$arrInHtml[] = "{{=".$value."=}}";
eval("\$eval= ".$value.";");
$valsForHtml[] = $eval;
}
$reportHTML = str_replace($arrInHtml,$valsForHtml,$result);
return $reportHTML;
}
$str = "invoice = {{invoice_no}}
customer = {{customer}}
item = {{item}}";
$data = ["invoice_no"=>"111","customer"=>"Niklesh Raut","item"=>"iphone"] ;
$newStr = putValuesInHTML($str,$data);
echo $newStr;
?>
Live demo
Input :
invoice = {{invoice_no}}
customer = {{customer}}
item = {{item}}
Output for $data = ["invoice_no"=>"111","customer"=>"Niklesh Raut","item"=>"iphone"] :
invoice = 111
customer = Niklesh Raut
item = iphone

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,
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.

Dynamic Content in PHP array

This should be a quick one.
I'm pulling a list of id's and I need to place them in an array.
Here is my php code to get the list of id's
$get_archives = mysql_query("SELECT * FROM archive WHERE user = '$email' ");
while ($row = mysql_fetch_assoc($get_archives)) {
$insta_id = $row['insta_id'];
$insta_id = "'" . $insta_id."',";
echo $insta_id;
};
This echo's a list of id's that looks like this: '146176036','136514942',
Now I want to put that list into an array. So i tried something like this:
$y = array($insta_id);
However that isn't working. Any suggestions?
$y = array();
while ($row = mysql_fetch_assoc($get_archives)) {
$y[] = $row['insta_id'];
}
$myArray = array();
$get_archives = mysql_query("SELECT * FROM archive WHERE user = '$email' ");
while ($row = mysql_fetch_assoc($get_archives)) {
$insta_id = $row['insta_id'];
$insta_id = "'" . $insta_id."',";
$myArray[] =$insta_id;
};
did you mean like this: ?
$insta_id=array();
while ($row = mysql_fetch_assoc($get_archives)) {
$insta_id[] = $row['insta_id'];
}
Create an array, and push the values into it:
$values = array();
while ( $row = mysql_fetch_assoc( $get_archives ) ) {
array_push( $values, $row['insta_id'] );
}

Best way to update a large amount of items in a MySQL Database

I'd like to think that there's a simple way of doing this. I have over thirty columns in a database that I need to update. All of the columns have the same name as the correlating session variables.
Here's my code to fetch the array and assign variables:
while ($row = mysql_fetch_array($result))
{
$_SESSION['PendingCivil'] = $row['PendingCivil'];
$_SESSION['PendingAsbestos'] = $row['PendingAsbestos'];
$_SESSION['PendingDomestic'] = $row['PendingDomestic'];
$_SESSION['AsgNewCivil'] = $row['AsgNewCivil'];
$_SESSION['AsgNewAsbestos'] = $row['AsgNewAsbestos'];
$_SESSION['AsgNewDomestic'] = $row['AsgNewDomestic'];
$_SESSION['AsgTransferCivil'] = $row['AsgTransferCivil'];
$_SESSION['AsgTransferAsbestos'] = $row['AsgTransferAsbestos'];
$_SESSION['AsgTransferDomestic'] = $row['AsgTransferDomestic'];
$_SESSION['AsgReopenedCivil'] = $row['AsgReopenedCivil'];
$_SESSION['AsgReopenedAsbestos'] = $row['AsgReopenedAsbestos'];
$_SESSION['AsgReopenedDomestic'] = $row['AsgReopenedDomestic'];
$_SESSION['DispWOPCivil'] = $row['DispWOPCivil'];
$_SESSION['DispWOPAsbestos'] = $row['DispWOPAsbestos'];
$_SESSION['DispWOPDomestic'] = $row['DispWOPDomestic'];
$_SESSION['DispFinalCivil'] = $row['DispFinalCivil'];
$_SESSION['DispFinalAsbestos'] = $row['DispFinalAsbestos'];
$_SESSION['DispFinalDomestic'] = $row['DispFinalDomestic'];
$_SESSION['DispBTCivil'] = $row['DispBTCivil'];
$_SESSION['DispBTAsbestos'] = $row['DispBTAsbestos'];
$_SESSION['DispBTDomestic'] = $row['DispBTDomestic'];
$_SESSION['DispJTCivil'] = $row['DispJTCivil'];
$_SESSION['DispJTAsbestos'] = $row['DispJTAsbestos'];
$_SESSION['DispJTDomestic'] = $row['DispJTDomestic'];
$_SESSION['DispTOCivil'] = $row['DispTOCivil'];
$_SESSION['DispTOAsbestos'] = $row['DispTOAsbestos'];
$_SESSION['DispTODomestic'] = $row['DispTODomestic'];
$_SESSION['OldCivil'] = $row['OldCivil'];
$_SESSION['OldAsbestos'] = $row['OldAsbestos'];
$_SESSION['OldDomestic'] = $row['OldDomestic'];
}
Basically I'd like to update each row with a (possibly) changed session variable on an update page. Is there a way to iterate through the session variables and update the corresponding columns? Note: I have other session variables set that are not in the DB.
What about:
while ($row = mysql_fetch_assoc($result))
foreach($row as $k => $v)
$_SESSION[$k] = $v;
fetch them all to associative arrays
while ($row = mysql_fetch_assoc($result)){
foreach($row as $column_name => $data){
$_SESSION[$column_name] = $data;
}
}

Explode then Choose the Correct Email?

I have a string which looks like this:
email#domain1.com|email#domain5.com
I need only the email matching 'domain5'.
$domain_needle = 'domain5.com';
$employer_email = 'email#domain1.com|email#domain5.com';
$employer_email = explode('|', $employer_email);
How can I pick the array member which has the domain5?
Using substr_count()?
$domain_needle = 'domain5.com';
$employer_email = 'email#domain1.com|email#domain5.com';
$employer_email = explode('|', $employer_email);
foreach($employer_email as $email){
if(strpos($email, $domain_needle) !== false){
$employer_email = $email;
break;
}
}
echo $employer_email;
You could use preg_grep:
$result = preg_grep("/$domain_needle/", $employer_email);
Example, with array_shift to extract the first value from the returned array:
$domain_needle = 'domain5.com';
$employer_email = 'email#domain1.com|email#domain5.com';
$employer_email = explode('|', $employer_email);
print array_shift(preg_grep("/$domain_needle/", $employer_email));
Outputs:
email#domain5.com

Categories