我查看了从我的数据库中提取的几条记录。我实现了记录的分页,每页显示3条记录,并且工作正常。现在我希望提供一个下拉菜单,以便在记录的升序和降序之间切换。默认视图按升序排列。现在的问题是,当我从下拉列表中单击降序选项时,记录将更改为降序,但它会将我带到第一页。
当我更改记录的顺序时,我需要保持在同一页面上。有没有办法使用会话实现这一目标。因为我希望在不使用ajax的情况下解决这个问题。任何帮助,将不胜感激。谢谢。
这是我的控制器:
<?php
class Datalist extends CI_Controller{
public function __Construct(){
parent::__Construct();
$this->load->model('myapp/list_model');
$this->load->helper(array('form','url'));
$this->load->library('session');
$this->load->library('pagination');
}
public function index(){
if(!isset($_SESSION["name"])){
redirect("myapp/login");
}
$config['base_url'] = base_url()."index.php/myapp/datalist/index"."/";
$config['total_rows'] = $this->list_model->get_count();
$config['per_page'] = 3;
$config['uri_segment'] = 4;
$config['use_page_numbers'] = TRUE;
$config["num_links"] = 2;
$this->pagination->initialize($config);
if($this->input->post('order')){
$_SESSION['order'] = $this->input->post('order');
}
if(isset($_SESSION['order'])){
$order = $_SESSION['order'];
}else{
$order = "ASC";
}
$limit = 3;
$offset = ($this->uri->segment(4))?$this->uri->segment(4):0;
$data['result'] = $this->list_model->get_list($offset,$limit,$order);
$data['games'] = $this->list_model->get_games();
$data['map'] = $this->list_model->get_map();
$data['links'] = $this->pagination->create_links();
$this->load->view('myapp/list_view,$data);
}
}
?>
这是我的模特
class List_model extends CI_Model{
public function __Construct(){
$this->load->database();
}
public function get_list($offset, $limit, $order){
$offset = $offset-1;
if($offset<0){
$offset = 0;
}
$from = $offset * $limit;
$query = $this->db->query("SELECT userData.Id,
userData.FirstName,
userData.LastName,
genderList.name,
userData.Email,
userData.Dob,
country.countryName,
state.stateName,
city.cityName,
userData.Pincode
FROM userData
LEFT JOIN country
ON userData.Country = country.countryId
LEFT JOIN state
ON userData.State = state.stateId
LEFT JOIN city
ON userData.City = city.cityId
LEFT JOIN genderList
ON userData.Gender = genderList.genderId
ORDER BY userData.FirstName $order
LIMIT $limit OFFSET $from ");
return $query->result();
}
public function get_games(){
$query = $this->db->query("select * from games");
return $query->result_array();
}
public function get_map(){
$query = $this->db->query("select * from mapGame");
return $query->result_array();
}
public function get_count(){
return $this->db->count_all("userData");
}
}
?>
我的观点
<div class = "list">
<table>
<thead>
<tr>
<th>FirstName</th>
<th>LastName</th>
<th>Gender</th>
<th>Email</th>
<th>DOB</th>
<th>Interest</th>
<th>Country</th>
<th>State</th>
<th>City</th>
<th>Pincode</th>
</tr>
</thead>
<tbody>
<?php foreach($result as $data): ?>
<tr>
<td><?php echo $data->FirstName; ?></td>
<td><?php echo $data->LastName; ?></td>
<td><?php echo $data->name; ?></td>
<td><?php echo $data->Email; ?></td>
<td><?php echo $data->Dob; ?></td>
<td><?php
foreach($map as $mapping):
if($data->Id == $mapping["userId"]){
foreach($games as $name):
if($mapping["GameId"] == $name["gameId"]){
echo $name["name"]."<br>";
}
endforeach;
}
endforeach;
?>
</td>
<td><?php echo $data->countryName; ?></td>
<td><?php echo $data->stateName; ?></td>
<td><?php echo $data->cityName; ?></td>
<td><?php echo $data->Pincode; ?></td></tr>
<?php endforeach; ?>
</tbody>
</table>
<div align="center" style="margin-top:10px;background-color: #F1F8E0">
<?php echo $links; ?>
</div>
</div>
<div class = "order">
<?php
if(isset($_SESSION["order"])){
if($_SESSION['order'] == 'ASC'){
$order = 'ASC';
}else{
$order = 'DESC';
}
}else{
$order = 'ASC';
}
echo form_open('myapp/datalist/index');
echo form_label('Order');
echo form_dropdown('order',array('ASC'=>'Ascending',
'DESC'=>'Descending'),$order,array('onclick'=>'this.form.submit()'));
echo form_close();
?>
</div>