$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))