I have two query strings: ?page and ?p. I'm using a slug as a link on these query strings, example: ?p/?page=slug-link . And these links is accessible only if it have the entry_type
especified on the ternary operator below. I was working with only two types of entry_type
and it was working perfectly, but now i need to add +1 type of entry_type
.
I'm trying to do it with the following code:
<?php
$Q = !empty($_GET['post']);
$slug = 'home';
if ($Q) {
$slug = $_GET['post'];
} elseif (!empty($_GET['p'])) {
$slug = $_GET['p'];
}
try {
$stmt = $conn->prepare('SELECT `id`, `title`, `description`, `img`, `alt`, `keywords`, `art`, `slug_url`, `slug_link`, `entry_type` FROM table_tudo WHERE `slug_link` = :slug_link AND `entry_type` = :entry_type');
$stmt->execute([
':entry_type' => ($Q) ? ('entry1') : (('entry2') ? ('entry2') : ('entry3')),
':slug_link' => $slug
]);
if (!$NF = $stmt->fetch(\PDO::FETCH_ASSOC)) {
throw new \InvalidArgumentException('Title ' . htmlentities($title, \ENT_QUOTES, 'UTF-8', false) . ' not found in database');
}
$id = $NF['id'];
$title = $shareTitle = $NF['title'];
$description = $shareDescription = $NF['description'];
$shareImg = $NF['img'];
$alt = $NF['alt'];
$keywords = $NF['keywords'];
$art = $NF['art'];
$ogUrl = $urlCanonical = $NF['slug_url'];
$slug = $NF['slug_link'];
$entry_type = $NF['entry_type'];
} catch (\InvalidArgumentException $e) {
header('Location: index.php?p=home');
exit;
} catch (\Exception $e) {
header('Location: error.php?e=Oops an error occurred');
throw $e;
}
function sanitize($data, $filter = \FILTER_SANITIZE_STRING) {
if ($data = filter_var(trim($data), $filter)) {
$data = preg_replace('/http(s)?:\/\//', '', $data);
}
return $data;
}
$loadPage = null;
if ($sanitizedName = sanitize($Q ? $title : $slug)) {
$loadPageSuffix = ($Q ? '/myDirectory/' : '/page_');
$loadPage = __DIR__ . $loadPageSuffix . $name . '.php';
}
if (null === $loadPage || !is_file($loadPage)) {
header('HTTP/1.1 404 Not Found');
exit;
}
index.php:
<?php
require_once 'load.php';
?>
...
...
<body>
<?php require_once $loadPage; ?>
...
...
I also use this code to have some global variables.
Old version of ':entry_type' => ($Q) ? ('entry1') : (('entry2') ? ('entry2') : ('entry3')),
was: ':entry_type' => $Q ? 'entry1' : 'entry2',
the old version was working. And I'm having a hard time trying to change this ternary operator.
Before, i had two entry_types
: entry1
and entry2
. But i need to add a entry3
to the query string ?post
. And the entry3
will be accessible on the directory /myDirectory/
.
Could someone help me?