View user data along with profile pic in codeigniter? - php

I want to show user data along with profile pic on it's profile page i am successful in displaying the profile pic but have some doubts in my mind how can i other show user data on same page should i make other controller and model for that or single controller and model is enough to perform task.If we make another model and controller for that is it possible to or good pratice to return different data one from profile_pic controller and other from user_data controller to same view.
//My profile_pic controller
public function index() {
if($this->session->userdata('is_login')) {
$session_data = $this->session->userdata('sessiondata');
$id = $session_data['user_id'];
$this->load->model('Display_profilepicture');
$data = $this->Display_profilepicture->getImage();
//var_dump($data);
print_r($data);
//echo '<br>';
$img = base_url().'upload/thumbs/'.$data;
//echo $img.'<br>';
$data = array('img' => $img);
//print_r($data);
//$this->load->view('header');
//$this->load->view("my_profile", $data);
// $data=array('profile_picture'=>$img);
//print_r( $data['profile_picture']);
//echo '<br>';
//header("Content-type: image/jpg");
//$this->load->view('header');
//$this->load->view('my_profile',array('data'=>$data));
}
//my model
function get_user_data(){
$session_data = $this->session->userdata('sessiondata');
$id = $session_data['user_id'];
$this->db->select("*");
$this->db->from('tbl_usrs');
$this->db->where('user_id', $id);
$query = $this->db->get();
if($query->num_rows()==0)
echo("Picture not found!");
else
$data = $query->result();
return $data=$query->result();
//print_r($data['profile_picture']);
}
function getImage(){
$session_data = $this->session->userdata('sessiondata');
$id = $session_data['user_id'];
$this->db->select("*");
$this->db->from('tbl_usrs');
$this->db->where('user_id', $id);
$query = $this->db->get();
if($query->num_rows()==0)
echo("Picture not found!");
else
$data = $query->row_array();
return $data['profile_picture'];
//print_r($data['profile_picture']);
}
this is data returned from model and result of print_r($data);
Array ( [user_id] => 26 [first_name] => aman [last_name] => [username] => aman123 [sex] => [dob] => 0000-00-00 [phone] => 0 [email] => aman#gmail.com [visited_country] => 0 [about_me] => [help_others] => [fav_activity] => [bed_offer] => 0 [couch_country] => 0 [couch_state] => 0 [couch_city] => 0 [couch_addline1] => [couch_addline2] => [profile_picture] => db421e40f1c1852d8cc0acc93d7bb963.jpg [picture1] => [picture2] => [picture3] => [picture4] => [picture5] => [country] => [state] => [city] => [addline1] => [addline2] => [zip] => 0 [created_on] => 0000-00-00 [password] => 123123 [modified_on] => 0000-00-00 [active] => [user_type] => 0 [ip] => ::1 )
if make changes in my model set return $data instead of $data['profile_picture']
then array of data is returned but i get error array to string conversion due line in controller "$img = base_url().'upload/thumbs/'.$data;" so i just wana ask what is best way to perform task and how??Some code help is also accepted??

In controller
function index()
{
if ( $this->session->userdata('is_login') )
{
$session_data = $this->session->userdata('sessiondata');
$id = $session_data['user_id'];
$this->load->model('Display_profilepicture');
$result = $this->Model_name->get_user_data($id);
if($result==0)
{
echo 'No user Found';
}
else
{
$data['user']=$result;
$this->load->view('header');
$this->load->view("my_profile", $data);
}
}
}
In model
function get_user_data($id)
{
$query = $this->db->query("SELECT * FROM tbl_usrs WHERE user_id='$id'");
$result = $query->result_array();
$count = count($result);
if(empty($count) || $count > 1)
{
$log = 0;
return $log ;
}
else
{
return $result;
}
}
In View
foreach ( $user as $new_user )
{
?>
<h4>Your name: <?php echo $new_user['first_name'] ?> </h4>
<img src="<?php echo base_url()?>upload/thumbs/<?php echo $new_user['profile_picture'] ?>" alt="<?php echo $new_user['first_name'] ?>">
<p>E-Mail: <?php echo $new_user['aman#gmail.com'] ?></p>
<?php
}

Related

Controller function does not display the return value inside foreach

I am getting my query output in the model and sending to controller If I check the data in the controller like print_r($result);
I am getting the output from my Model and My output is
stdClass Object
(
[membership_id] => 11
[member_id] => 10
[customer_id] =>21
[membership_approve] => 1
[membership_status] => 1
[profile_pic] =>
[first_name] => Juvin
[middle_name] => kumar
[last_name] => choudhary
[email] => juvin#gmail.com
[dob] => 2018-07-01
[phone] => 01236541254
[is_Approved] => 1
)
Model code is
if ($result) {
foreach($result as $result_set ){
if($result_set->membership_status == 1)
{
$result2=$this->db->select('*')
->from('membership_details')
->join('members','members.member_id = membership_details.member_id')
->group_start()
->where('members.is_Approved',1)
->where('membership_details.membership_status', 1)
->where('membership_details.member_id',$result_set->member_id)
->group_end()
->get()
->row();
return $result2;
}
else
{
return 0;
}
}
}
else{echo "Not working";}
Now I am on the controller. I am passing the output in the foreach but when I check $row->phone then it's showing meMessage: Trying to get property of non-object`
$result=$this->Search_model->check_membership($customer_name,$customer_mobile);
$arr_result2 = array();
if (count($result) > 0)
{
print_r($result);// It's displaying the same output which I aaded in the question.
foreach($result as $row){
print_r($row->customer_id);// not getting customer id
$arr_result2[] = array(
"profile_pic" => $row->profile_pic,
"name" => $row->first_name.' ' .$row->last_name,
"phone" => $row->phone
);
}
}
else{echo "No data availble";}
//print_r($arr_result2);
echo json_encode($arr_result2);
Hope this will help you :
row() fetches single record so u don't need to loop it do like this :
$result = $this->Search_model->check_membership($customer_name,$customer_mobile);
$arr_result2 = array();
if (count($result) > 0)
{
print_r($result);// It's displaying the same output which I aaded in the question.
$arr_result2[] = array(
"profile_pic" => $result->profile_pic,
"name" => $result->first_name.' ' .$result->last_name,
"phone" => $result->phone
);
}
else
{
echo "No data availble";
}
//print_r($arr_result2);
echo json_encode($arr_result2);

Foreach array to update table of database use those element of array

How to get the first value of element of array in php.
My story board is like this:
I have an array like this:
(
[0] => Array
(
[ID] => 68
[MATERIAL] => I have
[AC] => Try
)
[1] => Array
(
[ID] => 69
[MATERIAL] => It
[AC] => No Surrender
)
)
I want to update some record on my database like this,
foreach element of array,
UPDATE MY TABEL SET MATERIAL = [MATERIAL], AC = [AC] where id= [id]
this is the model named m_admin :
public function update_eir_to_cost($id, $material, $ac) {
$data = array(
"MATERIAL" => $material,
"AC" => $ac);
$this->db->trans_start();
$this->db->where($id);
$this->db->update('tb_repair_detail', $data);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE) {
// generate an error... or use the log_message() function to log your error
echo "Error Updating";
} else {
echo "Alhamdulillah";
}
}
This is the controller :
public function update_json_detail() {
$post_data = $this->input->post("POST_ARRAY");
$execute = array();
foreach ($post_data as $data) {
$execute[] = array(
'ID'=> $data['0'],
'MATERIAL' => $data['7'],
'AC' => $data['8']
);
}
echo "<pre>";
print_r($execute); // return an array like above.
/*forech element
update table using model
*/
}
This will solve your problem:
public function update_json_detail() {
$post_data = $this->input->post("POST_ARRAY");
$execute = array();
foreach ($post_data as $data) {
$execute[] = array(
'ID'=> $data['0'],
'MATERIAL' => $data['7'],
'AC' => $data['8']
);
}
echo "<pre>";
print_r($execute); // return an array like above.
$this->load->model('m_admin');
foreach ($execute as $row) {
$this->m_admin->update_eir_to_cost($row['ID'], $row['MATERIAL'], $row['AC']);
}
}

Codeigniter User Session Error

I'm face a new Problem in Codeigniter Framework. Her is my Out put
Array
(
[id] => 2
[firstname] => Maruf
[lastname] => Ifftekhar
[slug] =>
[email] => support#russelhost.com
[email_subscribe] => 1
[self] =>
[phone] => 01767820010
[company] => Russel Host
[default_billing_address] =>
[default_shipping_address] =>
[ship_to_bill_address] => true
[password] => 0689d59aa30bdca7207db3d449255650
[active] => 1
[group_id] => 1
[confirmed] => 0
[group_discount_formula] => - 0
[expire] => 1380390903
)
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/secure.php
Line Number: 46
abida Sultana
Here is Controller
`$`email = `$`this->input->post('email');
`$`password = `$`this->input->post('password');
`$`remember = `$`this->input->post('remember');
`$`redirect = `$`this->input->post('redirect');
`$`login = `$`this->Customer_model->login(`$`email, `$`password, `$`remember);
echo '/pre>-----';
print_r(`$`login);
echo 'abida Sultana'.`$`login->last_name; --------------------Line Number: 46
exit();
and Model is
function login(`$`email, `$`password, `$`remember = false) {
`$`this->db->select('*');
`$`this->db->where('email', `$`email);
`$`this->db->where('active', 1);
`$`this->db->where('password', md5(`$`password));
`$`this->db->limit(1);
`$`result = `$`this->db->get('customers');
`$`customer = `$`result->row_array();
if (`$`customer) {
// Retrieve customer addresses
`$`this->db->where(array('customer_id' => `$`customer['id'], 'id' => `$`customer['default_billing_address']));
`$`address = `$`this->db->get('customers_address_bank')->row_array();
if (`$`address) {
$fields = unserialize($address['field_data']);
$customer['bill_address'] = $fields;
$customer['bill_address']['id'] = $address['id']; // save the addres id for future reference
}
$this->db->where(array('customer_id' => $customer['id'], 'id' => $customer['default_shipping_address']));
$address = $this->db->get('customers_address_bank')->row_array();
if ($address) {
$fields = unserialize($address['field_data']);
$customer['ship_address'] = $fields;
$customer['ship_address']['id'] = $address['id'];
} else {
$customer['ship_to_bill_address'] = 'true';
}
// Set up any group discount
if ($customer['group_id'] != 0) {
$group = $this->get_group($customer['group_id']);
if ($group) { // group might not exist
if ($group->discount_type == "fixed") {
$customer['group_discount_formula'] = "- " . $group->discount;
} else {
$percent = (100 - (float) $group->discount) / 100;
$customer['group_discount_formula'] = '* (' . $percent . ')';
}
}
}
if (!$remember) {
$customer['expire'] = time() + $this->session_expire;
} else {
$customer['expire'] = false;
}
// put our customer in the cart
$this->go_cart->save_customer($customer);
return $customer;
} else {
return false;
}
}
If you did a var_dump on your login variable you will see that it is an array not an object so you can't deal with it like an object.
so go to your model and modify the following:
$customer = $result->row_array();
to be:
$customer['result'] = $result->row_array();
then in line 46 use it as:
$login['result']->last_name;
Why this is happening:
because as you defined $customer as an object at first. you also redefined it later in your model as an array by using $customer['something'] pattern of assignation.
So you can't have it your way, it's either an array or an object.
//You used this code because row_array return array not object
`$`login["result"] = `$`this->Customer_model->login(`$`email, `$`password, `$`remember);
echo $login['result']["last_name"];

CodeIgniter query result is returning array as $user['0']->name. How can i get it as $user->name?

Model function
public function getUserByID($id)
{
$q = $this->db->get_where('users', array('id' => $id), 1);
$user= $q->result();
if($user) {
return $user;
} else {
return false;
}
}
Controller code
$data['user'] = $this->users_model->getUserByID($id);
View file code
<pre><?php print_r($user); ?></pre>
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Saleem
[city] => Kuala Lumput
[state] => WP
[postal_code] => 58100
[country] => Malaysia
[phone] => 0123456789
)
)
How can i get the result as below.
Array
(
[id] => 1
[name] => Saleem
[city] => Kuala Lumput
[state] => WP
[postal_code] => 58100
[country] => Malaysia
[phone] => 0123456789
)
as now I need to use it as $user['0']->name; I want to use it as $user->name; Please help me
The correct way of doing this in CI would be using the $result->row() method:
public function getUserByID($id)
{
$q = $this->db->get_where('users', array('id' => $id), 1);
if( $q->num_rows() > 0 )
{
return $q->row();
}
return FALSE;
}
let the function getUserByID return the first user
it should solve the problem
public function getUserByID($id)
{
$q = $this->db->get_where('users', array('id' => $id), 1);
$user= $q->result();
if(count($user) == 1) {
return $user[0];
} else {
return false;
}
}
Try with
$data['user'][0] = $this->users_model->getUserByID($id);
You can directly get from the controller itself
or from model you can go through like
public function getUserByID($id)
{
$q = $this->db->get_where('users', array('id' => $id), 1);
$user= $q->result();
if(count($user) == 1) {
return $user[0];
} else {
return false;
}
}
Controller:
$data['user'] = $this->users_model->getUserByID($id);
foreach($data['user'] as $user)
{
$result = $user;
}
print_r($result);
will give you
Array
(
[id] => 1
[name] => Saleem
[city] => Kuala Lumput
[state] => WP
[postal_code] => 58100
[country] => Malaysia
[phone] => 0123456789
)
EDIT:
Or you can do this at model
public function getUserByID($id)
{
$q = $this->db->get_where('users', array('id' => $id), 1);
$user= $q->result();
if(count($user) > 0)
{
foreach($user as $out)
{
$blah = $out;
}
return $blah;
}
else
{
return false;
}
}
Modify your model function and make it like this
public function getUserByID($id)
{
$q = $this->db->get_where('users', array('id' => $id), 1);
$user= $q->result_array();
if($user) {
return $user;
} else {
return false;
}
}

PHP loop through multi-dimensional array

I am currently working on a site that is being built on codeigniter, I am currently querying the data at the moment I think there could be possible 3 arrays that could be returned as array each with a varying amount of results, my question I cannot for life of me loop through the array I have at the moment,
my model looks like this
public function get_special_backgrounds() {
$this->db->select('*');
$this->db->from('background');
$this->db->where('is_special', 1);
$query = $this->db->get();
return $query->result_array();
}
my controller
enter public function index() {
// $this->output->enable_profiler(TRUE);
$data = array();
if($query = $this->category_model->get_all_online()) {
$data['main_menu'] = $query;
}
$this->load->model('image_model');
/*
* Sort out the users backgrounds, basically do a check to see if there is a 'special' background
* if there is not a 'special' background then IF the user is logged in and has a background of there
* own show that one, if not show a generic one, if they are not logged in show a generic one
*/
$image = array();
if ($query = $this->image_model->get_special_backgrounds()) {
$image = $query;
}
$data = array_merge($data, $image);
die(print_r($data));
$this->load->view('home/main_page.php', $data);
}
the array the gets return looks like this,
Array
(
[main_menu] => CI_DB_mysql_result Object
(
[conn_id] => Resource id #28
[result_id] => Resource id #35
[result_array] => Array
(
)
[result_object] => Array
(
)
[current_row] => 0
[num_rows] => 1
[row_data] =>
)
[special] => Array
(
[0] => Array
(
[background_id] => 6
[background_name] => Master-Backgrounds.png
[background_path] => /Users/Simon/Sites/mysite/media/uploads/backgrounds/
[is_special] => 1
[background_date_uploaded] => 1262687809
[users_user_id] => 1
[users_user_group_group_id] => 1
)
[1] => Array
(
[background_id] => 11
[background_name] => Master-mysite-Template.png
[background_path] => /Users/Simon/Sites/mysite/media/uploads/backgrounds/
[is_special] => 1
[background_date_uploaded] => 1262795313
[users_user_id] => 5
[users_user_group_group_id] => 2
)
)
)
1
It's an object, so you can't loop through it like an array. I do see what you're trying to do and understand why it seems like that makes sense, but to see what I'm talking about, try this:
Change this:
public function get_special_backgrounds() {
$this->db->select('*');
$this->db->from('background');
$this->db->where('is_special', 1);
$query = $this->db->get();
return $query->result_array();
}
To this:
public function get_special_backgrounds() {
$this->db->select('*');
$this->db->from('background');
$this->db->where('is_special', 1);
$query = $this->db->get();
return $query;
}
AND
Change this:
$image = array();
if ($query = $this->image_model->get_special_backgrounds()) {
$image = $query;
}
To this:
if($images = $this->image_model->get_special_backgrounds()) {
foreach($images->result_array() as $image) {
echo "<pre>";
print_r($image);
echo "</pre></br >";
}
}
Do you need to loop on the special part of the array?
foreach ( $data['special'] as $row ) {
// do stuff with the $row array
}
Try foreach
$arr = (your array);
foreach ($arr as $key => $insideArrays) {
foreach ($insideArrays as $k2 => $insideInsideArrays){
..........
}
}
Looks like a result array, with an odd element at the beginning. I'd get rid of the first element and then just loop through it:
array_shift($data);
foreach ($data as $row) {
// Do stuff with $row
var_dump($row);
}

Categories