codeigniter search function is not working - php

I want to create simple search function. I follow some example. but I was unable to get results. please help me.
//control page
public function search(){
$key = $this->input->post('phone1',TRUE);
$data['ppp'] = $this->admin_model->searching($key);
$this->load->view('members/search_result',$data);
}
//Model page
public function searching($key){
$this->db->like('phone1',$key);
$query = $this->db->get('advertisement');
return $query->result();
}

try to print query
$query = $this->db->get('advertisement');
echo $this->db->last_query();exit;
try and check that query in your database is that giving result or not

Related

i want to display value from database in codeigniter but i am getting errror

I want to display a value in view in CodeIgniter but I am getting several errors like trying to get the property on non-object. I think my code is correct but I am getting errors. Below is my code.
controller:
public function trainer($id)
{
$user_record = $this->db->query("select * from usr_data where usr_id=$id")->result();
$data['title'] = 'trainer Dashboard';
$data['user_record'] = null;
$data['active_courses'] = [];
$data['inprogress'] = [];
if(count($user_record)) {
$user_record = $user_record[0];
$data['title'] = ucwords($user_record->firstname).' Dashboard';
$data['user_record'] = $user_record;
$active_courses = $this->base_model->getTrainercourseAll($id);
$data['active_courses'] = $active_courses;
$inprogress = $this->base_model->getstaffinprogress($id);
$data['inprogress'] = $inprogress;
}
$this->load->view('trainer-dashboard', $data);
}
model:
public function getstaffinprogress($user_id) {
$result=$this->executeSelectQuery("select AVG(m.percentage) from object_data o, ut_lp_marks m where o.obj_id=m.obj_id and o.type='crs' and m.status=1 ");
return $result;
}
view:
<h3>Avg inprogress:<?php echo "<span style='color:#ff00ff;font-family:verdana;'>".$inprogress->percentage."</span>";?></h3>
I want to display the column percentage which is coming from database.above code is in the controller, model and view.i thought my controller code is wrong.
Anyone help me to get rid of this error. I want to display a value in view in CodeIgniter but I am getting several errors like trying to get the property on non-object. I think my code is correct but I am getting errors. Below is my code.
Try this in your view file,
if(isset($inprogress)){
echo $inprogress->percentage;
}
Then your code look like this,
<h3>Avg inprogress:<?php if(isset($inprogress)){ echo "<span style='color:#ff00ff;font-family:verdana;'>".$inprogress->percentage."</span>";}?></h3>
Then call the controller function. I think inprogress is not set at the first time.
If it doesn't work, try to var_dump($inprogress) in controller and check value and type.
And try this code in your model. Query also seems not correct
public function getstaffinprogress($user_id) {
$this->db->select_avg('ut_lp_marks.percentage');
$this->db->where('ut_lp_marks.obj_id', $user_id);
$this->db->where('object_data.type', 'crs');
$this->db->where('ut_lp_marks.status', 1);
$this->db->join('object_data', 'object_data.obj_id = ut_lp_marks.obj_id');
$query = $this->db->get('ut_lp_marks');
return $query->result_array();
}
I assume that your db is ut_lp_marks. Then var_dump array and check data is correct first. Then access array element.
public function getstaffinprogress($user_id) {
$result = array();
$query=$this->db->query("select AVG(m.percentage) from object_data o, ut_lp_marks m where o.obj_id=m.obj_id and o.type='crs' and m.status=1 ");
foreach($query->result() as $row){
$result = $row;
}
return $result;
}
Also check $inprogress->percentage exists before print in view.

Codeigniter can't fetch all data from database table

I am fetching data from mysql database table in codeigniter using following code
$result = $this->db->get("shipping");
$data = $result->result_array();
But returns no data.
When I apply the limit as
$this->db->get("shipping",1,30)
The code works.
But I want to fetch all result and not by limiting it.
Could anyone please let me know how do I resolve this issue.
Thanks in advance.
have you tried using query rather than get?
$sql = "
SELECT
*
FROM
shipping
";
$query = $this->db->query($sql);
$data = $query->result();
You have not put a return
https://www.codeigniter.com/user_guide/database/results.html
// This will get every result from shipping
public function somefunction() {
$result = $this->db->get("shipping");
return $result->result_array();
}
Controller function
public function somecontrollerfunction() {
$this->load->model('some_model');
$data['shipping'] = array();
$data['shipping'] = $this->some_model->somefunction();
// var_dump($data['shipping']);
$this->load->view('your_view', $data);
}

How to call multiple store procedure in controller codeigniter?

I have a problem when I want to call more than one stored procesure in the controller. just only one stored procedure that execute.
This query stored procedure in the model :
function getKategori(){
$query = $this->db->query("call KategoriSelectPro('id_kategori','kategori')");
return $query->result();
}
function getEditSubKategori($id_subkategori){
$query = $this->db->query("call SubKategoriEditSelectPro(?,'id_kategori','kategori','sub_kategori')", $id_subkategori);
return $query->row_array();
}
and this code in controller:
function subkategoriedit($id_subkategori = ''){
$data['kategori'] = $this->madmin->getKategori();
$data['editsubkategori'] = $this->madmin->getEditSubKategori($id_subkategori);
}
The problem is only one function of the model can be called in the controller. example:
$data['kategori'] = $this->madmin->getKategori(); (SUCCESS)
$data['editsubkategori'] = $this->madmin->getEditSubKategori($id_subkategori); (NOT RUN)
The error message is :
Commands out of sync; you can't run this command now
so if reversed.
help me, how to call multiple store procedure in CodeIgniter?
I had the same problem a couple of moments earlier, searched for it on stackoverflow, found the answer and forgot to rate the answer .. anyhow
add the following function in file
system > database > drivers > mysqli > mysqli_driver.php
function next_result()
{
if (is_object($this->conn_id))
{
return mysqli_next_result($this->conn_id);
}
}
and then add the following command after every calling query you perform.
$this->db->next_result();
it worked like a charm for me ..
in your model change return $query->row_array(); to return $query->result_array();
function getKategori(){
$query = $this->db->query("call KategoriSelectPro('id_kategori','kategori')");
return $query->result();
}
function getEditSubKategori($id_subkategori){
$query = $this->db->query("call SubKategoriEditSelectPro(?,'id_kategori','kategori','sub_kategori')", $id_subkategori);
return $query->result_array();
}

Codeigniter php MVC

I have a table called 'News' with three columns: 'id', 'title' and 'details'
I have a function ('get_entry') inside a codeigniter model class (called 'News_model')
function get_entry()
{
$this->load->database();
return $this->db->select('id,title,details')->from ('news');
$data['newsarray'] = $this->db->row_array();
return $data['newsarray'];
}
I am connecting to the db so that is not the problem.I want to return an iterable array from get_entry() by calling the function from a controller file with the followlwing code. I want to push it into another array (called '$data['theNews']') using the code below.
foreach ($this->News_model->get_entry() as $key => $value){
array_push($data['theNews'],$value->title);
}
I have been using the code on this (https://www.codeigniter.com/user_guide/general/models.html) as a template (in particular the function 'get_last_ten_entries()' but I think I am close with the code I posted above. I would appreciate any help.
About your code:
You have two 'return' in your get_entry function:
function get_entry()
{
$this->load->database();
// First
return $this->db->select('id,title,details')->from ('news');
$data['newsarray'] = $this->db->row_array();
// Second
return $data['newsarray'];
}
Change it to:
function get_entry()
{
$this->load->database();
$query = $this->db->select('id,title,details')->from('news');
$data['newsarray'] = $query->row_array();
return $data['newsarray'];
}
It should work now.
Some advices:
Don't use Codeigniter 2 anymore. Version 3 is alive.
If you plan to return whole table columns, i suggest you to use the following code for the query:
$query = $this->db->get('news', 1, 20);
Where 1, 20 is the limit.
Now you can get the result:
return $query->result();
A simple example:
function get_entry()
{
$this->load->database();
$query = $this->db->get('news', 1, 20);
return $query->result();
}
This method returns the query result as an array of objects that you can print like so in your controller:
$news_array = $this->News_model->get_entry();
foreach ($news_array as $news)
{
echo $news->id;
}
Look at CI 3 Query Builder query builder for more examples.
One more suggestion, just autoload the database library in application/config/autoload.php if you need it globally.
Changing the code to this in the function worked:
function get_entry()
{
$this->load->database();
$query = $this->db->get('news');
//return $query->result();
foreach ($query->result() as $row)
{
echo "</br>";
echo $row->id;
echo "</br>";
echo $row->title;
echo "</br>";
echo $row->details;
echo "</br>";
}
}
Calling the function like so prints it out:
$news_array = $this->News_model->get_entry();

Issue in accessing table contents using status parameter

I am trying to display jobs won by a certain provider. What I did was to create function get_approved_job_proposals in my model. Then, I created function manage_job_contracts in my controller, but I never got to successfully run it.
Here's my code in model:
public function get_approved_job_proposals($status)
{
$this->db->select('*')->from('job_proposal')->where('status', $status);
$this->db->where("status" == "Awarded");
$query = $this->db->get();
return $query->result_array();
}
and this is what I have in my controller:
public function manage_job_contracts()
{
$this->validateRole('client');
$this->load->model('job_model');
$data['my_preference'] = $this->job_model->get_approved_job_proposals($status);
$data['job'] = $this->job_model->get_job($id);
$this->load->view('client/manage_job_contracts', $data);
}
Kindly help me fix this issue.
This is wrong:
$this->db->where("status" == "Awarded");
Correct:
$this->db->where("status", "Awarded");
Check documentation: http://ellislab.com/codeigniter/user-guide/database/active_record.html

Categories