Maestro Vladimir
Maestro Vladimir

Reputation: 1196

How to implementation dynamic url and get dynamic query

Hello i already create clean url but i want something get dynamic url and get dynamic query

i want link like this 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

but this user can change filter like this

     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 like this ??

i confused to implementation to my code and .htaccess

this is my current .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>

and this is my part of my code

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

help me thank's

or you have a solution or other ?

Upvotes: 0

Views: 46

Answers (1)

user2762134
user2762134

Reputation:

The problem you have is that you need to determine if the user has asked for a gender, category or both. Since your genders are generic and limited; m = male, f=female, a = all for example and they are all one character long you are better of just parsing that parameters and querying for the page if it exists.

The only real issue you face here is that your gender / category positions can change or reverse which means if both are supplied then you need to query both parts for both types, if this is required then you can use both of the below pattern matches to get the gender and category or if you decide on a format then just use one of them.

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

Upvotes: 0

Related Questions