如何实现动态url并获取动态查询

时间:2016-01-17 20:27:06

标签: php

您好我已经创建了干净的网址    但我希望获得动态网址并获得动态查询

我想要链接       像这样       http://localhost:8081/olshop/products/param/[category]-[gender]

     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

但是此用户可以像这样更改过滤器

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

如何像这样的技术?

我对我的代码和.htaccess

的实现感到困惑

这是我目前的.htaccess

<?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");
         }
    ?>

这是我的代码

CMD

帮我谢谢

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

1 个答案:

答案 0 :(得分:0)

您遇到的问题是您需要确定用户是否要求提供性别,类别或两者。因为你的性别是通用的和有限的; m = male,f = female,a = all例如,它们都是一个字符长,你最好只解析那些参数并查询页面是否存在。

您在这里遇到的唯一真正问题是您的性别/类别位置可以更改或反转,这意味着如果两者都提供,那么您需要查询这两种类型的两个部分,如果需要,那么您可以使用以下两个模式匹配以获取性别和类别,或者如果您决定使用其中一种格式,那么只需使用其中一种格式。

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