looking for similar data with like - codeigniter

In my controller, I send a string to my model. when I send that, the string is encoded and it makes problem in my query because I use LIKE Operator.
controller :
$this->base_model->get_post('football');
model:
function get_post($string){
$this->db->select('*');
$this->db->like('title' , $string , 'both');
$query = $this->db->get('post');
return $query->result();
}
In the model when I echo the $string it becomes to %D9%81%D9%88%D8%AA%D8%A8%D8%A7%D9%84 not football so the query can't fetch any data from my database

Decode it.
function get_post($string){
$decrypt = $this->encryption->decode($string);
$this->db->select('*');
$this->db->like('title' , $decrypt , 'both');
$query = $this->db->get('post');
return $query->result();
}

Related

fetching data of joining tables in Codeigniter

can you please tell me what's wrong with my function
public function get_by(){
$this->db->select('*');
$this->db->from('filiere');
$this->db->join('module', 'module.code_filiere = filiere.code_filiere');
$query = $this->db->get();
}
I wanna display Two tables in one table using the foriegn key (code_filiere)
Solution 1 : You need to return data for controller
public function get_by(){
$this->db->select('*');
$this->db->from('filiere');
$this->db->join('module', 'module.code_filiere = filiere.code_filiere');
$query = $this->db->get();
return $query->result();
}
In the controller
$data = $this->your_model->get_by();
Solution 2 : You need to return data for controller
public function get_by(){
$this->db->select('*');
$this->db->from('filiere');
$this->db->join('module', 'module.code_filiere = filiere.code_filiere');
$query = $this->db->get();
return $query;
}
In the controller
$data = $this->your_model->get_by()->result();
From the doc
nothing wrong in your function, but you didn't return the captured value from the function to the controller.
You should use
$query->result() for multi-value
OR
$query->row() for single value
from database
update your function
public function get_by(){
$this->db->select('*');
$this->db->from('filiere');
$this->db->join('module', 'module.code_filiere = filiere.code_filiere');
$query = $this->db->get();
return ($query->num_rows() > 0) ? $query->result() : false;
}

Codeigniter get database value in controller

This is my controller
function index($id=NULL)
{
$this->load->model('article');
$id= $this->uri->segment(3);
$data = array('article' => $this->article->show_a_article($id),
'sidebar' => $this->article->side_bar(9),
'related'=> $this->article->related_keyword($article_title),
);
echo $article->News_News_ID;
$this->load->view('page/baiviet',$data);
}
I want to get article_title value from table article and put it in related_workword() function.
Update model The show a article model is to get article detail. The second model is to get all article related to the chosen article.
function show_a_article($id)
{
$query= $this->db->get_where('news_news', array('News_News_ID'=>$id));
return $query->row();
}
function related_keyword($rkey)
{
$sql="SELECT `News_News_ID`,`News_News_Headline`,`News_News_thumb1` FROM `news_news` WHERE `news_tags` like '%$rkey%' ORDER BY `News_News_ID` desc LIMIT 10";
$query= $this->db->query($sql);
return $query->result();
}
Update: Igot it
echo $this->article->show_a_article($id)->News_News_Headline;
Call model method from controller
$article_title = $this->article->get_article_details($id)->article_title;
Add function in model to return data.
function get_article_details($id)
{
$query = 'select article_title from table where col = '.$this->db->escape($id);
return $this->db->query($query)->row();
}

how to fetch all records from order table in Codeigniter

$data is an array which contain user post data for fetch record from orders table
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$this->db->get();
$data = array(
'customer_id' => $this->input->post('custId')],
'paided' => 2
);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$this->db->get();
try this :
public function function_name (){
$data = array (
'customer_id' => $this->input->post('custId'),
'paided' => 2
);
$this->db->select('*');
$this->db->from('ordere');
$this->db->where($data);
$query = $this->db->get();
return $query->result_array();
}
You have done all good just need to put result() if you get multiple row or row() if you get one row
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$result= $this->db->get()->result(); //added result()
print_r($result);
as simple use
$custId = $_post['custId'];
$query = $this->db->query("SELECT * FROM orders WHERE customer_id= '$custId' AND paided='2'");
$result = $query->result_array();
return $result;//result will be array
This is the plus of using framework, you don't need to write that much of code,
$where = array('customer_id' => $this->input->post('custId'),'paided'=>2)
$result = $this->db->get_where('orders', $where);
and for fetching them, use $result->row() for single record retrieval.
If you want all records, use $result->result()
Here is documentation link, if you want to learn more.
What You should need to be Correct And Why
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*'); // by defaul select all so no need to pass *
$this->db->from('orders');
$this->db->where($data);
$this->db->get(); // this will not return data this is just return object
So Your Code Should be
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select(); // by defaul select all so no need to pass *
$this->db->from('orders');
$this->db->where($data);
$query = $this->db->get();
$data = $query->result_array();
// or You can
$data= $this->db->get()->result_array();
here result_array() return pure array where you can also use result()
this will return array of object

Codeigniter: Extract partial data from a query

I have a small question pls:
Let's say i have a long query:
$this->db->select('*')
->from('x')
->join(...)
->where('y >',$id)
->where('z <',$nr)
->where(...)
...
How can i put into a different variable all where statements so i can use them individually but only using one single query?
Thank you.
You need to create different model methods. Each method should have the where query outputting results. In your controller, you call each of these methods and store the results in variables.
Why not do this:
Controller:
function controller_method(){
$res1 = $this->model_name->get_where_results1($id, $nr);
$res2 = $this->model_name->get_where_results2($id, $nr);
$res3 = $this->model_name->get_where_results3($id, $nr);
}
Model:
function get_where_results1($id, $nr) {
$this->db->select('*')
->from('x')
->join("")
->where("");
$query = $this->db->get();
return $query->result();
}
function get_where_results2($id, $nr) {
$this->db->select('*')
->from('x')
->join("")
->where("");
$query = $this->db->get();
return $query->result();
}
function get_where_results3($id, $nr) {
$this->db->select('*')
->from('x')
->join("")
->where("");
$query = $this->db->get();
return $query->result();
}
UPDATE - if you wanted to use filters, i would this way:
Controller:
function controller_method() {
$res1 = $this->model_name->model_method($filters);
}
Model:
function model_method($filters) {
$this->db->select('*');
$this->db->from('x');
$this->db->join("");
if ($filters['filt1']) {
$this->db->where("something", $filters['filt1']);
}
if ($filters['filt2']) {
$this->db->where("something", $filters['filt2']);
}
$query = $this->db->get();
return $query->result();
}

how to use activerecord count_all_result()? with specific column?

I want to get count of specific column of table using codeigniter active record, something like this:
$this->db->count_all_results('t.column1');
so how can i change this db_active_record function? for any help thanks.
To count a specific column, You would do this;
$query = $this->db->get_where('table', array('column' => $column);
return $query->num_rows(); // This is what you're after...
create the query in your model, load it in your controller and then just count the array items in the view.
for example:
Model:
function getAll() {
$this->db->select('*');
$this->db->order_by('user_ad', 'asc');
$query = $this->db->get('users');
if ($query->num_rows() > 0) {
return $query->result();
}
}
controller:
$this->load->model('Users');
$data['allusers'] = $this->Users->getAll();
this->load->view('users', $data);
and in your view you can use
count($allusers);

Resources