For your case, the solution would be demonstrated here, but because you do not know exactly what the structure of your table is, I will rely on the details provided in the comments. Although this is using a different table, the example is very simple. The only problem you would have would be slug
which is basically the title of the article, but with some arrangements, it has this , and this here .
config.php
<?php
// configuracao (nao importante)
$config = array(
'mysql' => array(
'host'=> 'localhost',
'usr' => 'root',
'pwd' => '',
'db' => 'nuwie'
),
'site' => array(
'nome' => 'Site sem Nome',
'inicio' => 'http://127.0.0.1:8080' . dirname($_SERVER['SCRIPT_NAME'])
)
);
// agilizar a busca dos valores da configuração (nao importante)
function conf($nome){
$p = preg_split('/[\s-@]/', $nome);
global $config;
foreach($config as $nome => $valor){
if($nome === $p[0] && array_key_exists($p[1], $valor)){
return $config[$nome][$p[1]];
}
}
return false;
}
// conexao
$mysqli = new mysqli(conf('mysql-host'), conf('mysql-usr'), conf('mysql-pwd'), conf('mysql-db'));
if($mysqli->errno){
die('Erro ao conectar banco de dados');
}
// buscar todos os artigos
function buscarArtigos($tabela){
global $mysqli;
$resultados = array();
if($stmt = $mysqli->query("SELECT id,titulo,seo_url FROM {$tabela}")){
if($stmt->num_rows > 0){
while($rs = $stmt->fetch_assoc()){
array_push($resultados, $rs);
}
return $resultados;
}
}
return false;
}
// buscar um artigo pela sua ID
function buscarArtigo($id, $tabela){
global $mysqli;
if($stmt = $mysqli->query("SELECT * FROM {$tabela} WHERE id = {$id}")){
if($stmt->num_rows > 0){
return $stmt->fetch_assoc();
}
}
return false;
}
// reajustar os titulos para que se adequem a url
// retirada de: http://www.visualscope.com/seo-friendly-urls.html
function friendly_seo_string($vp_string){
$vp_string = trim($vp_string);
$vp_string = html_entity_decode($vp_string);
$vp_string = strip_tags($vp_string);
$vp_string = strtolower($vp_string);
$vp_string = preg_replace('~[^ a-z0-9_.]~', ' ', $vp_string);
$vp_string = preg_replace('~ ~', '-', $vp_string);
$vp_string = preg_replace('~-+~', '-', $vp_string);
return $vp_string;
}
This file here basically contains the only two functions you would need to make the idea work Search and search >, which is the most (not important) created in the heat of the moment since they were simple except for the last one that can be useful and is not my own. The friendly_seo_string function is only required when you want to create a new article, for example:
- When you create a new article, in the database table for the
titulo
and conteudo
fields you can respectively use the desired title and content, the same applies to the adicionado
field, but to the field seo_url
would have to be the return of this function passing the title used as argument friendly_seo_string (title_used_no_article) . Or if you prefer, you can always write your own function, so you can do the same thing.
.htaccess
RewriteEngine On
RewriteRule ^artigos/[0-9]\/(.*)$ ver_artigo.php?artigo=$0
For this example you would not need too much in the .htaccess file, only these two lines would suffice, or you could still check that the rewrite
module is enabled before enabling it.
SQL table
CREATE TABLE 'artigos' (
'id' int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
'titulo' varchar(60) NOT NULL,
'conteudo' text NOT NULL,
'seo_url' varchar(60) NOT NULL,
'adicionado' datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
index.php
<?php
require_once 'config.php';
print "<a href=\"" . conf('site-inicio') ."\"><h1>" . conf('site-nome') . "</h1></a>";
// - inicio do conteudo
if(($artigos = buscarArtigos('artigos')) != false){
foreach($artigos as $artigo){
print "<div id=\"artigo\">";
print "<a href=\"artigos/{$artigo['id']}/{$artigo['seo_url']}\" target=\"_blank\"><h4>{$artigo['titulo']}</h4></a>";
print "</div>";
}
} else {
print "Nenhum artigo encontrado";
}
// - fim do conteudo
?>
The index.php file simply provides the titles and links of existing articles.
Attention to link to the article:
print "<a href=\"artigos/{$artigo['id']}/{$artigo['seo_url']}\" target=\"_blank\"><h4>{$artigo['titulo']}</h4></a>";
NA% of what you store, for articles, must precede url
at the beginning of href .
ver_artigo.php
<?php
require_once 'config.php';
if(isset($_GET['artigo'])){
$artigo_id = explode('/', $_GET['artigo']);
if(($artigo = buscarArtigo($artigo_id[0], 'artigos')) != false){
print "<div id=\"artigo\">";
print "<h2>{$artigo['titulo']}</h2>";
print $artigo['conteudo'];
print "</div>";
} else {
print "Artigo não encontrado";
}
print "<p><a href=\"" . conf('site-inicio') ."\">voltar</a></p>";
} else {
print "<h1>Erro</h1>";
}
?>
This part, would be the equivalent of your page visual.pph . Search the details of this article using your artigos
and return them, if you can not find the article, provide a message that you could not find the article.
In this case, your id
would be in this format similar to ptSO:
www.nomedomeusite.com/artigos/id/title-of-article
For the format:
www.nomedomeusite.com/companies/any-company
The url
itself should be matched instead of slug
on the page that shows the details of the selected item.
ps: I worked on the idea, but I did not give a damn about the security factor, so I recommend first of all that you search mainly about .htaccess , and of course, you can always adapt or even , improve the code so that it works more closely with what you want. If there are still any questions, leave them in the comments, and good luck.
Recommended Reading
Apache