Welcome Guest, Not a member yet? Register   Sign In
User ID data not showing.
#1

Hello everybody,


Can somebody tell me where I got the error in my code? I already tried different queries in my get_userdata method and I even tried $query->result_objec(); but so far I've been unable to display the correct info.


This is my user method in User_controller:

PHP Code:
    public function user($id){
 
       //Check Login
 
       if (!$this->session->userdata('is_member')) {
 
           redirect('dashboard/login');
 
       
        
} else {
        
        
// Get user info
 
       $data['item'] = $this->User_model->get_userdata($id);
            
 
       // Load template
 
       $this->template->load('public''default''users/user'$data);
        
        }
    } 

This is my get_userdata method in my User_model:

PHP Code:
   public function get_userdata($id)
 
   {
 
       $this->db->select('*');
 
       $this->db->from($this->table);
 
       $this->db->where('id'$id);
 
       $this->db->limit(1);

 
       $query $this->db->get();

 
       if ($query->num_rows() > 1) {
 
           return $query->row(); //using row as I'm usng $item->username ,etc as well
 
       } else {
 
           return false;
 
       }
 
   

and this is my view in which I need to display the user data according to the user ID:

PHP Code:
<section class="showcase text-center" style="background: url(<?php if($item->cover_img) : echo $item->cover_img; else : echo base_url().'assets/img/nocoverimage.gif'; endif ?>)no-repeat center center; background-attachment:fixed;">
    <
div class="container">
    <
img class="img-thumbnail" src="<?php if($item->avatar_img) : echo $item->avatar_img; else : echo base_url().'assets/img/noavatar.jpg'; endif ?>">
 
       <h1><?php echo get_user_full_name($item->user_id); ?></h1>
        <p><?php echo $item->occupation ?></p>
        <a href="#" class="btn btn-outline btn-lg">Follow</a>
    </div>
</section> 
I do Front-End development most of the time 
Reply
#2

(This post was last modified: 01-27-2018, 04:57 AM by InsiteFX.)

PHP Code:
// Change
if ($query->num_rows() > 1) {

// To
if ($query->num_rows() > 0) { 

Greater than 1 means it has to be higher than 1
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Here is a helper that will show you nice formatted dumps of arrays etc;

PHP Code:
// -----------------------------------------------------------------------

/**
 * varDebug () - Add this method to a CodeIgniter Helper.
 * -----------------------------------------------------------------------
 *
 * Formatted output of var_dump() etc;
 */
if ( ! function_exists('varDebug'))
{
    
/**
     * Variable array Debug Helper
     * -------------------------------------------------------------------
     * Outputs the given variable(s) with color formatting and location
     *
     * USAGE: varDebug($array);
     *
     * @param    mixed    - variables to be output
     */
    
function varDebug()
    {
        list(
$callee) = debug_backtrace();

        
$arguments func_get_args();

        
$total_arguments func_num_args();

        echo 
'<div><fieldset style="background: #fefefe !important; border:1px red solid; padding:15px">';
        echo 
'<legend style="background:lightgrey; padding:5px;">'.$callee['file'].' @line: '.$callee['line'].'</legend><pre><code>';

        
$i 0;
        foreach (
$arguments as $argument)
        {
            echo 
'<strong>Debug #'.++$i.' of '.$total_arguments.'</strong>: '.'<br>';
            
var_dump($argument);
        }

        echo 
"</code></pre></fieldset><div><br>";
        exit;
    }


Use this to make sure that your queries and data arrays contain values...
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(01-27-2018, 04:56 AM)InsiteFX Wrote:
PHP Code:
// Change
if ($query->num_rows() > 1) {

// To
if ($query->num_rows() > 0) { 

Greater than 1 means it has to be higher than 1

I did as you said so( $query->num_rows() > 0) but I'm still getting errors.

PHP Code:
$query->row() ; // with $item->username returns a NULL.

$query->row_array(); // returns a NULL.

$query->result_array(); // returns a bool(false).

$query->result_row(); // returns a bool(false).

$query->result_object() ; // with $item->username returns a bool(false).

$query->free_result(); // returns a bool(false).

/*
How many more $queries should I try?
*/ 

and all of them (literally) shows this as well:

Quote:Trying to get property of non-object
The way in which I display the values in the view remains the same (e.x : $item->username ; according to the query I have).
Thanks for helping.
I do Front-End development most of the time 
Reply
#5

One of the most common fatal errors in PHP is the “call to a member function of anon-object” type.
This occurs whenever a method is called on anything other than an object (usually null).

Check to make sure that you are getting a value passed for you users ID.

Here is a list of the returned query results.


PHP Code:
    /**
     * -----------------------------------------------------------------------
     * Return Object's:
     * result() = $row->title;
     * row()    = $row->title;
     *
     * Return Array's:
     * result_array() = $row['title'];
     * row_array()    = $row['title'];
     * -----------------------------------------------------------------------
     */ 


It sounds like your users ID is not being passed to the method.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(01-28-2018, 05:17 AM)InsiteFX Wrote: One of the most common fatal errors in PHP is the “call to a member function of anon-object” type.
This occurs whenever a method is called on anything other than an object (usually null).

Check to make sure that you are getting a value passed for you users ID.

Here is a list of the returned query results.


PHP Code:
    /**
     * -----------------------------------------------------------------------
     * Return Object's:
     * result() = $row->title;
     * row()    = $row->title;
     *
     * Return Array's:
     * result_array() = $row['title'];
     * row_array()    = $row['title'];
     * -----------------------------------------------------------------------
     */ 


It sounds like your users ID is not being passed to the method.

I still get the same error :/ . I will tell you that three columns in my users table can be NULL (avatar_img, cover_img, occupation ,etc) but so far all the users found in that table have everything filled in. The only column which I believe is empty in both users(currently two resgistered users) is the e-mail but I dont even plan to display that value in the front end.

Thanks man for helping. I will try to post the same question on StackOverflow ; some of them may know as well.

By the way the helper you provided to me is pretty cool. Thnaks.

NOTE: I may get back(again; if I dont get it fixed) later on.
I do Front-End development most of the time 
Reply
#7

If this is a method in your controller or model, the view cannot access it
without the CI Super Object...

PHP Code:
<h1><?php echo get_user_full_name($item->user_id); ?></h1> 

Get back to me and let me know...
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#8

(01-29-2018, 04:04 AM)InsiteFX Wrote: If this is a method in your controller or model, the view cannot access it
without the CI Super Object...

PHP Code:
<h1><?php echo get_user_full_name($item->user_id); ?></h1> 

Get back to me and let me know...

Hey I'm back(could not get it solved). The method is not actually from any controller of model, it is from a helper I crerated in order to get the first name and last name of the user together.

Here it is:
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function 
get_user_full_name($id){
    
$CI =& get_instance();
 
   $CI->load->database();
 
    
    $CI
->db->where('id',$id);
 
   $query $CI->db->get('users');
 
   if($query->num_rows() > 0){
 
       return $query->row()->first_name' '.$query->row()->last_name;
 
   } else {
 
       return false;
 
   }


Thanks for helping I dont want to move further without solving this as this feature(viewing profiles) is pretty much important.
I do Front-End development most of the time 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB