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
– bfavaretto
Great. I was wondering if I could use the chat feature for that purpose.
– Marcos Xavier