-2

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?

mario
  • 367
  • 1
  • 4
  • 17
  • 5
    That will become unreadable very soon. Use a `switch..case` instead – Federico klez Culloca Nov 14 '18 at 16:58
  • 2
    Better to use an `if..else` or a `switch` statement instead, it's very bad practice to chain multiple ternaries together. (Even 2 ternaries chained is bad practice, and hard to read) – GrumpyCrouton Nov 14 '18 at 17:05
  • 2
    Per the manual, http://php.net/manual/en/language.operators.comparison.php, `It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious` See example 4. – user3783243 Nov 14 '18 at 17:09
  • 3
    @515948453225 I didn't say, or even _imply_ that using a ternary is bad. I personally love to use them. However, it is bad practice to use _multiple_ ternaries on the same line. – GrumpyCrouton Nov 14 '18 at 17:10
  • 2
    If you're not very skilled with it, why are you ignoring the advice of other people here who are telling you that what you're trying to do is a bad idea? – miken32 Nov 14 '18 at 17:26

1 Answers1

1

I would agree with Frederico, if it is not readable when you review in the future you wont know whats happening. I'd also suggest switch case but why not remove ternary and use

if ( $Q ) {
    $entry='entry1'; 
} elseif ( $Q2 ) {
    $entry='entry2';
} else {
    $entry='entry3';
}

then use ':entry_type' => $entry

indeed 'entry2' would always return true (slaps back of head)

ThurstonLevi
  • 664
  • 13
  • 34