Empty result in Codeigniter - php

I want to get query result from my model and use it in view. I don't know why but my result is empty. Can you look at this and tell me what I'm doing wrong?
Controller:
$peugeot = $this->input->post('peugeot');
$citroen = $this->input->post('Citroen C-Elysee');
$nissan= $this->input->post('Nissan Evalia');
$renault = $this->input->post('Renault Trafic 9-os');
$this->load->model('Edit_model');
$this->Edit_model->peugeot($peugeot);
if($peugeot)
{
$result = $this->Edit_model->peugeot($peugeot);
$data['result'] = $result;
}
Model:
public function peugeot($peugeot)
{
$this->db->like('model', $peugeot, 'after');
$query = $this->db->get('cars');
$result = $query->result();
return $result;
}
View:
if($peugeot)
{
print_r($result);
}

Kinda strange, but It works
I've changed my controller into this
$data['peugeot'] = $this->input->post('peugeot');
$data['citroen'] = $this->input->post('citroen');
$data['nissan'] = $this->input->post('nissan');
$data['renault'] = $this->input->post('renault');
$this->load->model('Edit_model');
if($data['peugeot'])
{
$this->Edit_model->peugeot($data['peugeot']);
$result = $this->Edit_model->peugeot($data['peugeot']);
$data['result'] = $result;
$this->load->view('content/editprocess',$data);
}

Related

Codeigniter Select and Count MySQL Records

Using Codeigniter 3, I would like to display all the records from a table in a MySQL database. I'd also like to include the number of records selected.
For example;
Showing x number of records;
record 1
record 2
record 3
etc
Currently I have the following (which works);
// select all records
public function selectRecords() {
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
return $query->result_array();
}
// count all records
public function countRecords() {
$this->db->select('count(*) as count');
$this->db->from('records');
$query = $this->db->get();
return $query->row();
}
My question is do I need two separate queries in order to achieve this (select and count)?
Is there a more efficient way of achieving what I want?
You can do something like this :
public function selectRecords()
{
$query = $this->db->get('records');
if ($query->num_rows() > 0 )
{
$records = $query->result_array();
$data['count'] = count($records);
$data['all_records'] = $records;
return $data;
}
}
Pass it to the view from your controller :
$data = $this->model_name->selectRecords();
/*print_r($data) to see the output*/
$this->load->view('your_view',$data);
In view :
<?php echo $count .' number of records';?>
you can do only:
public function selectRecords() {
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
return $query->result_array();
}
and
$records = $this->selectRecords();
$count = count($records);
In The first function itself you can get the count using $query->num_rows() function
public function selectRecords() {
$return = array();
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
$return['count'] = $query->num_rows();
$return['records'] = $query->result_array();
return $return;
}
try this
it will help you to provide pagination for records
public function selectRecords($params = array(), $count = false) {
$offset = isset($params['offset']) ? $params['offset'] : '';
$limit = isset($params['limit']) ? $params['limit'] : '';
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
if ($count) {
return $this->db->get()->num_rows();
}
if (empty($offset) && !empty($limit)) {
$this->db->limit($limit);
}
if (!empty($offset) && !empty($limit)) {
$this->db->limit($limit, $offset);
}
$result = $this->db->get()->result();
return $result;
}

getting warning Invalid argument for foreach in codeigniter

In foreach loop i am getting warning Invalid argument supplied for foreach() below is my loop code and modal code please let me know where i done wrong
Controller
$concond = array("con_id"=>1,"con_status"=>1);
$this->data['contact']=$this->Frontend_model->get_contact($concond);
$this->load->view('frontend/clienthome',$this->data);
foreach Loop
if($contact)
{
foreach($contact as $foot)
{
$footadr1 = $foot->con_addr_line_1;
$footadr2 = $foot->con_addr_line_2;
$footcity = $foot->con_city;
$footstate = $foot->con_state;
$footcountry = $foot->con_country;
$footpin = $foot->con_pincode;
$footp1 = $foot->con_phone_1;
$footp2 = $foot->con_phone_2;
$footp3 = $foot->con_phone_3;
$footp4 = $foot->con_phone_4;
$footemail = $foot->con_email_id;
}
}
modal
function get_contact($concond)
{
$this->db->select('*');
$this->db->from('is_addres_contact');
$this->db->where($concond);
$query = $this->db->get();
return $query->result();
}
Seems like I got your problem!
You should try to do it like this:
$data['contact'] = $this->Frontend_model->get_contact($concond);
$this->load->view('frontend/clienthome', $data);
Try in model
function get_contact($con_id, $con_stat)
{
$this->db->where('con_id', $con_id);
$this->db->where('con_status', $con_stat);
$query = $this->db->get('is_addres_contact');
return $query->result();
}
Controller
$con_id = '1';
$con_stat = '1';
$this->data['contact'] = $this->frontend_model->get_contact($con_id, $con_stat);
$this->load->view('frontend/clienthome',$this->data);
Can you just try this,
In model,
function get_contact($concond)
{
$this->db->select('*');
$this->db->from('is_addres_contact');
$this->db->where($concond);
$query = $this->db->get();
return $query->result_array();
}
In View
if($contact)
{
foreach($contact as $foot)
{
$footadr1 = $foot['con_addr_line_1'];
$footadr2 = $foot['con_addr_line_2'];
$footcity =$foot['con_city'];
$footstate = $foot['con_state'];
$footcountry = $foot['con_country'];
$footpin = $foot['con_pincode'];
$footp1 = $foot['con_phone_1'];
$footp2 = $foot['con_phone_2'];
$footp3 = $foot['con_phone_3'];
$footp4 = $foot['con_phone_4'];
$footemail = $foot['con_email_id'];
}
}

PHP bringing back all rows instead of requested

I have a query that is bringing back all rows from the table instead of the fields I have specified. Can you see any mistakes in this? I'm using codeigniter.
Thanks in advance!
unset($conditions);
$conditions['conditions'] = array("accountid"=>$this->sessionInfo['database_account_id'],
"DATE_FORMAT(salestart,'%Y-%m-%d')"=>$today,
"shop"=>"london"
);
$conditions['group_by'] = "item";
$conditions['fields'] = "accountid, item, count(uniqueid) as totalitems, sum(options) as totaloptions, colour";
$today_sales = $this->Database_Model->selectData("sales",$conditions);
My model is:
public function selectData($table,$condition=array()) {
if(isset($condition['fields'])){
$fields = $condition['fields'];
}
else{
$fields = "*";
}
$this->Database->select('*');
$this->Database->from($table);
if(isset($condition['conditions'])){
$this->Database->where($condition['conditions']);
}
if(isset($condition['group_by'])){
$this->Database->group_by($condition['group_by']);
}
if(isset($condition['order_by'])){
$this->Database->order_by($condition['order_by']);
}
if(isset($condition['where_in'])){
$where_in = $condition['where_in'];
foreach($where_in as $key =>$value){
$this->Database->where_in($key,$value);
}
}
if(isset($condition['joins'])){
$joins = $condition['joins'];
foreach($joins as $join){
$this->Database->join($join['table'], $join['joinWith'],$join['type']);
}
}
$query = $this->Database->get();
return $query->result_array();
}
Change this
$this->Database->select('*');
to this
$this->Database->select($fields);

Correct way to display my query

I have a query in my model that I need to print the data in my view
Model
function get_bank()
{
$query = $this->db->query("SELECT
(
12* (YEAR('account_add_date') - YEAR('start_date')) +
(MONTH('account_add_date') - MONTH('start_date'))
) AS differenceInMonth
->FROM ('bank')
WHERE mem_id = '".$this->session->userdata('id')."'");
return $query->result();
$data['account_age'] = $query->row_array();
}
and I am trying to print the output in my model, but its not working and I do not know where I have gone wrong. I am new to MVC and still getting used to it.
View
<h2>age of account</h2>
<?php
$age = $this->model('profiles_model' , $data );
print "<h2>$age</h2>";
?>
Controller
function index()
{
$data = array();
$this->load->model('user_profile/profiles_model');
$query = $this->profiles_model->get_bank();
if(!empty($query))
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
Let's first write your code in proper convention.
Model
function get_bank() {
$mem_id = $this->session->userdata('id');
$query = $this->db
->select("12*(YEAR('account_add_date') - YEAR('start_date')) + (MONTH('account_add_date') - MONTH('start_date')) AS differenceInMonth")
->where('mem_id', $mem_id)
->get('bank');
return $query;
// $data['account_age'] = $query->row_array(); <-- Statement after return is useless.
}
Controller
function index() {
$data = array(
'records' => array()
);
$this->load->model('user_profile/profiles_model');
$bank = $this->profiles_model->get_bank();
if($bank->num_rows()){
$data['records'] = $bank->row_array();
}
$this->load->view('profile_view', $data);
}
View
Not sure what you are trying to do with the bank data but here's how you print the record
<p>Bank data</p>
<p><?=isset($records['differenceInMonth'])?$records['differenceInMonth']:"No record found"?></p>
You are not far away, do it this way:
Controller:
function index()
{
$this->load->model('user_profile/profiles_model');
$data = $this->profiles_model->get_bank();
$this->load->view('profile_view', $data);
}
Model:
function get_bank()
{
$query = $this->db->query("SELECT
(
12* (YEAR('account_add_date') - YEAR('start_date')) +
(MONTH('account_add_date') - MONTH('start_date'))
) AS differenceInMonth
->FROM ('bank')
WHERE mem_id = '".$this->session->userdata('id')."'");
return $query->result();
}
View:
<?php
foreach(differenceInMonth AS $age){
echo "<p>" . $age . "</p>";
}
When you load the view you are passing in $data with $data['records'] that has the data you are wanting to display.
Since that is how you pass the data into the view when you load it you will need to call it that way:
<?php
var_dump($records);
?>
You will also need to loop through the data as well assuming $records is an array of the query results or use var_dump instead of print just to verify the data is there.
Reading through these will help going forward:
https://codeigniter.com/user_guide/general/views.html
https://codeigniter.com/user_guide/general/models.html

This query show me with this active record

I am having trouble getting two tables and passing them to controller:
IN A MODEL:
function get_all_entries() {
$query = $this->db->get('entry');
return $query->result();
$this->db->select('entry_id , count(comment_id) as total_comment');
$this->db->group_by('entry_id');
$comment = $this->db->get('comment');
return $comment->result();
}
IN A CONTROLLER:
$data['query'] = $this->blog_model->get_all_entries();
$this->load->view('blog/index',$data);
How do I return $query and $comment variables to controller? I think I am doing it wrong.
Use this because you are not allowed to return twice in the same method
function get_all_entries()
{
$query = $this->db->get('entry');
$data[] = $query->result();
$this->db->select('entry_id , count(comment_id) as total_comment');
$this->db->group_by('entry_id');
$comment = $this->db->get('comment');
$data[] = $comment->result();
return $data;
}
EDITS:
In controller
function index(){
$this->load->model('mymodel');
$result = $this->mymodel->get_all_entries();
$entries = $result[0] ;
$comments = $result[1] ;
$data['entries'] = $entries;
$data['comments '] = $comments;
}
Your issue is that you're returning $query->result() in first place, return function halts the current function, so the next steps are not being processed.
Best way would be to create two methods for either $query get and $comment get.
An alternative to your issue would be
function get_all_entries() {
$query = $this->db->get('entry');
$this->db->select('entry_id , count(comment_id) as total_comment');
$this->db->group_by('entry_id');
$comment = $this->db->get('comment');
return array($query->result(),$comment->result());
}
Then in your controller
list($data['query'],$data['comment']) = $this->blog_model->get_all_entries();
$this->load->view('blog/index',$data);

Categories