in_array with friendly URL

Asked

Viewed 77 times

0

I’m applying Url friendly with explode, doing as follows:

$url = isset( $_GET['url'] ) ? addslashes( $_GET['url'] ) : '';
$url = rtrim( $url, '/');
$explode = explode('/',$url);

Everything works perfectly, I’m already doing it dynamically, making the products look like this:

http://localhost/produtos/camara-de-ar

STRUCTURE:

The idea is that when you don’t find the url, in this case, the product (e.g., camera-to-air test), displays the error page, and this works perfectly. However, the error already reported above is presented before the inclusion of the file.

} elseif($explode[0] == 'produtos'){ // se for igual a produtos
    if(isset($explode[1]) && in_array($categorias[$explode[1]], $categorias) || !isset($explode[1]) || $explode[1] == ''){
        include ("pages/".$explode[0].".php");
    } else {
        include("pages/erro.php");
    }
}

THE MISTAKE:

Notice: Undefined index: camara-de-vento in C:\wamp\www\index.php on line 126

Only the line 126:

 if(isset($explode[1]) && in_array($categorias[$explode[1]], $categorias)){

That is: if there are [products] and the typed category (wind camera) exists in the $categories array, include the products page, if not, include the error.

As already said, everything works.. My idea is to treat the error page, which until then is being included, but together with this the error already informed.

  • 1

    if(isset($explode[1]) && isset($categories[$explode[1]]) && in_array($categories[$explode[1]], $categories)){

  • Hello! So simple and me killing myself.. was just checking if it also existed, before knowing if it was in the array.. anyway, thanks!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.