0

I am creating the user module on my social network. I am trying to set it up to where if you are signed in and looking at your own profile you are $user['id']; and if you are signed in and looking at someone else's profile you are $prof['id']; which I have started to accomplish.

However I am new to codeigniter and php all together and trying to figure out how to attach the users id to the end of the url so my system can check to see if it's $user['id'] or $prof['id']

Controller:

public function profile() 
    {
        $data['profile_icon'] = 'profile';
        $data['main_content'] = 'account/profile';
        $this->load->view('includes/templates/profile_template', $data);
        $this->load->helper('date');
        $this->load->library('session');
        $session_id = $this->session->userdata['id'];
        //USER ID LOOKING AT OWN PROFILE
        $this->load->model('account_model');
        $user = $this->account_model->user();
        $data['user'] = $user;
        //PROF ID LOOKING AT SOMEONE ELSE'S PROFILE
        $this->load->model('account_model');
        $prof = $this->account_model->prof();
        $data_prof['prof'] = $prof;


        if($session_id != $user['id'])
        {
            echo $prof['id'];
            echo "<br />";
            // FOR TESTING PURPOSES
            echo "other user";
        }

        elseif($session_id == $user['id'])
        {
            echo $user['id'];
            echo "<br />";
            // FOR TESTING PURPOSES
            echo "hey it's me ";

        }


    }

View:

<div class="edit_prof_header">
// WHAT IS A GOOD WAY TO MAKE THIS DYNAMIC WITH THE `$prof` AND `$user` WITH OUT HAVING TO D O AN IF STATEMENT WITH EACH ELEMENT?
  <?php echo $user['first_name']." ".$user['last_name'];?>  
</div>

Model: // PERHAPS I COULD SOLVE THE WHOLE DIFFERENCE OF SINGED IN AND OUT HERE AND INCORPORATE TO THE CONTROLLERS AND VIEWS?????

public function user() 
        {
            $session = $this->session->userdata('is_logged_in');
            $user_id = $this->session->userdata('id');

            $query = $this->db->query("SELECT * FROM users WHERE id=$user_id LIMIT 1");
            if($query->num_rows()==1)
            {
               $data = $query->result_array();
               return $data[0];
            //above returns the single row you need to the controller as an array.
            //to the $data['user'] variable.

            }

        }
        public function prof() 
            {
                $session = $this->session->userdata('is_logged_in');
                $user_id = $this->session->userdata('id');

                $query = $this->db->query("SELECT * FROM users WHERE id=$user_id LIMIT 1");
                if($query->num_rows()==1)
                {
                   $data_prof = $query->result_array();
                   return $data_prof[0];
                //above returns the single row you need to the controller as an array.
                //to the $data['user'] variable.

                }

            }

Thanks in advance.

***EDIT//Trying to get the id attched to the url

$this->load->view('includes/templates/profile_template', $data);
$this->load->helper('date');
$this->load->library('session');
$session_id = $this->session->userdata['id'];
$this->load->model('account_model');
//USER ID LOOKING AT OWN PROFILE
$data_user['user'] = $this->account_model->user();
$user['id'] = $this->uri->segment(3);
echo $user['id'];
$data['profile_icon'] = 'profile';
$data['main_content'] = 'account/profile/'.$user['id'];

***EDIT FOR ROUTES

route['default_controller'] = "home";
$route['404_override'] = '';
$route['profile/:num'] = "auth/profiles";

//CONTROLLER

public function profile() 
    {

        $this->load->helper('date');
            $this->load->library('session');
        $session_id = $this->session->userdata['id'];
        $this->load->model('account_model');
        $user = $this->account_model->user();
            $data['user'] = $user;
        $user['id'] = $this->uri->segment(4);
        $data['profile_icon'] = 'profile';
        $data['main_content'] = 'account/profile/'.$user['id'];
        $this->load->view('includes/templates/profile_template', $data);

    }
LightningWrist
  • 937
  • 4
  • 20
  • 37
  • basically both are the same, so you only have to add the missing data using an if statement, not for everything as you're doing right now. anyway codeigniter's sessions uses normal braces () not [] – ahmad Feb 04 '13 at 20:43
  • $user['id'] and $prof['id'] are not being created by session helper completely. – LightningWrist Feb 04 '13 at 20:51

1 Answers1

2

First of all I don't see the need for switching ID's like that, it serves no practical purpose to be honest. If you're viewing a profile you do it by userId, regardless of if it's your profile or someone else's profile. If you want the ability to edit a profile page you can do that simply by checking if the userId for the profile matches the session userId.

For example in your view you could have a link:

<?php
    if($user['userId']==$this->session->userdata('userId'))
    {
        echo '<a href="users/edit_user/'.$user['userId'].'">Edit my profile</a>';
    }

You would also check again that the userId matched the session before doing any saving. By the way above is how you would create a link with the userId attached, to use that link in a controller you'd check the uri segment, so something like this:

Example URL: www.example.com/profiles/view_profile/23

$userId = $this->uri->segment(3);

Understanding uri segments. I think sometimes people get confused about the URI segment. Your base_url is 0, you start counting from there. It doesn't matter where it is in the actual URL structure as long as you remember the base_url is always 0. For example if your base URL is www.example.com/hello/world Then uri segment 1 comes after world NOT .com.

You also do a lot of beginner things that to be honest I have no idea why I see tutorials showing people all the time.

    $prof = $this->account_model->prof();
    $data_prof['prof'] = $prof;

Why not do this? What is the purpose of setting a variable to set a variable?:

    $data_prof['prof'] = $this->account_model->prof();

Also in your user query take the LIMIT out. That is going to ensure you only get one result even if your query matches more than one result, you should ensure you can't have multiple users with the same ID at all times obviously but having that in the query sort of defeats the purpose of the if num rows statement. You're going to get 1 or 0 no matter what.

Rick Calder
  • 18,310
  • 3
  • 24
  • 41
  • Thank you so much. This is a wonderful answer, and all I was looking for! – LightningWrist Feb 04 '13 at 21:13
  • I just found this helpful article http://ellislab.com/codeigniter/user-guide/general/urls.html – LightningWrist Feb 05 '13 at 02:08
  • I think I must be applying the logic wrong, as I'm getting Unable to load the requested file: .php error with this code: SEE EDIT****** – LightningWrist Feb 05 '13 at 02:27
  • Can you show your URL structure? It's possible you're not grabbing the right segment. You can try just echoing out what the uri segment is to be sure. Start counting after the base URL. so if your url is example.com/profile/25 and 25 is the ID you want that segment is 2 I just used 3 as an example, I should have put a corresponding URL to give you the idea how it works. – Rick Calder Feb 05 '13 at 12:01
  • I believe the index.php counts as a segment, try 4. Honestly I take the index.php out of my urls on every new project so I've never had to deal with that. – Rick Calder Feb 05 '13 at 20:34
  • That's not working either. The best I can get is not loading page with an error that says the requested url 'www.clci.dev/index.php/auth/profile/218 can not be loaded. At least I'm able to add the users id to the url? – LightningWrist Feb 05 '13 at 21:06
  • Your routing is wrong. Go to routes.php and add profile/(:num) to the routing before profile. – Rick Calder Feb 06 '13 at 12:17
  • I've read up on documentation and took a stab at it but not sure exactly how and I should add a value ( and what value ) to the $routes. I'm also unsure of my controller. See my edits. Thanks again for all the help – LightningWrist Feb 06 '13 at 20:01
  • Basically the routes.php just tells codeigniter what to load based on the url. So if you add $route['auth/profile/(:num)']='auth/profile/$1'; it should send anything with a number in the segment after profile to the profile method in the auth page. You could even leave auth out of the first part and shorten your urls. So if you put $route['profile/(:num)']='auth/profile/$1'; then you could leave the auth part out of your urls that the public sees. – Rick Calder Feb 06 '13 at 22:46
  • I'm suspicious of my controller $this->load->helper('date'); $this->load->library('session'); $session_id = $this->session->userdata['id']; $this->load->model('account_model'); $user = $this->account_model->user(); $data['user'] = $user; $user['id'] = $this->uri->segment(4); $data['profile_icon'] = 'profile'; $data['main_content'] = 'account/profile/'.$user['id']; $this->load->view('includes/templates/profile_template', $data); – LightningWrist Feb 07 '13 at 01:25