2016-01-17 68 views
0

你好,我已經創造清潔網址 ,但我想要的東西,得到動態的URL並獲得動態查詢如何實現動態URL並獲得動態查詢

我要像鏈接 此 http://localhost:8081/olshop/products/param/[category]-[gender]

sample : http://localhost:8081/olshop/products/param/pants-m 

    with paging 
    http://localhost:8081/olshop/products/param/[category]-[gender]/paging 
    sample : http://localhost:8081/olshop/products/param/pants-m/1 

但此用戶可以像這樣更換過濾器

 http://localhost:8081/olshop/products/param/[category] 
    sample : http://localhost:8081/olshop/products/param/pants 
    with paging 
    http://localhost:8081/olshop/products/param/[category]/paging 
    sample : http://localhost:8081/olshop/products/param/pants/1 

or 
    http://localhost:8081/olshop/products/param/[gender] 
    sample : http://localhost:8081/olshop/products/param/m 
    with paging 
    http://localhost:8081/olshop/products/param/[gender]/paging 
    sample : http://localhost:8081/olshop/products/param/m/1 

or 
    http://localhost:8081/olshop/products/param/[gender]-[category] 
    sample : http://localhost:8081/olshop/products/param/m-pants 
    with paging 
    http://localhost:8081/olshop/products/param/[gender]-[category]/paging 
    sample : http://localhost:8081/olshop/products/param/m-pants/1 

how to technique li這個?

我困惑,實施到我的代碼和的.htaccess

這是我目前的.htaccess

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteRule index$           index.php [L] 
    RewriteRule ^products/(\d+)$        index.php?p=products&page=$1 [L] 
    RewriteRule ^products/param/([^/]+)/?$      index.php?p=products&param=$1 [L] 
    RewriteRule ^products/param/(.*)\/(\d+)$     index.php?p=products&param=$1&page=$2 [L] 
    RewriteRule ^([^/.]+)/?$         index.php?p=$1 [QSA,L] 
</IfModule> 

,這是我的我的代碼

<?php 
     if($_GET['param']!=NULL) 
     { 
      $query = mysqli_query($con,"select * from goods where category='".$_GET['param']."' and gender='".$_GET['param']."' order by date_publish DESC LIMIT $start, $limit"); 
     } 
     else 
     { 
      $query = mysqli_query($con,"select * from goods order by date_publish DESC LIMIT $start, $limit"); 
     } 
    ?> 

幫助我感謝的

部分

或者您有解決方案或其他?

回答

0

您遇到的問題是您需要確定用戶是否要求提供性別,類別或兩者兼有。由於你的性別是通用的和有限的; m =男性,f =女性,a =全部例如,並且它們都是一個字符長度,您最好解析該參數並查詢頁面是否存在。

你在這裏遇到的唯一真正的問題是你的性別/類別位置可以改變或反向,這意味着如果兩者都提供,那麼你需要查詢這兩個類型的兩個部分,如果這是必需的,那麼你可以使用下面的模式匹配來獲得性別和類別,或者如果您決定格式,那麼只需使用其中之一。

$param = 'pants-m'; //$_GET['param']; 

if(preg_match('/^([m|f])(?:[-](.*))?$/i', $param, $match)) { 
    $gender = $match[1]; 
    $category = isset($match[2]) ? $match[2] : null; 
} 
elseif(preg_match('/^([a-z0-9]{2,})(?:[-]([m|f]))?$/i', $param, $match)) { 
    $gender = isset($match[2]) ? $match[2] : null; 
    $category = $match[1]; 
} 

var_dump($gender, $category);