I have finally got Codeigniter to work with ignited datatables. Now i have run into a different problem. Can anyone help or tell me if i could run the below query with the datatables plugin for codeigniter.
At present i'm doing it within the controller which is lame i know (this was only for testing)
Controller
$data['query'] = $this->test_queries->list_partners();
foreach($data['query'] as $k => $company){
$data['query'][$k]->partner_contacts = $this->test_queries->get_partner_contacts($company->id);
}
Queries in the Model
function list_partners(){
$this->db->select("company.id,name,general_email,general_phone,market");
$this->db->from("company");
$this->db->join('markets','markets.id = company.market_id');
$query = $this->db->get();
$result = $query->result();
return $result;
}
function get_partner_contacts($id){
$this->db->select('partner_contacts.id,contact_type');
$this->db->from('partner_contacts');
$this->db->where('company_id',$id);
$this->db->join('department','department.id = partner_contacts.contact_type_id');
$query = $this->db->get();
$result = $query->result();
return $result;
}
You can change the queries in the model as below:
function list_partners(){
$this->datatables->select("company.id,name,general_email,general_phone,market");
$this->datatables->from("company");
$this->datatables->join('markets','markets.id = company.market_id');
return $this->datatables->generate();
}
function get_partner_contacts($id){
$this->datatables->select('partner_contacts.id,contact_type');
$this->datatables->from('partner_contacts');
$this->datatables->where('company_id',$id);
$this->datatables->join('department','department.id = partner_contacts.contact_type_id');
return $this->datatables->generate();
}
You can also use method chaining if you are using PHP 5 or above.
Related
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);
}
I m using codeigniter with MSSQL db. to get a number of records from a query I m using $query->num_rows(). but its not working. If I use MYSQL DB then its working fine. My code is -
function getCountry(){
$this->db->distinct();
$this->db->select('CM_CHANNEL_TELCAS_DETAIL.CTD_CNTRY_CODE as CNTRY_CODE, CM_COUNTRY.CC_CNTRY_NAME as CNTRY_NAME');
$this->db->from('CM_CHANNEL_TELCAS_DETAIL');
$this->db->join('CM_COUNTRY','CM_CHANNEL_TELCAS_DETAIL.CTD_CNTRY_CODE = CM_COUNTRY.CC_CNTRY_CODE','inner');
$query = $this->db->get();
if($query -> num_rows() > 0){
return $query->result();
}else{
return false;
}
}
If I write above code like this then It gives the results -
function getCountry(){
$this->db->distinct();
$this->db->select('CM_CHANNEL_TELCAS_DETAIL.CTD_CNTRY_CODE as CNTRY_CODE, CM_COUNTRY.CC_CNTRY_NAME as CNTRY_NAME');
$this->db->from('CM_CHANNEL_TELCAS_DETAIL');
$this->db->join('CM_COUNTRY','CM_CHANNEL_TELCAS_DETAIL.CTD_CNTRY_CODE = CM_COUNTRY.CC_CNTRY_CODE','inner');
$query = $this->db->get();
return $query->result();
}
I tried to echo $query->num_rows() value. but its not giving any value.
Please help me to resolve this issue.
Thanks in Advance.
Try with other method $this->db->count_all_results()
if($this->db->count_all_results() >0)
{
return $query->result();
}
else
{
return false;
}
try this
echo $query->affected_rows();
hope help youu
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();
}
unable to use order_by random in codeigniter
My Code is:
function fetch_popular_news()
{
$query=$this->db->order_by('id','rand')->limit(1)->get('main_slider');
$data=$query->result();
return $data;
}
i also tried:
function fetch_popular_news()
{
$query=$this->db->order_by('id','RANDOM')->limit(1)->get('main_slider');
$data=$query->result();
return $data;
}
but it's not working...
Use this instead, it will work:
order_by('rand()')
In codeignitor you need to specify it as follows
function fetch_popular_news()
{
$this->db->order_by('id', 'RANDOM');
$this->db->limit(1);
$query = $this->db->get('main_slider');
return $query->result_array();
}
Another way would be
function fetch_popular_news()
{
$query = $this->db->query('SELECT * FROM main_slider ORDER BY RAND() LIMIT 1');
return $query->result_array();
}
Friends I have found the solution
$query=$this->db->order_by('rand()')->limit(1)->get('main_slider');
anyway thanks to every one for help me...
I have to extract two separate pieces of information from my mysql database. I'm having a hard time trying to figure out how to extract two different sets of information via function(s) I'm writing. I'm trying to figure out a solution but I'm not getting it. Below is my syntax so far. My goal here is to get both of the functions (getPrice and getOtherPrice) all within the same function working. If I // one, the other one works. If both are active, just one is working. How would you guys go about correcting this issue, what do you think I'm doing wrong? Thanks Everyone.
function getJoinInformation($year,$make,$model)
{
$data = $this->getPrice($year,$make,$model);
$data = $this->getOtherPrice($year,$make,$model);
return $data;
}
function getPrice($year,$make,$model)
{
$this->db->select('*');
$this->db->from('tbl_car_description d');
$this->db->join('tbl_car_prices p', 'd.id = p.cardescription_id');
$this->db->where('d.year', $year);
$this->db->where('d.make', $make);
$this->db->where('d.model', $model);
$query = $this->db->get();
return $query->result();
}
function getOtherPrice($year,$make,$model)
{
$this->db->select('*');
$this->db->from('tbl_car_description d');
$this->db->where('d.year', $year);
$this->db->where('d.make', $make);
$this->db->where('d.model', $model);
$query = $this->db->get();
return $query->result();
}
you are returning only last function result you should put the the result of both function by making an array of $data
function getJoinInformation($year,$make,$model)
{
$data['getPrice'] = $this->getPrice($year,$make,$model);
$data['getOtherPrice'] = $this->getOtherPrice($year,$make,$model);
return $data;
}
You're replacing the result in the variable $data.
$data = $this->getPrice($year,$make,$model);
$data = $this->getOtherPrice($year,$make,$odel);
$data will always just contain the result of the last function.