当我点击视图中的链接时,它显示没有找到任何页面。任何人都可以帮助我吗?还告诉我为什么那些链接不起作用?数据显示没问题。但是转到表的其他页面的链接却无法正常工作。当我点击“1,2,3”页面未找到。它仍然在同一个地方..我猜它会发布或得到问题?。
这是我的控制者。
<?PHP
/**
*
*/
class Search extends MY_Controller
{
public function index ()
{
if ($this->form_validation->run('search') == FALSE)
{
$this->load->model('City');
$deals=$this->City->default_city();
foreach ($deals as $key)
{
$city_id=$key->city_id;
$city_name=$key->city_name;
$city_status=$key->city_status;
}
}
else
{
$city_name=$this->input->post('city_name');
$this->load->model('City');
$city_id=$this->City->other_city($city_name);
foreach ($city_id as $row)
{
$city_id= $row->city_id;
$city_name=$row->city_name;
$city_status=$row->city_status;
}
}
$deal_name=$this->input->post('deal_name');
$data['city_id']=$city_id;
$data['city_name']=$city_name;
$data['city_status']=$city_status;
$this->load->model('Deals');
$total_deals=$this->Deals->total_deals();
$config=[
'base_url' => base_url().'Search',
'per_page' => 3,
'total_rows' => $this->Deals->number_rows($deal_name,$city_id,$city_status),
'uri_segment'=> 2,
];
$this->pagination->initialize($config);
$user_search=$this->Deals->user_search($deal_name,$city_id,$city_status,$config['per_page'],$this->uri->segment(2));
$data['links']=$this->pagination->create_links();
$data['deals']=$user_search;
$data['total_deals']=$total_deals;
$total_categories=$this->Deals->total_categories();
$data['total_categories']=$total_categories;
for($i=1;$i<=$total_categories;$i++)
{
$deals_by_categories[]=$this->Deals->deals_by_categories($i);
}
$category_name=$this->Deals->categories();
//$category_name = json_decode(json_encode($category_name), true);
$data['category_name']=$category_name;
$data['deals_by_categories']=$deals_by_categories;
$this->load->view('page-search',$data);
}
}
?>
这是我的模特函数
public function user_search($deal_name,$city_id,$city_status,$limit,$offset)
{
$query= $this->db->select('merchants.merchant_logo,deals.deal_title,deals.deal_description,deals.start_date,deals.end_date,deal_id')
->from('deals')
->join('merchants_branches', 'deals.branch_id=merchants_branches.branch_id', 'inner')
->join('merchants', 'merchants_branches.merchant_id=merchants.merchant_id', 'inner')
->join('cities','cities.city_id=merchants_branches.city_id','inner')
->where('merchants_branches.city_id',$city_id)
->where('cities.city_status',$city_status)
->where('deal_status', 1)
->like('deals.deal_name',$deal_name)
->limit($limit,$offset)
->get();
return $query->result();
}
public function number_rows($deal_name,$city_id,$city_status)
{
$query= $this->db->select('merchants.merchant_logo,deals.deal_title,deals.deal_description,deals.start_date,deals.end_date,deal_id')
->from('deals')
->join('merchants_branches', 'deals.branch_id=merchants_branches.branch_id', 'inner')
->join('merchants', 'merchants_branches.merchant_id=merchants.merchant_id', 'inner')
->join('cities','cities.city_id=merchants_branches.city_id','inner')
->where('merchants_branches.city_id',$city_id)
->where('cities.city_status',$city_status)
->where('deal_status', 1)
->like('deals.deal_name',$deal_name)
->get();
return $query->num_rows();
}
这是我的观点
<?php
if(count($deals)):
foreach ($deals as $deals) :
date_default_timezone_set("Asia/Dubai");
$date=date('Y-m-d H:i:s');
$enddate=$deals->end_date;
$date = new DateTime($date);
$enddate = new DateTime($enddate);
// The diff-methods returns a new DateInterval-object...
$diff = $date->diff($enddate);
if($date<$enddate):
// Call the format method on the DateInterval-object
?>
<a class="col-md-4 model-1" href="<?php echo base_url(); ?>DealDescription/<?php echo $deals->deal_id ?>/<?php echo $city_status?>">
<div class="product-thumb model-2">
<header class="product-header">
<img src="<?php echo base_url(); ?>img/logos/<?php echo $deals->merchant_logo ?>" alt="Image Alternative text" title="Food is Pride" />
</header>
<div class="product-inner">
<h5 class="product-title"><?php echo $deals->deal_title ?></h5>
<p class="product-desciption"><?php echo $deals->deal_description ?></p>
<div class="product-meta"><span class="product-time"><i class="fa fa-clock-o"></i> <?php echo $diff->format('%a Day and %h hours');?></span>
<ul class="product-price-list">
<li><span class="product-price">$118</span>
</li>
<li><span class="product-old-price">$227</span>
</li>
<li><span class="product-save">Save 52%</span>
</li>
</ul>
</div>
<p class="product-location"><i class="fa fa-map-marker"></i> <?php echo $city_name;?></p>
</div>
</div>
</a>
<?php else: header('location:'.base_url().'SearchError'); endif; endforeach;?>
<?php else: header('location:'.base_url().'SearchError');
endif;
?>
<?php echo $links; ?>
答案 0 :(得分:0)
由于CodeIgniter的工作方式,以及您实际上无法在控制器的索引方法中使用URI段,因此您需要将搜索移动到其他方法。如果您执行以下操作,这非常简单:
public function index()
{
$this->results();
}
public function results( $page = 1 )
{
// your code
}
现在请注意,您现在实际上在URI段3中有一个页面变量。这很重要,因为CodeIgniter将根据配置查找该页面:
$config['uri_segment'] = 3; // You can't use 2!
此外,在CodeIgniter中使用site_url非常重要,而对于此分页base_url不是base_url:
$config['base_url'] = site_url('search/results'); // Do not uppercase "search"
此外,如果您希望页面具有自然的用户体验,则应使用页码:
$config['use_page_numbers'] = TRUE;
采用这种方法,您仍然需要确定您的查询偏移量,因为它显然不是页码:
$offset = ( $page * $config['per_page'] ) - $config['per_page'];
这应该可以让你立刻进行分页。