0

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

但是,我是 codeigniter 和 php 的新手,并试图弄清楚如何将用户 id 附加到 url 的末尾,以便我的系统可以检查它是否是$user['id'] 或$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 ";

        }


    }

看法:

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

模型://也许我可以解决这里烧录和烧录的全部差异,并结合到控制器和视图中??????

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.

                }

            }

提前致谢。

***编辑//试图让 id 附加到 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'];

***编辑路线

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

//控制器

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

    }
4

1 回答 1

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 段 1 位于 world NOT .com 之后.

您还做了很多初学者的事情,老实说,我不知道为什么我总是看到向人们展示的教程。

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

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

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

同样在您的用户查询中取出 LIMIT 。这将确保即使您的查询与多个结果匹配,您也只能得到一个结果,您应该确保您不能始终拥有多个具有相同 ID 的用户,但是在查询中使用该 ID 有点违背了if num rows 语句。无论如何,你都会得到 1 或 0。

于 2013-02-04T21:07:08.417 回答