2013-04-29 58 views
0

我目前工作的一个项目,用户可以保存笔记片段中有自己的小用户区。笨数据库检查

我想知道如何将我检查,如果一个项目的ID存在,例如,如果用户访问 http://mysite.com/view/1并有1记片断它会显示所有与一个ID的数据。现在,如果用户要更改网址以允许说1000,该ID不存在,并且该视图只是错误。

我希望能够将其重定向到一个错误消息的某一页“片段不存在”等

继承人是我迄今(我现在已经有一个条件语句在这里检查片段是私有的,那么如果是重定向到/ publicsnippets)

控制器:

class Publicsnippets extends CI_Controller { 

function __construct() 
{ 
    parent::__construct(); 

    if (!$this->tank_auth->is_logged_in()) { 
     redirect('/login/'); 
    } 

    $this->load->model('dashboard_model'); 

    $this->data['user_id'] = $this->tank_auth->get_user_id(); 
    $this->data['username']= $this->tank_auth->get_username(); 
} 

public function index() 
{ 
    $this->data['public_snippets'] = $this->dashboard_model->public_snippets(); 
    $this->load->view('dashboard/public_snippets', $this->data); 
} 

public function view($snippet_id) 
{ 
    $snippet = $this->dashboard_model->get_snippet($snippet_id); 

    if ($snippet['state'] === 'private') 
    { 
     $this->session->set_flashdata('message', "<b style=\"color:red;\">You are not allowed to view this snippet!</b>"); 
     redirect('/publicsnippets'); 

    } else { 

     $this->data['snippet'] = $snippet; 
    } 

    $this->load->view('dashboard/view_public_snippet', $this->data); 

} 

} 

型号:

class Dashboard_model extends CI_Model { 

public function public_snippets() 
{ 
    $this->db->select('id, title, description, tags, author, date_submitted'); 
    $query = $this->db->get_where('snippets', array('state' => 'public')); 
    return $query->result_array(); 
} 

public function private_snippets() 
{ 
    $this->db->select('id, title, description, tags, author, date_submitted'); 
    $query = $this->db->get_where('snippets', array('user_id' => $this->tank_auth->get_user_id())); 
    return $query->result_array(); 
} 

public function add_snippet($data) 
{ 
    $this->db->insert('snippets', $data); 
    $id = $this->db->insert_id(); 
    return (isset($id)) ? $id : FALSE; 
} 

public function get_snippet($snippet_id) { 

    $query = $this->db->get_where('snippets', array('id' => $snippet_id)); 
    return $query->row_array(); 
} 

public function update_snippet($snippet_id, $data) 
{ 
    $this->db->where('id', $snippet_id); 
    $this->db->update('snippets', $data); 
} 

public function delete_snippet($snippet_id) 
{ 
    $this->db->where('id', $snippet_id); 
    $this->db->delete('snippets'); 
} 


    } 

查看:

 <h3>Description</h3> 
     <p><?php echo $snippet['description']; ?></p> 

     <h3>Tags</h3> 
     <p><?php echo $snippet['tags']; ?></p> 

     <h3>Date Submitted</h3> 
     <p><?php echo $snippet['date_submitted']; ?></p> 

     <h3>Snippet</h3></pre> 
     <pre class="prettyprint"><?php echo $snippet['code_snippet']; ?></pre> 

回答

1

你的工作是精细只需添加这样的检查在视图方法

控制器

public function view($snippet_id) 
{ 
    $snippet = $this->dashboard_model->get_snippet($snippet_id); 

    if($snippet){ 
     if ($snippet['state'] === 'private') 
     { 
      $this->session->set_flashdata('message', "<b style=\"color:red;\">You are not allowed to view this snippet!</b>"); 
      redirect('/publicsnippets'); 

     } else { 

      $this->data['snippet'] = $snippet; 
     } 

     $this->load->view('dashboard/view_public_snippet', $this->data); 
    }else{ 
      $this->session->set_flashdata('message', "<b style=\"color:red;\">snippet does not exist</b>"); 
      redirect('/publicsnippets');    
    } 

} 

如果没有排在get_snippet方法发现$snippet将包含falsenull 和条件的第二块会跑。

+0

非常感谢工作就像一个魅力! – jackthedev 2013-04-29 20:38:47

1

在你的模型变化get_snippet()来检查行:

if ($snippet = $this->dashboard_model->get_snippet($snippet_id)) { 
    // code if snippet exists 
} else { 
    // code if snippet doesn't exist 
} 

OR

$snippet = $this->dashboard_model->get_snippet($snippet_id); 
if ($snippet) { 
// etc... 

public function get_snippet($snippet_id) { 
    $query = $this->db->get_where('snippets', array('id' => $snippet_id)); 

    if ($query->num_rows() > 0) { 
     return $query->row_array(); 
    } else { 
     return false; 
    } 
} 
在控制器

然后