如何在代码点火器中将用户ID附加到URL?

时间:2013-02-04 20:15:51

标签: php codeigniter codeigniter-routing

我正在社交网络上创建用户模块。我正在尝试将其设置为如果您已登录并查看自己的个人资料,$user['id'];如果您已登录并查看其他人的个人资料,则您$prof['id'];我已开始完成。

但是我是codeigniter和php的新手,并试图弄清楚如何将用户ID附加到网址的末尾,这样我的系统就可以查看它是$user['id']还是{{1} }

控制器:

$prof['id']

查看:

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

        }


    }

型号: // PERHAPS我可以解决在这里进出的整体差异,并将其与控制器和视图相结合?????

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

提前致谢。

***编辑//尝试将ID附加到网址

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.

                }

            }

***为路线编辑

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

// CONTROLLER

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

1 个答案:

答案 0 :(得分:2)

首先,我不认为需要像这样切换ID,说实话它没有任何实际意义。如果您正在查看个人资料,则可以通过userId执行此操作,无论是您的个人资料还是其他人的个人资料。如果您希望能够编辑个人资料页面,只需检查个人资料的userId是否与会话userId匹配即可。

例如,在您的视图中,您可以有一个链接:

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

在进行任何保存之前,您还会再次检查userId是否与会话匹配。顺便说一下,如何使用附加的userId创建链接,在控制器中使用该链接检查uri段,如下所示:

示例网址:www.example.com/profiles/view_profile/23

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

了解uri细分。 我想有时人们会对URI段感到困惑。你的base_url是0,你从那里开始计数。只要您记住base_url始终为0,它在实际URL结构中的位置无关紧要。例如,如果您的基本URL是www.example.com/hello/world那么uri segment 1来自world NOT .com

你还做了很多初学者的事情,说实话我不知道为什么我会看到教程一直向人们展示。

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

为什么不这样做?设置变量以设置变量的目的是什么?:

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

同样在您的用户查询中,取出LIMIT。即使您的查询匹配多个结果,这也将确保您只获得一个结果,您应该确保您在任何时候都不能拥有具有相同ID的多个用户,但是在查询中有这样的结果会失败的目的if num rows语句。无论如何,你都会获得1或0。