Correct way to display my query - php

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

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;
}

Empty result in Codeigniter

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);
}

How to display all the id_following

My Code :
$this->db->join('followers','followers.id_follower = post.id_account','LEFT');
$id_account = $this->session->userdata('id');
$where = ("followers.id_following=$id_account or post.id_account = $id_account");
$this->db->where($where);
Please help me
Something like this?
In you controller:
// Set variables
$page_data = array();
$id_account = $this->session->userdata('id');
// Get data
$this->db->from('post');
$this->db->join('followers','followers.id_follower = post.id_account','LEFT');
$this->db->where('id_following', $id_account);
$this->db->or_where('id_account', $id_account);
$page_data['followers'] = $this->db->get();
// Load views
$this->load->view('myview', $data);
In your view somewhere
<?php if (!empty($followers)) {
foreach ($followers as $item) {
echo $item['follower_id'];
}
} ?>
You may want to use short forms of php in your view, but you get the idea.

How do I access an Array Value using it's Key in a view

I've been working on a method to email the users data to themselves and I'm having trouble printing data in the view. I can access all the data in bulk but I would like to format it so it displays within a HTML table for the user.
Model:
function getEmailData($usersid){
$this->db->select('*');
$this->db->from('symptom');
$this->db->where('userid', $usersid);
$symptomState = $this->db->get();
$result = $symptomState->result_array();
return $result;
}
function getEmailBMData($usersid){
$this->db->select('*');
$this->db->from('bowelmovement');
$this->db->where('userid', $usersid);
$bmState = $this->db->get();
$result = $bmState->result_array();
return $result;
}
Controller:
function sendDataAsEmail(){
$uid = $this->session->userdata('id');
$uname = $this->session->userdata('username');
$uemail = $this->input->post('email_data');
$data = array(
'userName'=> $uname,
);
$data['symptomData'] = $this->poomonitormodel->getEmailData($uid);
$data['bmData'] = $this->poomonitormodel->getEmailBMData($uid);
$data['symptomCount'] = $this->poomonitormodel->getTotalSymptomCount($uid);
$data['bmCount'] = $this->poomonitormodel->getTotalBMCount($uid);
var_dump($data);
$contents = $this->load->view('pooemail.php',$data,TRUE);
$this->email
->from('#', '#')
->to($uemail)
->subject('#')
->message($contents)
->set_newline("\r\n")
->set_mailtype('html');
$this->email->send();
}
View:
<p>
<?php foreach($symptomData as $data){
foreach($data as $nestdata){
echo $nestdata;
};
};?>
</p>
<p>
<?php foreach($bmData as $bmdata){
foreach($bmdata as $nestbmdata){
echo $nestbmdata;
};
};?>
</p>
Currently echo $nestdata outputs the data as a complete string like this, but I would like to access a single attribute like $nestdata['symptomdate']:
462016-04-1402:00burningOesophagus7vomitting 562016-04-1405:00tinglingGallBladder3vomitting 662016-04-1410:00shootingSmallIntenstine8vomitting 1362016-04-2016:47crampGallBladder1Gurgling Noise 1462016-04-2016:58tinglingRectum1Strange tingling sensation in the raer 1962016-04-2017:41crampIleum2Cramping 2062016-04-2017:42crampAnus7It whistles 2162016-04-2017:42crampRectum7It also whistles
But I would like to access a specific value so that I can put each bit of data into a table data cell so it's easier to read for the user.
Thanks!
The keys in $data will be the name of the var in the view. So, $data['symptomCount'] in the controller will be accessed as $symptomCount in the view.
What you do with it depends on its type. If $symptomCount is an object (class) then $x = $symptomCount->some_property. If it is an array - $symptomCount['some_index']
Iterate them like so
foreach($symptomData as $symptom){
echo $symptom['symptomdate'];
}

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