Issue in accessing table contents using status parameter - php

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

Related

codeigniter search function is not working

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

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 $data not passed from Controller to View

my controller is not passing $data to my view and I don't know why not. I'm reusing code from a previous project which worked fine and I certainly understand the idea of how $data passing is meant to work. But maybe I missed something when copying code over?
I put in the variable $data['hello'] in there just for testing purposes. As you can see from the output $hello isn't even getting through. The if fails and the else code is run correctly which means the view file itself is being loaded.
Controller:
function users() {
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
View:
<?php
echo $hello;
if ($users->num_rows != 0) {
foreach ($users->result() as $user) {
}
} else {
echo "No users.";
}
Output (abridged):
A PHP Error was encountered
Message: Undefined variable: hello
Line Number: 2
A PHP Error was encountered
Message: Undefined variable: users
Line Number: 3
A PHP Error was encountered
Message: Trying to get property of non-object
Line Number: 3
No users.
Edit: more info on request:
Model:
public function get_users($amount = 0, $offset = 0) {
$this->db->from('users');
$this->db->order_by('l_name', 'desc');
if ($amount != 0)
$this->db->limit($amount, $offset);
return $this->db->get();
}
I always do like this
change your model to
$query = $this->db->get();
return $query->result();
And in view
if (count($users)> 0) {
foreach ($users as $user) {
echo $user['name'];
}
} else {
echo "No users.";
}
Hope this helps
Regards
iijb
Just write $data = array(); before you are sending some data into $data array.
function users() {
$data = array();
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
I think you could solve your issue with some simple var_dump() checks.
Check what's coming out of your model by var_dump()ing $data['users'] - is this an object? What happens when you var_dump() $data['users']->result()?
Then, var_dump() $data in your view - does it have all the pieces?
Thing is, even showing us your model function doesn't prove that your getting a real data result. Check that. Your code looks okay at a glance so I don't think that is where the issue exists.
There is something very basic that is wrong. So get out of that controller and do a sanity check. First confirm that your welcome view is working. If it is go to the welcome controller and put this in the index method
$data['here'] = 'we are here' ;
$this->load->view('welcome_message', $data);
and then somewhere in the welcome.php view file
<?php echo $here ?>
You do not need to set this: $data = array();
However some people suggest it because that way even if you dont create any data variables you wont get an error if its in the view call $this->load->view('welcome_message', $data);
finally i would suggest looking at this
function users() {
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
lets see you have method called users, returning an object called users, and a view called users -- that could get confusing ! :-)

REST API with CodeIgniter

I am creating a web-service backend for a mobile app I am developing. (I am an experience Obj-C developer, not a web-designer!) Essentially I would like to use Codeigniter and Phil Sturgeon's RESTful API Server https://github.com/philsturgeon/codeigniter-restserver however, I'm having some trouble getting it all setup and working.
I have MySQL database that is setup with data in it. And I need some help writing a CodeIgniter PHP model and controller that returns JSON data of what is inside that database. All of the tutorials and forum post's i have found deal with hardcoded data in the controler, not a MySQL database. I would ideally like to have the URL formatted like this http://api.mysite.com/v1/search?id=1&name=foo&city=bar , I could potentially have 50+ parameters to pass in the url.
Using Phil's code, I have come up with this as my controller:
public function index_get()
{
if (!$this->get('id'))
{
$this->response(NULL, 400);
}
$data = $this->grid_m->get_id($this->get('id'));
if ($data)
{
$this->response($data, 200);
}
else
{
$this->response(NULL, 404);
}
}
That only gets me one search term: id?=# .. I need to know how to get multiple search terms
Here is my Codeigniter model:
<?php
class Grid_m extends CI_Model
{
function get_all()
{
$query = $this->db->get('grid');
if ($query->num_rows() > 0)
{
return $query->result();
}
return FALSE;;
}
This just returns EVERYTHING in my MySQL database regardless of what id or url term I pass it in the URL.
I'm a big noob when it comes to developing my own custom API so any suggestions on how to fix my controller and database model would be a huge help!
Thanks for the help!
-brian
This is old question but if somebody go here and still need help, try these code:
In your controller:
public function index_get()
{
$where = '';
if ($this->get('id')) // search by id when id passed by
{
$where .= 'id = '.$this->get('id');
}
if ($this->get('name')) // add search by name when name passed by
{
$where .= (empty($where)? '' : ' or ')."name like '%".$this->get('name')."%'";
}
if ($this->get('city')) // add search by city when city passed by
{
$where .= (empty($where)? '' : ' or ')."city like '%".$this->get('city')."%'";
}
// you can add as many as search terms
if ( ! empty($where))
{
$data = $this->grid_m->get_searched($where);
if ($data)
{
$this->response($data, 200);
}
else
{
$this->response(NULL, 404);
}
}
else
{
$this->response(NULL, 404);
}
}
In your model, create get_searched function:
class Grid_m extends CI_Model
{
function get_searched($where = NULL)
{
if ( ! is_null($where))
{
$this->db->where($where);
$query = $this->db->get('grid');
if ($query->num_rows() > 0)
{
return $query->result();
}
}
return FALSE;
}
}
User Codeigniter's Active record to build proper query, you can build any type of query using methods of active record, refer following example I have just added one condition in it you can add more conditions as per your need.
<?php
class Grid_m extends CI_Model
{
function get_all()
{
$this->db->select('col1, col2, col3')->where('id', 5);
$query = $this->db->get('grid');
if ($query->num_rows() > 0)
{
return $query->result();
}
return FALSE;;
}
}
Please check you query
$query = $this->db->get_where('grid', array('id' => $id));

Two different functions one works one calls a non-object

I have two functions getCompanyDetails and getHostingDetails
The first database getCompanyDetails works fine but the getHostingDetails shows
Trying to get property of non-object
getCompanyDetails:
Controller: $data['companyName'] = $this->quote->getCompanyDetails()->companyName;
Model:
public function getCompanyDetails()
{
$this->db->select('companyName,companySlogan,companyContact,
companyEmail,companyWebsite,companyPhone,
companyFax,companyAddress');
$this->db->from('companyDetails');
$result = $this->db->get();
if($result->num_rows()<1)
{
return FALSE;
}else{
return $result->row();
}
}
getHostingDetails:
Controller:
$data['hostingRequired'] = $this->quote->getHostingDetails()->hostingRequired;
Model:
public function getHostingDetails()
{
$this->db->select('hostingRequired,domainRequired,domainToBeReged,
domaintoBeReged0,domainTransfer,domainToBeTransfered,
domainToBeTransfered0,currentHosting');
$this->db->from('hostingDetails');
$result = $this->db->get();
if($result->num_rows()<1)
{
return FALSE;
}else{
return $result->row();
}
}
Well, one method returns an object from $result->row() and the other false. You can't call a method on false.
false is returned when no record is found. So you need to check the return value before using it.
Well in your get functions chances is your code might return you false if there is no rows returned. You might want to check before retrieving the details. Example:
$details = $this->quote->getHostingDetails();
if($details){
$data['hostingRequired'] = $details->hostingRequired;
}
The problem is probably how you use those functions in your controller. If any of them returns FALSE, then
$this->quote->getHostingDetails()->hostingRequired;
is going to give you errors. Try
if ($row = $this->quote->getHostingDetails()) {
echo $row->hostingRequired;
}

Categories