您能否帮我弄清楚如何在PHP父页面和子页面之间创建动态链接
在我的情况下,我有4页像category.php
subCat.php
products.php
single.php
使用如下数据库结构:
类别与subCat有一对多的关系 subCat与产品有一对多的关系 产品与单个
具有一对多的关系现在我想知道如何建立从category.php到每个选定的subCat.php的链接
<?php
echo '<a href="url">Comedy</a>';
等等? 感谢
答案 0 :(得分:0)
以下是分页的示例,其中url是动态的,但更重要的是理解动态URL的逻辑。所有工作都是这一部分
if($categoryID == null){
redirect('backend/subcategories/all/filterAll');
}
if(!isset($categoryID) || ( $categoryID == "filterAll" )) {
$data['subcategories'] = $this->model_m->get_all_subcategories();
} else {
$data['subcategories'] = $this->model_m->get_by_category($categoryID);
}
$data['selected_category'] = $categoryID;
控制器
$this->load->library('pagination');
$pagination_config['base_url'] = base_url('/webs/webs1/all/');
$pagination_config['total_rows'] = $this->model_m->count_all();
$pagination_config['per_page'] = 2;
$pagination_offset = $this->uri->segment(5);
$this->pagination->initialize($pagination_config);
$data['pagination_links'] = $this->pagination->create_links();
//end
//fetch categories from the database
$data['categories'] = $this->model_m->get_categories();
$categoryID = $this->uri->segment(4);
if($categoryID == null){
redirect('backend/subcategories/all/filterAll');
}
if(!isset($categoryID) || ( $categoryID == "filterAll" )) {
$data['subcategories'] = $this->model_m->get_all_subcategories();
} else {
$data['subcategories'] = $this->model_m->get_by_category($categoryID);
}
$data['selected_category'] = $categoryID;
$data['view'] = $this->load->view($view,$data,true);
$this->load->view($viewlayout,$data);
MODEL
public function get_by_category($id){
$query = $this->db->get_where('items', array('prod_id' => $id));
if ($query->num_rows() > 0) { return $query->result(); }
return false;
}
public function get_all_subcategories($catID = NULL,$limit = NULL ,$offset = NULL ) {
if(isset($catID) && is_int($catID)) {
if(isset($limit) && isset($offset)){
$result = $this->db->get_where('items',array('prod_id' => $catID),$limit,$offset);
return $result->result();
}
$result = $this->db->get_where('items',array('prod_id' => $catID));
return $result->result();
}
if(isset($limit) && isset($offset)){
$result = $this->db->get('items',$limit,$offset);
return $result->result();
}
$result = $this->db->get('items');
return $result->result();
}
public function count_all() {
return $this->db->get('items')->num_rows();
}
希望这有助于你