My if is not working as expected

Asked

Viewed 159 times

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 called "entry_type" has only two possible values: "page" or "post", this code should give echo in meta tag that is on if when the entry_type = pagina, and give echo in meta tags that are on else when the entry_type = post.

This code is duplicating the meta tags according to how many records have the values pagina or post in the column entry_type of my DB. Regardless of whether the page I’m on equals entry_type = pagina or entry_type = post. And this code is also printing the meta tags from if and of 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 from if and of else together. Ignoring the instruction of if.

What’s wrong with my code, and how do I fix 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 ) )

  • Score 2 :

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

  • Score 3 :

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

  • Please avoid long discussions in the comments; your talk was moved to the chat

  • Great. I was wondering if I could use the chat feature for that purpose.

2 answers

0

For his var_dump apparently the first index of its vector $mt has the value null in the column entry_type. In other words, how null is not página he will fall into your else.

For safety I advise you to put one else if in place. Here is 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;
    }
 }
  • I’m sorry I didn’t understand which one?

  • Aaah yes, now I get it. so, it’s right according to the var_dump he entered the correct location. how are you testing these tags?

  • Using Chrome type the view-source:http://localhost/seuscript.php address and see if you’re not actually printing

  • he is printing more than once the meta tag

  • I edited my answer and added a break, see if it gets to the result you expect.

  • After I put the break is only printing the meta tag that is on if, and that meta tag is printing on all pages, at least this time did not duplicate the meta tag. I updated my question, please take a look at Edit.

  • Now I understand what you want, I looked at the tags just now. Sorry, I edited the answer.

  • It’s because of the nulls ready, edited...

  • Still the same, you’re ignoring the instruction of else if, and the outcome of prin_r continues the way I put it on Edit. What is the reason for NULL? I checked my logs, and they have the entry_type correct.

Show 4 more comments

0

For you to do this there that you want, you have to pass which key of the associative array will use as value within the traversed array, because the structure that closeAll fera is the following:

Array/Row/Column, for example: Result(0,'entry_type') with page, Result(1,'entry_type') with post, Result(2,'entry_type') with article, and so on and so forth... With this, sinlize 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="">';
    }
}
  • passes the PDO::FETCH_ASSOC inside the fechAll that will no longer appear duplicated and makes the WHERE and GROUP as you said...

  • I edited my answer up there, and for me here at home, it seems to have worked out with the modification. he no longer ignored the if...

  • Continue only printing the meta tags from else, I put some print_r the result ta no link: https://pastebin.com/ZuRspCRD Even if not working as I wanted, by print_r you can see that the entry_type is equal to pagina, but he still insists on printing the else, only printed the if if I change the if ($FB["entry_type"] == 'pagina') for if ($FB["entry_type"] != 'pagina'). I tried to put another if along with the else else if($FB["entry_type"] == 'post') not yet certain.

  • Placing the DFO>>FETCH_ASSO, you can take the chado from the foreach value, thus: foreach ($res as $FB). The key I would only leave if I didn’t pass the kind of fetch I’m doing...

  • DFO>>FETCH_ASSO? Did you mean PDO::FETCH_ASSOC? Please explain your comment better, I really did not understand.

  • That’s right, foiy PDO::FETCH_ASSOC, sorry man, I typed too much in a hurry and I didn’t even review it, it was really bad!

  • Relax! The point is that I still have the same problem, I just solved the problem with duplications. I don’t know if you saw it, but I added new information to my question. I’m suspecting my code is wrong about what I want to do. But I don’t know any other way to do it, other than the condition if and else. What do you think? Some dirt?

  • Actually I would use switch case, but if also serves. you can guarantee with certainty that what comes from the database is exactly the same as the possibility of if, including uppercase, buttocks, accents...

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.