我希望这个标题准确地描述了我想要问的内容......
我的控制器使用url(intId)的第三段来搜索我的数据库以在其视图页面等中加载数据库信息。它工作正常,提供intId对应于我的数据库中的一行...但是,如果某个流氓决定输入一个随机的intId,那么该页面会加载错误,因为如果intId不存在,则自然不会设置视图文件中所需的变量。
在我的Controller中,我如何设置和if语句来确定intId是否有效,如果没有 - 设置重定向到错误页面等。
<?php
class Brands extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Products_model');
}
public function products_lookup_by_id($intId)
{
$config = array(
'base_url' => base_url() . '/brands/' . $intId . '/',
'total_rows' => $this->Products_model->total_rows_brands($intId),
'per_page' => 25,
'uri_segment' => 3
);
$this->pagination->initialize($config);
$data = array(
'offset_no' => $this->uri->segment(3),
'total_rows' => $config['total_rows'],
'per_page' => $config['per_page'],
'nav1' => $this->Products_model->nav1(),
'nav2' => $this->Products_model->nav2(),
'nav3' => $this->Products_model->nav3(),
'nav4' => $this->Products_model->nav4(),
'nav5' => $this->Products_model->nav5(),
'nav6' => $this->Products_model->nav6(),
'nav7' => $this->Products_model->nav7()
);
if ($data['total_rows'] > 0)
{
$data['from'] = $data['offset_no'] + 1;
}
else
{
$data['from'] = 0;
}
if (($data['total_rows'] - $data['offset_no']) < $data['per_page'])
{
$data['to'] = ($data['offset_no'] + ($data['total_rows'] - $data['offset_no']));
}
else
{
$data['to'] = $data['offset_no'] + $data['per_page'];
}
$query = $this->Products_model->query_brands($intId);
foreach ($query as $row)
{
$data['title'] = $row['my_brand'];
$data['description'] = $row['description'];
$data['breadcrumb'] = $row['my_brand'];
$data['images'] = 'brands/' . $row['intId'];
}
$data['section_page'] = 'Brands';
$data['section_page_link'] = 'brands';
$data['items1'] = $this->Products_model->items1_brands($data['per_page'], $data['offset_no']);
$this->template->load_header('products/header_view', $data);
$this->template->load_mainview('products/mainview_view', $data);
$this->template->load_grid('products/grid_view', $data);
$this->template->load_footer('footer_view', $data);
}
}
答案 0 :(得分:2)
public function products_lookup_by_id($intId)
{
$config = array(
'base_url' => base_url() . '/brands/' . $intId . '/',
'total_rows' => $this->Products_model->total_rows_brands($intId),
'per_page' => 25,
'uri_segment' => 3
);
if (empty($config['total_rows'])) {
redirect('/errors');
}
}
答案 1 :(得分:0)
验证total_rows
值,在$config
初始化
if (empty($config['total_rows'])) {
// redirect
}
答案 2 :(得分:0)
您可以修改$ this-&gt; Products_model-&gt; total_rows_brands($ intId)函数:
function total_rows_brands($intId)
{
if (! is_numeric($intId)) {
return FALSE;
}
$query = $this->db->get_where('your_table', array('id' => $intId));
if (! $query) {
return FALSE;
}
....
}
然后在你的控制器中:
public function products_lookup_by_id($intId)
{
$data = $this->Products_model->total_rows_brands($intId);
if (! $data) {
redirect('your_500_error_controller');
}
答案 3 :(得分:0)
负载&GT;模型(&#39; Products_model&#39); } public function products_lookup_by_id($ intId) { $ config = array( &#39; BASE_URL&#39; =&GT; base_url()。 &#39; /品牌/&#39; 。 $ intId。 &#39; /&#39 ;, &#39; TOTAL_ROWS&#39; =&GT; $这 - &GT; Products_model-&GT; total_rows_brands($ INTID), &#39; per_page&#39; =&GT; 25, &#39; uri_segment&#39; =&GT; 3 );
############# This code for $intId record check ####
$productRecord = $this->Products_model->total_rows_brands($intId);
if(!count($ productRecord)&gt; 0){
$ heading =&#34; 404 Page Not Found&#34 ;;
$ message =&#34;找不到您请求的页面。&#34 ;;
// show_error($ heading,$ message);
show_error($ message,500,$ heading);
}
#############产品记录检查代码结束###
$this->pagination->initialize($config);
$data = array(
'offset_no' => $this->uri->segment(3),
'total_rows' => $config['total_rows'],
'per_page' => $config['per_page'],
'nav1' => $this->Products_model->nav1(),
'nav2' => $this->Products_model->nav2(),
'nav3' => $this->Products_model->nav3(),
'nav4' => $this->Products_model->nav4(),
'nav5' => $this->Products_model->nav5(),
'nav6' => $this->Products_model->nav6(),
'nav7' => $this->Products_model->nav7()
);
if ($data['total_rows'] > 0)
{
$data['from'] = $data['offset_no'] + 1;
}
else
{
$data['from'] = 0;
}
if (($data['total_rows'] - $data['offset_no']) < $data['per_page'])
{
$data['to'] = ($data['offset_no'] + ($data['total_rows'] - $data['offset_no']));
}
else
{
$data['to'] = $data['offset_no'] + $data['per_page'];
}
$query = $this->Products_model->query_brands($intId);
foreach ($query as $row)
{
$data['title'] = $row['my_brand'];
$data['description'] = $row['description'];
$data['breadcrumb'] = $row['my_brand'];
$data['images'] = 'brands/' . $row['intId'];
}
$data['section_page'] = 'Brands';
$data['section_page_link'] = 'brands';
$data['items1'] = $this->Products_model->items1_brands($data['per_page'], $data['offset_no']);
$this->template->load_header('products/header_view', $data);
$this->template->load_mainview('products/mainview_view', $data);
$this->template->load_grid('products/grid_view', $data);
$this->template->load_footer('footer_view', $data);
}
}