My if is not working as expected

1
$mt = $conn->query("SELECT entry_type FROM myTable")->fetchAll();

foreach ($mt as $FB) {

if ($FB['entry_type'] == 'pagina'){ 
    echo '<meta property="og:type" content="website">';
} else{
    echo '<meta property="og:type" content="article">';
    echo '<meta property="article:author" content="">';
    echo '<meta property="article:publisher" content="">';
    echo '<meta property="article:published_time" content="">';
    echo '<meta property="article:modified_time" content="">';
}

}

The column named "entry_type" has only two possible values: "page" or "post", this code should give echo in meta tag that is in if when entry_type = pagina , and give echo in meta tags that are in else when entry_type = post .

This code is duplicating the meta tags according to how many records have the pagina or post values in the entry_type column of my DB. Regardless if the page I'm in is equal to entry_type = pagina or entry_type = post . And this code is also printing the meta tags of if and else together.

Edit: I solved the duplication problem:

$mt = $conn->query("SELECT entry_type FROM myTable WHERE entry_type IS NOT NULL GROUP BY entry_type")->fetchAll();

foreach ($mt as $FB) {

    if ($FB['entry_type'] == 'pagina'){ 
        echo '<meta property="og:type" content="website">';
    } else{
        echo '<meta property="og:type" content="article">';
        echo '<meta property="article:author" content="">';
        echo '<meta property="article:publisher" content="">';
        echo '<meta property="article:published_time" content="">';
        echo '<meta property="article:modified_time" content="">';
    }

 }

This code is printing the meta tags of if and else together. Ignoring the if statement.

What's wrong with my code, and how do I solve it?

This is the result of print_r :

$mt = $conn->query("SELECT entry_type FROM myTable WHERE entry_type IS NOT NULL GROUP BY entry_type")->fetchAll();

foreach ($mt as $FB) {
print_r($mt); //Resultado 1

    if ($FB['entry_type'] == 'pagina'){
    print_r($mt); //Resultado 2

        //codigo postado na pergunta

    } else{    
      print_r($mt); //Resultado 3

        //codigo postado na pergunta

    }

} 
  • Result 1 :
  

Array ([0] => Array ([entry_type] = > page [0] = > page) [1] = >   Array ([entry_type] => post [0] => post)) Array ([0] = & Array   [entry_type] = > page [0] = > page) [1] = > Array ([entry_type] = >   post [0] = > post))

  • Result 2 :
  

Array ([0] => Array ([entry_type] = > page [0] = > page) [1] = >   Array ([entry_type] = > post [0] = > post))

  • Result 3 :
  

Array ([0] => Array ([entry_type] = > page [0] = > page) [1] = >   Array ([entry_type] = > post [0] = > post))

    
asked by anonymous 25.09.2018 / 23:21

2 answers

0

By its var_dump it seems that the first index of its $mt vector has the value null in the entry_type column. In other words, as null is not página it will fall into else .

For security I advise you to put a else if on the spot. Here's an example of how the code would look:

$mt = $conn->query("SELECT entry_type FROM myTable")->fetchAll();

foreach ($mt as $FB) {
    $type = '';
    if ($FB['entry_type'] == 'pagina'){ 
        $type = 'website';
    } else if ($FB['entry_type'] == 'post') {
        $type = 'article';
    }

    if($type == 'website' || $type == 'article') {
      echo '<meta property="og:type" content="'.$type.'">';
      echo '<meta property="article:author" content="">';
      echo '<meta property="article:publisher" content="">';
      echo '<meta property="article:published_time" content="">';
      echo '<meta property="article:modified_time" content="">';
      break;
    }
 }
    
25.09.2018 / 23:52
0

In order to do this, you have to pass the associative array key to use as a value inside the array, since the structure that closes it is as follows:

Array / Line / Column, for example: Result (0, 'entry_type') with page, Result (1, 'entry_type') with post, Result (2, 'entry_type') with article, and so on... With this, syntax in the foreach after the 'as' what you want to use in the array, as below:

$mt = $conn->query("
    SELECT
       entry_type
    FROM myTable
    WHERE
       entry_type = 'pagina'
    GROUP BY
       entry_type");

$res = $mt->fetchAll(PDO::FETCH_ASSOC);

print("<pre>");
print_r($res);
print("</pre>");

foreach ($res as $FB) {
    if ($FB["entry_type"] == "pagina") {
        echo '<meta property="og:type" content="website">';
    } else {
        echo '<meta property="og:type" content="article">';
        echo '<meta property="article:author" content="">';
        echo '<meta property="article:publisher" content="">';
        echo '<meta property="article:published_time" content="">';
        echo '<meta property="article:modified_time" content="">';
    }
}
    
26.09.2018 / 04:08