codeigniter controller input Hebrew letters - php

I have a function in the controller that gets a string and then queries the database (via the model) for records that have this string as their name. This works fine with English but I have a problem when the input is in Hebrew. When I echo the string I see something like %D7%91 and the query fails.
All database tables entries are defined as utf8_general_ci.
My controller code:
function get_records_by_name($name)
{
echo 'searching for: '.$name.'</br>';
$keys = new DMkeys() ;
$query = $keys->get_keys();
$arr = array();
$count = 0;
foreach ($query->result() as $row)
{
if(stristr($row->name, $name) != FALSE)
{
$arr[] = $row->name;
$count++;
echo $row->name.'</br>';
}
}
$result = array('count' => $count, 'list' => $arr);
echo json_encode($result) ;
}
My model code:
function get_keys()
{
$query = $this->db->get('keys');
return $query;
}
Thanks,
Simon

You need to decode the variable before using it, try:
$name = urldecode($name);

Related

php pdo json encode array print

When I pull data from the hello database, it just writes the first line, not the other lines, what could be the reason?
I did not know much about php json but did not get any results.
İD 88 NOT FOUND
**database print **
[{"title":"facebook","contact":"facebook adres"},
{"title":"twitter","contact":"twitter adres"}]
database data
id:87 [{"title":"facebook","contact":"facebook adres"},
{"title":"twitter","contact":"twitter adres"}]
id:88
[{"title":"instagram","contact":"instagram adres"},
{"title":"google","contact":"google adres"}]
database function
function menu($menu_title)
{
global $db;
$query = $db->prepare('SELECT * FROM menu WHERE menu_title = :menu_title');
$query->execute([
'menu_title' => $menu_title
]);
$result = $query->fetchAll(PDO::FETCH_ASSOC);
if ($result) {
$data = [];
foreach ($result as $key => $value) {
$data += json_decode($value['menu_content'], true);
}
return $data;
}
return null;
}
socialmedia foreach
<?php foreach (menu('sosyalmedya') as $key => $menu): ?>
<?= $menu['title'] ?><br>
<?= $menu['contact'] ?><br>
<?php endforeach; ?>
You're only returning the first element of each JSON array. Append the whole array to the result.
foreach ($result as $value) {
$data = array_merge($data, json_decode($value['menu_content'], true));
}
BTW, don't use SELECT * if you only want the menu_content column. Use SELECT menu_content.

How to get data from database that result as an array in model codeigniter

I want to make a query that results like this in codeigniter MODEL:
$caldata = array (
15 => 'yes',
17 => 'no'
);
Is that possible to do?
Take NOTE: The 15,17 and yes,no are in the same database table.
There is no core helper function to achieve what you want in CI. But you can create your own helper function:
function pluck($arr = [], $val = '', $key = '')
{
// val - label for value in array
// key - label for key in array
$result = [];
foreach ($arr as $value) {
if(!empty($key)){
$result[$value[$key]] = $value[$val];
}else{
$result[] = $value[$val];
}
}
return $result;
}
you can use result_array() function so you can have something like:
$query = $this->db->select('id,answer')->from('users')->get();
$result = $query->result_array();
print_r($result);
After that you have your array and you can make the $key => $value relation of the fields
After a long search i found an answer. Sample way to do this:
$query = $this->db->select('start_date, class')->from('event')->like('start_date', "$year-$month", 'after')->get();
$datavariable = $query->result();
$caldata = array();
foreach($datavariable as $row){
$caldata[substr($row->start_date,8,2)] = $row->class;
}

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); ?>

Navigate an array from controller in Codeigniter

Good I'm starting with php and codeigniter, I have the following problem I'm running an array from my model, which is the result of a query, how can I read one by one my records in the controller?
function getCustomers() {
$sql = "SELECT * FROM customers";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$i = 0;
foreach($query->result() as $row) {
$img[$i]['id'] = $row->id;
$img[$i]['name'] = $row->name;
$img[$i]['Location'] = $row->Location;
$img[$i]['telephone'] = $row->telephone;
$i++;
}
return $img;
}
}
You can return array to the controller by loading the model into the controller like this.
$this->load->model('yourmodel');
$array = $this->yourmodel->yourmodelsmethod();
First you have to return this array to your controller like
$this->load->model('yourmodel');
$array['test'] = $this->yourmodel->yourmodelsmethod();
foreach($test as $key=>$value)
{
echo $value;
}
By doing this you can easily print your data from model in controller

Pass an array from controller to model in codeigniter

I am doing a project in Codeigniter. Here I fetch the latest 10 mails from my gmail id using imap.Here I want to take the from field of fetched mail and i want to check whether the from field of fetched mail is in my database table('clients'). Here I stored the 10 from field of fetched mail to array and passed it to model,where the checking is takingplace and returns the matching field name. But it is not working for me.
My controller function is:
if ($mbox = imap_open($authhost, $user, $pass)) {
$emails = imap_search($mbox, 'ALL');
$some = imap_search($mbox, 'SUBJECT "Suspicious sign in prevented"', SE_UID);
$MC = imap_check($mbox);
$inbox = $MC->Nmsgs;
$inboxs = $inbox - 9;
if ($emails) {
$data['overview'] = imap_fetch_overview($mbox, "$inboxs,$inboxs:$inbox", 0);
$i = 0;
foreach ($data['overview'] as $i => $val) {
$from[$i] = $val->from;
$i++;
}
$data['result'] = $this->crm_model->get_names($from);
foreach ($data['result'] as $row) {
echo $row->name;echo "<br/>";
}
}
imap_close($mbox);
}
And my model function is:
function get_names($from) {
$this->db->select('name');
$this->db->from('clients');
$this->db->where_in('name', $from);
$query = $this->db->get();
return $query->result();
}
But when I used the above model function like below, it returns the value
function get_names() {
$options = array(
'0' => 'Job Openings',
'1' => 'Offers',
'2' => 'Techgig',
'3' => 'Australia',
);
$this->db->select('name');
$this->db->from('clients');
$this->db->where_in('name', $options);
$query = $this->db->get();
return $query->result();
}
I think the problem is with passing value from controller to model. Can anyone help me. Thanks in advance
While executing any query in database using where_in (list of comma separated values),
the list of values should be like this array:
$options = array('Hello', 'World!', 'Beautiful', 'Day!');
Not like this:
$options = array('0' => 'Job Openings','1' => 'Offers','2' => 'Techgig','3' =>'Australia',);
to do this you should implode this array!
$opt=implode(",",$options);
and pass this $opt array to WHERE_IN()
Hope this will solve your problem !
You don't have to use $i..
if ($emails) {
$data['overview'] = imap_fetch_overview($mbox, "$inboxs,$inboxs:$inbox", 0);
// $i = 0;
foreach ($data['overview'] as $val) {
$from[] = $val->from;
//$i++;
}
$data['result'] = $this->crm_model->get_names($from);
foreach ($data['result'] as $row) {
echo $row->name;echo "<br/>";
}
}
Do these changes and check

Categories