Piccola applicazione per News con Php, Xml e Flash: problema ordinamento.

hyperoom

Nuovo Utente
10 Dic 2010
8
0
0
Ciao a tutti.
Tramite due scripts in PHP, leggo e scrivo un file XML che verra' poi letto da un file Flash.
Desidero la visualizzazione delle news in Flash, dopo l'inserimento, in ordine decrescente per data (dalla piu' recente alla piu' vecchia), ma non azzecco la giusta istruzione.
Vi posto i files in modo da avere una visione completa dell'applicazione.
Spero tanto mi possiate aiutare.

FILE "news.xml"

<?xml version="1.0" encoding="UTF-8"?>
<news>
<articolo>
<data>10/10/2010</data>
<titolo>Da oggi nuovi progettisti al lavoro!!</titolo>
<testo>Si è conclusa con successo la prima edizione del corso "Progettare: imprese nel cantiere Europa". Un ringraziamento di cuore a tutti i partecipanti che ci hanno aiutato a rendere questa una vera occasione di apprendimento e di crescita.</testo>
</articolo>

<articolo>
<data>22/08/2010</data>
<titolo>Il VENTO soffia anche in Val D'Aosta!!</titolo>
<testo>Inizia una nuova avventura: InVENTO aiuterà l'assessorato Agricoltura e Risorse naturali della Valle D'aosta nella gestione del sistema dei controlli del Programma di sviluppo rurale 2007-2013.</testo>
</articolo></news>

FILE "amministraNews.php"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Gestione News</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="MyStyle.css" rel="stylesheet" type="text/css">
</head>
<body>

<h2>Amministra news</h2>

<?php
$myFile = "news.xml";
$myEntity = "articolo";
$myFields = array("data", "titolo", "testo");
include_once("amministraXML.php");
XMLInitialize($myFile, $myEntity, $myFields);
XMLStart();
?>
</html>

FILE "amministraXML.php"

<link href="MyStyle.css" rel="stylesheet" type="text/css" />
<?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("articolo");
foreach ($fields as $field) {
$news->addChild($field, utf8_encode($_POST[$field]));
$field = stripslashes($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];
$field = stripslashes($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) {
$field = stripslashes($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) {
$field = stripslashes($field);
switch($field) {

case 'data':
print " <td valign=\"top\">"."<input type=\"text\" name=\"" . $field . "\" value=\"" . $records->{$entity}[$i]->$field . "\" /></td>\n";
break;

case 'titolo':
print " <td valign=\"top\">"."<input type=\"text\" name=\"" . $field . "\" value=\"" . $records->{$entity}[$i]->$field . "\" /></td>\n";
break;

case 'testo':
print " <td><textarea name=\"" . $field . "\" cols=\"40\" rows=\"6\">" . $records->{$entity}[$i]->$field . "</textarea>\n";
break;
}
}
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) {
$field = stripslashes($field);
switch($field) {

case 'data':
print " <td valign=\"top\">"."<input type=\"text\" name=\"" . $field . "\" value=\"\" /></td>\n";
break;

case 'titolo':
print " <td valign=\"top\">"."<input type=\"text\" name=\"" . $field . "\" value=\"\" /></td>\n";
break;

case 'testo':
print " <td><textarea name=\"" . $field . "\" cols=\"40\" rows=\"6\">" . "</textarea>\n";
break;
}
}

print <<<EOL
<td>
<input type="submit" name="insert" value="Nuovo" />
</td>
</form>
</tr>
</table>

EOL;
}
?>

Codice AS nel file Flash

// --
System.useCodepage=true;
import TextField.StyleSheet;
news.autoSize = true;
news.html = true;
news.htmlText = "Sto caricando..";
//creo e inizializzo css
var myCSS:StyleSheet = new StyleSheet();
myCSS.setStyle("em", {fontWeight:'bold',color:'#000000'});
myCSS.setStyle("h6", {fontWeight:'normal',color:'#000000'});
news.styleSheet = myCSS;
news.htmlText = this.news;
// --
var nNews:XML = new XML();
nNews.ignoreWhite = true;
nNews.load('news.xml');
nNews.onLoad = function() {
news.htmlText = "";
qtd = this.childNodes[0].childNodes.length;
for (i=0; i<qtd; i++) {
var nData = (this.childNodes[0].childNodes.childNodes[0]);
var nTitolo = (this.childNodes[0].childNodes.childNodes[1]);
var nTesto = (this.childNodes[0].childNodes.childNodes[2]);
news.htmlText += +nData+"</h6>"+"<br />"+"<em>"+nTitolo+"</em>"+nTesto+"<br/><br/>";
}
};

Ringrazio anticipatamente.
 
Ultima modifica:

Discussioni simili