Qualcuno può darmi una mano?
Non riesco a far funzionare questo codice...
L'ho trovato sul web, ma il post è di due anni fa, e non credo che ci sia ancora quell'utente...
Mi serve per creare facilmente delle news che devo riportare in una pagina web..
Queste news le scrivo tramite la pagina php, e vanno a finire in un xml...così senza che ogni volta devo modificare il codice html...
Mi sembrava una buona idea...
Lo riscrivo:
Ho creato un file news.xml
poi un file amministraNews.phpcodice:<?xml version="1.0" encoding="UTF-8" ?> - <news> - <articolo> <titolo>Titolo 4654949</titolo> <testo>Lorem 2Lorem 456464534 2Lorem 2Lorem 2Lorem 2Lorem 2</testo> </articolo> - <articolo> <titolo>zxcz\cz\xczxc</titolo> <testo>lkjh;kugyfjhghf</testo> </articolo> </news>
e un file amministraXML.phpcodice:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Gestione News</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <h2>Frasi</h2> </p> <?php $myFile = "news.xml"; $myEntity = "frase"; $myFields = array("titolo", "testo"); include_once("amministraXML.php"); XMLInitialize($myFile, $myEntity, $myFields); XMLStart(); ?> </body> </html>
ma non ne vuole sapere di funzionare...codice:<?php //definizione funzioni $filename = ""; $entity = ""; $fields = array(); function XMLLoadFile($filename) { $xmlstr = file_get_contents($filename); $xml = new SimpleXMLElement($xmlstr); return $xml; } function XMLSaveFile($filename, $xml) { $xmlstr = $xml->asXML(); return file_put_contents($filename, $xmlstr); } function XMLInitialize($p_filename = "", $p_entity = "", $p_fields = array()) { global $filename, $entity, $fields; $filename = $p_filename; $entity = $p_entity; $fields = $p_fields; } function XMLStart() { global $filename, $entity, $fields; if (!strlen($filename)) return; $records = XMLLoadFile($filename); if (isset($_POST["insert"])) { $news = $records->addChild("news"); foreach ($fields as $field) { $news->addChild($field, utf8_encode($_POST[$field])); } XMLSaveFile($filename, $records); } if (isset($_POST["delete"])) { if (isset($_POST["ID"])) { $id = (int) $_POST["ID"]; unset($records->{$entity}[$id]); XMLSaveFile($filename, $records); } } if (isset($_POST["edit"])) { if (isset($_POST["ID"])) { $id = (int) $_POST["ID"]; foreach ($fields as $field) { $records->{$entity}[$id]->$field = $_POST[$field]; } XMLSaveFile($filename, $records); } } XMLShowRecords($records); } function XMLShowRecords($records) { global $filename, $entity, $fields; print <<<EOL <table border="0" cellpadding="5" cellspacing="5" width="100%"> <tr> EOL; foreach ($fields as $field) { print " <th>" . $field . "</th>\n"; } print <<<EOL </tr> EOL; for ($i = 0; $i < count($records); $i++) { print <<<EOL <tr> <form method="POST"> EOL; foreach ($fields as $field) { print " <td><input type=\"text\" name=\"" . $field . "\" value=\"" . $records->{$entity}[$i]->$field . "\" /></td>\n"; } print <<<EOL <td> <input type="hidden" name="ID" value="{$i}" /> <input type="submit" name="edit" value="Modifica" /> <input type="submit" name="delete" value="Elimina" onclick="javascript: return window.confirm('Sei sicuro di voler cancellare il record?');" /> </td> </form> </tr> EOL; } print <<<EOL <tr> <form method="POST"> EOL; foreach ($fields as $field) { print " <td><input type=\"text\" name=\"" . $field . "\" value=\"\" /></td>\n"; } print <<<EOL <td> <input type="submit" name="insert" value="Nuovo" /> </td> </form> </tr> </table> EOL; } ?>
ho messo tutt'e 3 nella stessa cartella ovviamente, con diritti di esecuzione, lettura e scrittura.
Ma quando vado al file amministranews.php mi da questo errore:
mentre se vado su amministraxml.php mi esce pagina bianca...codice:Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 2: parser error : Start tag expected, '<' not found in D:\Inetpub\webs\in-modit\news\amministraXML.php on line 9 Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: - <news> in D:\Inetpub\webs\in-modit\news\amministraXML.php on line 9 Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in D:\Inetpub\webs\in-modit\news\amministraXML.php on line 9 Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in D:\Inetpub\webs\in-modit\news\amministraXML.php:9 Stack trace: #0 D:\Inetpub\webs\in-modit\news\amministraXML.php(9): SimpleXMLElement->__construct('<?xml version="...') #1 D:\Inetpub\webs\in-modit\news\amministraXML.php(29): XMLLoadFile('news.xml') #2 D:\Inetpub\webs\in-modit\news\amministraNews.php(22): XMLStart() #3 {main} thrown in D:\Inetpub\webs\in-modit\news\amministraXML.php on line 9
Non riesco a capire cosa devo correggere
C'è qualcuno che mi può dare una mano?

Rispondi quotando