home
clear breadcrumbs
search
login
 
xml_crud_example
{{ :xml-crud-example.zip |}} , , http://peteolson.byethost22.com/sandbox/xml-crud-example|working example , , I didnt include the css related files, they are all in the zip archive , ,
, ,
, ,
,
peter
,
notes
,
, ,
,
janice
,
notes
,
, ,
,
buddy
,
notes
,
, ,
, ,
, ,
, , ,
, , , , , ,
,
Your page title here :)
,
,
, , ,
, , ,
, , ,
,
, , ,
, , , ,
,
,
, , , ,
Home
,
,
Add User
,
, ,
,
,
,
, , , , , , , ,
,
,
,
,
, ,
, ,
Add New Record
,
,
, ,
,
Name
,
,
, ,
,
Notes
, ,
,
,
,
,
, , ,
Update:
,
,
,
, ,
attributes()->id;?>/>
,
,
,
Name
,
name;?>/>
,
, ,
,
Notes
, ,
notes;?>/>
,
,
,
,
,
,
attributes()->id;?>/> ,
,
, , ,
, ,
, _xml = simplexml_load_file(DQdata.xmlDQ); , } , , public function getXml() , { , return $this->_xml; , } , , /** , * @param mixed $path , */ , public function setPath($path) , { , $this->path = $path; , } , , /** , * @return mixed , */ , public function getPath() , { , return $this->path; , } , , public function userlist() , { , echo DQ
,
,
,
Name
,
Notes
,
,
, , DQ; , , foreach ($this->_xml as $user) { , echo DQ ,
,
,
attributes()->id . DQ>{$user->name}
,
, ,
, {$user->notes} ,
,
, DQ; , } , echo DQ
DQ; , , } , , public function filterList($post) , { , $xml = $this->_xml->xpath(username=DQ . strtolower($post) . DQ); , echo DQ
DQ; , , foreach ($xml as $user) { , , echo DQ ,
,
{$user->name}
,
{$user->notes}
,
, DQ; , } , echo DQ
DQ; , } , public function format_xml_file() , { , $xmlFile = $this->path; , if( !file_exists($xmlFile) ) die(Missing file: . $xmlFile); , else , { , $dom = new DOMDocument(1.0); , $dom->preserveWhiteSpace = false; , $dom->formatOutput = true; , $dl = @$dom->load($xmlFile); // remove error control operator //(@) to print any error message generated while loading. , if ( !$dl ) die(Error while parsing the document: . $xmlFile); , $dom->save($xmlFile); , } , } , public function adduser($post) , { , if ($postDQnameDQ != DQDQ) , { , $xml = $this->_xml; , $user = $xml->addChild(user); , $name = $user->addChild(DQnameDQ, $postDQnameDQ); , $user_notes = $user->addChild(DQnotesDQ, $postDQnotesDQ); , $user->addAttribute(DQidDQ, $this->user_id()); , $xml->asXML($this->path); , } , $this->format_xml_file(); , header(location:process.php?list); , } , public function user_id() , { , $id = 0; , $ids = $this->_xml; , foreach ($this->_xml as $ids) , { , if ($id < (int) $ids->attributes()->id) , { , $id = $ids->attributes()->id; , } , } , return $id +=1 ; , } , , public function getUserById($id) , { , $user = $this->_xml->xpath(//user@id=DQ . $id . DQ); , return $user0; , } , , public function updateUser($post) , { , $user = $this->_xml->xpath(user@id=DQ . $postid . DQ); , $user0->name = $postDQnameDQ; , $user0->notes = $postDQnotesDQ; , $this->_xml->asXML($this->path); , header(location:process.php?list); , } , public function deleteUser($post) , { , $id = $postid; , $i = 0; , foreach ($this->getXml() as $user){ , if ($user->attributes()->id == $id){ , unset($this->getXml()->user$i); , $this->getXml()->asXML($this->path); , break; , } , $i++; , } , header(location:process.php?list); , } , } , , //Include template , //include index.php; , , , , //Controller , $param = $_SERVERQUERY_STRING; , $arr = explode(DQ=DQ, $param); , if (count($arr) > 1) { , $param = $arr0; , } , $path = getcwd().DQ/data.xmlDQ; , $process = new Process(); , $process->setPath($path); , , if ($param == DQlistDQ) { , $process->userlist(); , } , if ($param == DQaddDQ) { , $post = $_POST; , $process->adduser($post); , //echo $process->id(); , //include user.php; , } , if ($param == DQfilterDQ) { , $post = $_POSTDQnameDQ; , $process->filterList($post); , } , , if ($param == DQdeleteDQ) { , $post = $_POST; , $process->deleteUser($post); , } , , if ($param == DQeditDQ) { , $id = $arr1; , $user = $process->getUserById($id); , include user.php; , } , , if ($param == DQupdateDQ) { , $post = $_POST; , $process->updateUser($post); , } , , include(footer.php); , ?> , , ,