问题描述:

Here are some examples of url

 0 | 1 | 2

-----------------------------

.com / en / page=x / product

.com / en / category / product

.com / en / category / page=x

.com / en / category / subcategory

Based on the word(-s) after particular / I decide what to fetch from mysql. For example if I see product I connect to products table and fetch one product. If I seesubcategory, I connect toproductstable and fetch all products where insubcategorycolumn is name ofsubcategory`.

I need decide, for example, what is after third trailing slash (product, page=x, or subcategory).

Now I at first remove last /

$url_slashes = rtrim( (ltrim($_SERVER['QUERY_STRING'],'/')) ,'/');

Then create array

$array_url_slashes = explode( "/", $url_slashes );

And then like this

if ( strpos( $array_url_slashes[2] ,'page=') !== false ) {

then it is page and so on.

This works if I limit maximum number of subcategories (or array elements).

But what would be solution for unlimited subcategories. I need not so much code, but need idea how all this works.

It would be simply for example .com/en/c=category/s1=subcategory/s2=next-subcategory

After searching google found this http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

Appears, that there is a good idea.

For example url:

.com/en/electronics/portable-electronics/mp3-players/flash

And I need to fetch Title, Description, url for each element of url (for example, to create breadcrumbs)

All categories and subcategories are located in one mysql table

1) create variable from url $url_slashes = rtrim( (ltrim($_SERVER['QUERY_STRING'],'/')) ,'/');

2) create array $array_url_slashes = explode( "/", $url_slashes );

3) detect number of last element (last /) $num_arr_trail_slash = count($array_url_slashes);

4) mysql query like this

SELECT

t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4,

t1.url AS url1, t2.url as url2, t3.url as url3, t4.url as url4

FROM CategoryForTest AS t1

LEFT JOIN CategoryForTest AS t2 ON t2.parent = t1.category_id

LEFT JOIN CategoryForTest AS t3 ON t3.parent = t2.category_id

LEFT JOIN CategoryForTest AS t4 ON t4.parent = t3.category_id

WHERE t4.name = ?;

? is 'FLASH' or $array_url_slashes[$num_arr_trail_slash-1]

And get such php array

Array

(

[0] => Array

(

[lev1] => ELECTRONICS

[lev2] => PORTABLE ELECTRONICS

[lev3] => MP3 PLAYERS

[lev4] => FLASH

[url1] => url-electronics

[url2] => url-portable-electronics

[url3] => url-mp3-players

[url4] => url-flash

)

)

网友答案:

If you have an htaccess that rewrites your URL like this:

RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?page=$1&lang=$2&action=$3 [L] 
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?page=$1&lang=$2 [L] 
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

By rewriting your URL, it means you can always call the parameters as how you define them. In this example, I always go over index.php, next parameter is $page, next one is $lang and then I defined my $action.

So url example: www.yoururl.com/products/en/product.

So what can you do by using this htaccess, you can call the url variables like:

$_REQUEST['page']  == 'products'
$_REQUEST['lang']  == 'en'
$_REQUEST['action']  == 'product'

So based on the page name, you know what you have to show the user or what is comming, and based on action, you know what you are going to show / call.. .

You can just do it like:

if(isset($_REQUEST['action']){
    switch($_REQUEST['action']{
        case 'product':
            //DO STUFF
            break;
    }
}

That's the basic idea.. Is this what you want?

相关阅读:
Top