How to display all the id_following - php

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.

Related

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

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

Codeigniter put values from db in array and print them

All I want is to get some place_ids from db, put them in array and echo that array in view. Could you please check the code below and help me to find the mistake. I think the problem is in view part.
Model:
$this->db->select('place_id');
$this->db->from('table');
$this->db->where('ses_id', $ses_id);
$query = $this->db->get();
if ($query && $query->num_rows() > 0) {
return $query->result_array();
}
else {return false;}
Controller:
$ses_id = $this->uri->segment(3);
$data["results"] = $this->mymodel->did_get($ses_id);
$this->load->view("my_view", $data);
View:
<?php
$place_id = array();
foreach($results as $row){
$place_id[] = $row['place_id'];
}
print_r($place_id);
?>
checking the $results is empty or not
<?php
$place_id = array();
if(!empty($results)
{
foreach($results as $row)
{
$place_id[] = $row['place_id'];
}
}else
{ echo "no data found";}
print_r($place_id);
?>

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

Categories