I am working on a seo “plugin” for my app, i manage to create the arrays needed… but when i try to do a foreach loop, it outputs nothing… and i get no errors, it might be a foreach loop inside a foreach loop ?
My PHP code:
include 'seo.class.php';
$SEO = new SEO();
$SEO->Set("Image", "img.png");
$SEO->Set("Description", "My description");
$SEO->Set("PageTitle", "Page Title");
$SEO->Set("Author", "Author");
$SEO->build();
?>
<html>
<head>
<?php echo $SEO->render($SEO->Array); ?>
</head>
</html>
My php class:
class SEO{
public $SiteName = "Website Name";
public $Author;
public $BaseUrl;
public $PageUrl;
public $PageTitle;
public $Description;
public $Image;
public $Array;
function Set($What, $Value){
$this->$What = $Value;
}
function build(){
$SEO = array();
//For twitter:
$SEO['Twitter'] = array(
'type' => 'name',
'content' => array(
'twitter:description' => $this->Description,
'twitter:title' => $this->PageTitle,
'twitter:image' => $this->Image,
'twitter:creator' => $this->Author )
);
//For facebok:
$SEO['Facebook'] = array(
'type' => 'property',
'content' => array(
'og_title' => $this->PageTitle,
'og:description' => $this->Description,
'og:type' => "article",
'og:url' => $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],
'og:image' => $this->Image,
'og:site_name' => $this->SiteName )
);
$this->Array = $SEO;
}
function render($Array){
foreach($Array as $Location => $Meta){
foreach($Meta[1] as $Property => $Value){
$R .= "<meta ".$Type.'="'.$Property.'" content="'.$Value.'"/>';
}
}
return $R;
}
}
Array output on print_r($Array);
:
Array
(
[Twitter] => Array
(
[type] => name
[content] => Array
(
[twitter:description] => My description
[twitter:title] => Page Title
[twitter:image] => img.png
[twitter:creator] => Author
)
)
[Facebook] => Array
(
[type] => property
[content] => Array
(
[og_title] => Page Title
[og:description] => My description
[og:type] => article
[og:url] => localhost/seotest.php
[og:image] => img.png
[og:site_name] => Website Name
)
)
)
2
Answers
Change
To
Specify
$Meta['content']
instead of$Meta[1]
.Also you may be interested in using PHP’s magic
__set()
method instead of basically reimplementing it with your Set() method. You can define supported @properties within PHPDoc.http://php.net/manual/en/language.oop5.overloading.php#object.set