1
I am creating a page with image gallery with database, and when clicking an image, is redirected to the page profile.php
with the image data, as if it were a profile.
I want to make my URL more beautiful, currently it is appearing like this:
localhost/meusite/profile?slug=nome-da-imagem
I’d like to leave it at that:
localhost/meusite/nome-da-imagem
I will post the structure and part of the code to better understand, it is difficult for me to find a solution. The file structure is like this:
- Meusite
- css
- css style.
- js
- script js.
- pages
- home
- profile
- cPanel
- pages
- uploads
- .htaccess
- config.php
- index php.
- css
in config.php:
define('INCLUDE_PATH', 'http://localhost/meuSite/');
define('INCLUDE_PATH_PAINEL',INCLUDE_PATH.'painel/');//usei esses
INCLUDE_PATH pra nao precisar configurar muito ao passar para o servidor.
define('BASE_DIR_PAINEL',__DIR__.'/painel');
//conexao banco de dados
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','meusite');
index.php: (I will not put all HTML tags so as not to take up too much space)
<header>
<?php
$url = isset($_GET['url']) ? $_GET['url'] : 'home';
if(file_exists('pages/'.$url.'.php')){
include('pages/'.$url.'.php');
}else{
echo 'essa página não existe!';
}
?>
<footer>
</footer>
in htaccess:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Options -Indexes
in php home.:
<section>
//conexão do banco
<?php
//MySql::banco() = função para conectar no meu banco de dados.
$bd = MySql::Banco()->prepare("SELECT * FROM `nome_tabela`");
$bd->execute();
$imagens = $bd->fetchAll();
foreach($imagens as $key => $value){
$slug = $value['slug'];
?>
<a href="<?php echo INCLUDE_PATH; ?>profile?slug=<?php echo $slug?>"></a>
<?php }?>
on the profile page.php:
<?php
$slug = $_GET['slug'];
if($slug == $slug){
$bd = MySql::Banco()->prepare("SELECT * FROM `nome_tabela ` WHERE slug =
?");
$bd->execute(array($slug));
$imagens = $bd->fetchAll();
foreach ($imagens as $key => $value) {
?>
<h1><?php echo $value['nome_img'];</h1>
<img scr="<?php INCLUDE_PATH;?>uploads/<?php echo $value['imagem'];?>"
<?php } ?>