Skip to content

Instantly share code, notes, and snippets.

@rakotomandimby
Created October 29, 2012 12:07
Show Gist options
  • Save rakotomandimby/3973202 to your computer and use it in GitHub Desktop.
Save rakotomandimby/3973202 to your computer and use it in GitHub Desktop.
<?php
class PersonModel
{
public function __construct($id)
{
$xml = new XMLReader();
$tmp = $xml->open('encheres.xml');
while($xml->read())
{
if(('person' == $xml->name) && ($xml->nodeType == XMLReader::ELEMENT) && ($id == $xml->getAttribute('id')))
{
// converti person en DOM
$node = $xml->expand();
$a_person = new DomDocument();
$this->person = $a_person->importNode($node,true);
$this->id = $id;
$this->name = $this->person->getElementsByTagName('name');
$this->name = $this->name->item(0)->nodeValue;
$this->email = $this->person->getElementsByTagName('emailaddress');
$this->email = $this->email->item(0)->nodeValue;
$this->data_for_view=array('id' => $this->id , 'name' => $this->name, 'email' => $this->email);
break;
}
}
}
public static function getPersonIds()
{
$xml = new XMLReader();
$tmp = $xml->open('encheres.xml');
while($xml->read())
{
if(('person' == $xml->name) && ($xml->nodeType == XMLReader::ELEMENT))
{
$result[]=$xml->getAttribute('id');
}
}
return $result;
}
}
<?php
class PersonView
{
public function renderPersonIdList($person_id_array)
{
$result = '<html><head></head><body>';
$result .= '<ul>';
foreach($person_id_array as $un_id)
{
$result .= '<li><a href="?person='.$un_id.'">'.$un_id.'</a></li>';
}
$result .= '</ul>';
$result .= '</body></html>';
return $result;
}
public function renderPersonDetail($person_detail_array)
{
$result = '<html><head></head><body>';
$result .= '<ul>';
foreach($person_detail_array as $attribut => $valeur)
{
$result .= '<li>'.$attribut.' : '.$valeur.'</li>';
}
$result .= '</ul>';
$result .= '</body></html>';
return $result;
}
}
<?php
require_once('Model/PersonModel.php');
require_once('View/PersonView.php');
if($_REQUEST['q'] == 'liste-personnes')
{
$person_id_array=PersonModel::getPersonIds();
$view = new PersonView();
$output = $view->renderPersonIdList($person_id_array);
print $output;
}
elseif (isset($_REQUEST['person']))
{
$la_personne = new PersonModel($_REQUEST['person']);
$view = new PersonView();
$output = $view->renderPersonDetail($la_personne->data_for_view);
print $output;
}
else
{print 'mauvais paramètres';}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment