-2

私の管理者のページには、ユーザーのリストがあります..そして、ユーザー名をクリックすると、そのプロファイルにリダイレクトされるようにしたい..これは私の管理者ページのビューです:

<table class="table table-striped table-bordered table-condensed">
<tr>
 <th>username</th>
 <th>firstname</th>
     <th>lastname</th>
 <th>email</th>
 <th>usertype</th>
 <th>Action</th>
</tr>
 <?php foreach ($info as $infos): ?>
<tr>
 <td>
  <?php 
   $user=$infos['username']; 
   print_r ($user);
   $this->session->set_userdata($user);
  ?>
  </td>
  <td><?php echo $infos['firstname']?></td>
  <td><?php echo $infos['lastname']?></td>
  <td><?php echo $infos['email']?></td>
 <td><a href="<?php echo base_url()?>users/showuser">Show</a> | <a href="<?php echo      base_url()?>users/deleteuser">Delete</a></td>
</tr>
<?php endforeach ?>

私のコントローラーの一部はこれです:

  public function showuser()
{
    $this->load->helper(array('form','url'));
    $this->load->library('form_validation');
    $this->check_isValidated();

    $data['info'] = $this->users_model->user_info();
    $this->load->view('users/showuser',$data);          
}

そして私のモデルでは:

public function user_info()
{
    $this->load->database();
    $this->db->select('username,firstname,lastname,email');
    $user = $this->session->userdata('user');
    $this->db->where('username',$user);
    $query = $this->db->get('users');
    if($query->num_rows > 0)
    {
        $row = $query->row();
        $data = array(
            'firstname' =>$row->firstname,
            'lastname' =>$row->lastname,
            'username' =>$row->username,
            'email' =>$row->email,
        );
        return $data;
    }else{
        return false;
    }

私の問題は、特定のユーザーをクリックすると、それぞれのプロファイルが表示されず、データベースにリストされている最初のユーザーのプロファイルが表示されることです。
モデルの ID を比較する方法

4

1 に答える 1

1

このまま進めていくと、さらに多くの問題に遭遇することになるため、コードの外観からいくつかのチュートリアルを読む必要があります。何よりもまず、一意の ID ではなくユーザー名でユーザーをロードしていることです。これは、ユーザー テーブルに一意の ID さえないため、インデックスが作成されていないため、最終的にパフォーマンスの問題が発生することを意味します。

とにかくそれはさておき、質問に。最初の問題はビューです。必要なユーザーにパラメーターを渡していないため、毎回パラメーターなしで単一の関数をロードしているだけです。URL は次のようなもので、毎回適切なパラメーターを渡すことができます。

//Again, you really should have a userId that you are passing here.
<a href="<?php echo base_url()?>users/showuser/$infos['username']">Show</a>

次に、問題 2 があるモデルでは、ログインしているユーザーのユーザー名をデータベースに渡しているため、他の人のプロファイルを取得することはありません。下記参照:

コントローラ:

//this sets the user to the parameter we added to the url above and passes it to the model function.
$data['info'] = $this->users_model->user_info($this->uri->segment(3));

モデル:

public function user_info($user)
{
// you're now getting the user from the controller like you should be.
$this->load->database();
$this->db->select('username,firstname,lastname,email');
$this->db->where('username',$user);

すべてのコードを含めたわけではなく、関連する部分だけを含めました。これにより当面の問題は解決されますが、これまでに行ったことに関する他の問題には何も対処できないことに注意してください。そのURLを持っている人なら誰でも他の人のプロフィールを見ることができるという意味のセキュリティチェックはありません.IDなどはないようです.幸運を祈ります.

于 2013-01-18T12:41:41.047 に答える