2015-11-24 25 views
0

能否請你幫我弄清楚如何創建PHP父和子頁之間的動態聯繫如何在PHP之間創造家長和子頁面動態超鏈接

在我的情況

我有一個4頁像

category.php 
subCat.php 
products.php 
single.php 

與數據庫結構是這樣的:

enter image description here

Category擁有一個與SUBCAT 0一對多的關係一個SUBCAT有一個與產品 一個產品的許多關係,有一個與單

現在我想很多關係,知道我怎麼可以從category.php鏈接到每個slected subCat.php

<?php 
echo '<a href="url">Comedy</a>'; 

等等? 謝謝

+1

有根據生產解決方案的多種方式,形成高度的意見答案。你有什麼嘗試? –

+0

感謝iam的回覆。我希望能夠理解的是以WordPress的方式創建一個簡單的CMS。 1 – Behseini

+0

wordpress很大程度上依賴使用url querystrings和post querystrings將數據和動作從頁面'a'移動到頁面'b',一個體面的解決方案是將每個步驟的鏈接轉換爲一個包含被點擊的ID的URL查詢字符串的url。使用你的例子,你的url類似於'url/subCat.php?id = 3',其中id'3'對應於數據庫中的'comedy'子類別,從那裏你將生成鏈接, ''''單' –

回答

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); 

模型

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(); 
} 

希望這有助於你

相關問題