Sql and PHP Friendly Url

Asked

Viewed 1,625 times

9

I have the following php code to make the urls of my site friendly:

<?php
 $atual = (isset($_GET['pg'])) ? $_GET['pg'] : 'home';
 $permissao = array('home', 'contato', 'sobre', 'politica');
 $pasta = 'arquivos';
   if (substr_count($atual, '/') > 0){
     $atual = explode('/', $atual);
     $pagina = (file_exists("{$pasta}/".$atual[0].'.php') && in_array($atual[0], $permissao)) ? $atual[0] : 'erro';
   } else {
       $pagina = (file_exists("{$pasta}/".$atual.'.php') && in_array($atual, $permissao)) ? $atual : 'erro';
   }
?>

My . htaccess is like this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?pg=$1 [L]

For static pages it works perfectly. The problem now is to adapt this code to dynamic pages.

On my website I will need a category.php page and company.php that will receive the 'Slug' value from my database.

Urls would look that way:
site.com.br/category.php? Slug=category-name
site.com.br/company.php? Slug=company name

How to turn these urls into:
site.com.br/category name
site.com.br/company/company name

How I should make the sql call on these pages to display the values of the database?
Can someone please help me?

  • 1

    Your question is about SQL or how should the .htaccess?

  • I need to know what the sql call would look like on the category.php and company.php pages. And in case this modification changes something in htaccess I needed to know how the new file would look.

  • the way you’re doing, you’re letting PHP do Apache’s work, I suggest you read Learning Friendly Urls with Complex Rules

  • In the .htaccess writes a new expression for url, and sets the parameters in the category.php and enterprise.php

1 answer

2

I had the same experience with a client once.

For that I did it this way and it worked:

index php.

$pagina = $_GET['pg']; //peguei a página.php

$pagina = (isset($pagina)) ? $pagina : 'home';

//agora vou verificar se exite algum valor depois da '/'. 
if(substr_count($pagina , '/') > 0) {

    //se existir, vou atribuir o valor à uma variável que chamarei de $slug aonde farei o get mais tarde.
    $pagina = explode('/',$pagina);
    $slug= $pagina[1]; // repare que a posição do valor é a segunda, ou seja, depois da barra
    include($pagina[0].".php") ;// inclua a pagina que o usuário escolheu (exemplo: empresa.php)

} else {

     // se não existir, o $slug será nulo, mas mesmo assim incluiremos a pagina.php.
     $slug = 0; 
     include($pagina.".php") ; 

}

That is, through this code, you can get the Slug when it exists in the url.

Example with the url:

www.site.com.br/empresa/stack-exchange

php company.

<?php

$selecionaEmpresa = mysql_query("SELECT * FROM empresas WHERE slugEmpresa = '$slug'");
//verifica se não existe esta empresa no DB
if(mysql_num_rows($selecionaEmpresa) == 0){
    //Se não existir inclua uma pagina geral estática (pode ter a lista das empresas por exemplo)
    include(geral.php);

} else {
    // mas se existir ele fará um looping e resgatar as informações desta empresa no DB
    while($empresaSelecionada = mysql_fetch_array($selecionaEmpresa)){
?>

    <div><?php echo $empresaSelecionada['conteudo']; ?> </div>

    <?php 
    } } 
    //fecha o else e o looping 
    ?>

with the category is the same thing without modifying the index

only the file will be called category.php

with the link:

www.site.com.br/categoria/nome-da-categoria

you will make another selection

$selecionaEmpresa = mysql_query("SELECT * FROM empresas WHERE categoriaEmpresa = '$slug'");

good luck!

Browser other questions tagged

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