I currently have one question and that is if I can pass 2 parameters in my url to identify a page.
Currently I have this code, which gets me through the parameter “url_slug” my post stored in the database.
<?php include($_SERVER['DOCUMENT_ROOT'].'/app/controllers/posts.php');
function selectPostBySlug($slug)
{
global $conn;
$sql = "SELECT * FROM animales WHERE url_slug like ? LIMIT 1";
$stmt = executeQuery($sql, [$slug]);
$records = $stmt->get_result()->fetch_assoc();
return $records;
}
if (isset($_GET['url_slug'])){
$url_slug= preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($_GET["url_slug"])));
$episode = selectPostBySlug($url_slug);
}
?>
So far everything works fine for me, but I would like to add that when I get the “url_slug” parameter I also get an extra parameter to identify the post more accurately, for example in the database.
Currently the urls work this way => localhost/animales?url_slug=oso
—–
but since I added the new “tipo” column I would like the url to identify the posts this way => localhost/animales?url_slug=oso&tipo=panda
| and send me to the post panda
and when I enter to = localhost/animales?url_slug=oso&tipo=polar
| send me to the post polar
EDIT:
function executeQuery($sql, $data)
{
global $conn;
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $conn->prepare($sql);
$values = array_values($data);
$types = str_repeat('s', count($values));
$stmt->bind_param($types, $values);
$stmt->execute();
return $stmt;
}
Advertisement
Answer
You can add tipo
parameter to your selectPostBySlug
and do the same thing fot the tipo
query
// 3. Added: Parameter to handle Tipo Filter
// vvvvvvv
function selectPostBySlug($slug, $tipo)
{
global $conn;
// 4. Added: Additional Condition to Handle `tipo` filter
// vvvvvvvvvvvvvvv
$sql = "SELECT * FROM animales WHERE url_slug like ? AND tipo like ? LIMIT 1";
$stmt = executeQuery($sql, [$slug, $tipo]);
// ^^^^^^^
// 5. Added: Assign `tipo` input from function parameter
$records = $stmt->get_result()->fetch_assoc();
return $records;
}
// 0. Added: Condition to check if `tipo` query exists
// vvvvvvvvvvvvvvvvvvvvvvv
if (isset($_GET['url_slug']) && isset($_GET['tipo'])){
$url_slug= preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($_GET["url_slug"])));
// 1. Added: Tipo Input Filter
$tipo= preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($_GET["tipo"])));
$episode = selectPostBySlug($url_slug, $tipo);
// ^^^^^^^^
// 2. Added: for Tipo Filter
}