Hello, I would like to do a search system with friendly Urls, I researched a lot and tried many things, not yet I could not.
I would like the url to be of this form site.com/home/categoria/pesquisa
.
home.php
<form method="POST" class="floatLeft formSearch" action="home/go">
<div class="formSearchDiv">
<input type="text" name="searchInput" class="searchInput floatLeft">
<select name="categorySearch" maxlength="255" class="categorySearch floatLeft">
<option value="Todos" selected="selected">Todos</option>
<option value="Serie">Serie</option>
<option value="Filme">Filme</option>
<option value="Anime">Anime</option>
<option value="Filme Adulto">Filme Adulto</option>
<option value="Desenho">Desenho</option>
<option value="Software">Software</option>
<option value="Jogos">Jogos</option>
</select>
<input type="image" name="submitSearch" src="<?=DIR_IMAGES ?>lupa.png" class="submitSearch floatLeft">
</div>
</form>
require DIR_FUNCS.'/funcSQL.php';
$sql = new SQL();
if(isset($_POST['submitSearch']))
{
if(isset($_GET['go']))
{
if(preg_match("^/[A-Za-z]+/", $_POST['searchInput']))
{
$search = $_POST['searchInput'];
echo $search;
$sql->listTorrentSearch($search, "Todos");
}
}
}
FuncSQL.php
public function listTorrentSearch($search, $category)
{
$conn = $this->openSQL();
$cont = 0;
if($category == "Todos")
{
$result = mysqli_query($conn, "SELECT * FROM torrents WHERE Nome LIKE '".$search."%'");
}
else
{
$result = mysqli_query($conn, "SELECT * FROM torrents WHERE Nome LIKE '".$search."%' AND Categoria='".$category."'");
}
while($row = mysqli_fetch_array($result))
{
if($cont < 25)
{
$name = $row['Nome'];
$category = $row['Categoria'];
$size = $row['Size'];
$magnet = $row['Magnet'];
$createTorrentClass = new CreateTorrent();
$createTorrentClass->createTorrentDiv($name, $category, $size, $magnet);
$cont++;
}
}
mysqli_close($conn);
}
.htacess
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
treatUrl.php
$pUrl = strip_tags(trim(filter_input(INPUT_GET, 'url', FILTER_DEFAULT)));
$sUrl = (empty($pUrl) ? "index" : $pUrl);
$url = array_filter(explode('/', $sUrl));
if (count($url) > 1) {
$cont = 1;
foreach ($url as $arg) {
define("PARAM" . $cont, $arg);
$cont++;
}
} else if (count($url) == 1) {
if (file_exists(DIR_PAGES . $url[0] . '.php')) {
$pag = DIR_PAGES . $url[0] . '.php';
} else {
if($url[0] != 'index')
{
$pag = DIR_PAGES . '404.php';
}
else
{
$pag = DIR_PAGES . 'home.php';
}
}
} else {
$pag = DIR_PAGES . '404.php';
}