我为我的网站创建了一个非常简单的CMS
面板(我仍在学习)。
我有一些问题。我已在Flash-AS3
中创建了面板,该面板会加载php
文件,该文件会打开并保存xml data
。以下是代码:
AS3代码:
function onClicked(e:MouseEvent):void
{
var myXmlString:String = "<?xml version=\"1.0\" encoding=\"utf-8\"?><NEWSCONTENT><NEWS ID=\"" + idTxt.text + "\" IMG=\"" + sourceTxt.text +"\" TITLE=\"" + titleTxt.text + "\" DATE=\"" + dateTxt.text + "\" CONTENT=\"" + contentTxt.text + "\"/></NEWSCONTENT>";
trace(myXmlString);
var myXml:XML = new XML(myXmlString);
var req:URLRequest = new URLRequest("phpXML.php");
req.data = myXml;
req.contentType = "text/xml";
req.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
function onPHPLoaded(e:Event):void
{
statusTxt.text = "Dane zostaly zaktualizowane!";
}
loader.load(req);
loader.addEventListener(Event.COMPLETE, onPHPLoaded);
}
send_btn.addEventListener(MouseEvent.CLICK, onClicked);
PHP代码:
<?php
if (isset($GLOBALS["HTTP_RAW_POST_DATA"])) {
$xml = $GLOBALS["HTTP_RAW_POST_DATA"];
$file = fopen("www.xml", "w");
fwrite($file, $xml);
fclose($file);
echo ($GLOBALS["HTTP_RAW_POST_DATA"]);
}
?>
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<NEWSCONTENT>
<NEWS ID="1" IMG="galerry/1b.jpg" TITLE="Let's begin!" DATE="23-11-2014" CONTENT="This is the content"/>
</NEWSCONTENT>
没有errors
,一切正常,但它不按我想要的方式工作。
现在,当我将data/"strings"
添加到text fields
时,它会移除所有xml data
并使用我写的文字再次保存。但我想在"<NEWS ID="1" IMG="galerry/1b.jpg" TITLE="Let's begin!" DATE="23-11-2014" CONTENT="This is the content"/>"
文件中添加下一行xml
。
我尝试将$file = fopen("www.xml", "w");
更改为$file = fopen("www.xml", "a");
,但这又将整个xml
代码添加到第一个。
有人可以帮忙吗?
答案 0 :(得分:0)
如果我理解你的问题,你有一个xml文件来保存新闻,每次你想要添加一个新的。如果是,您不需要每次都发送所有xml内容,您只需发送新的新闻属性并将其附加到现有文件即可。为此,请使用此代码:
PHP代码:
<?php
if(count($_POST) == 5){
$xml_path = 'news.xml';
if(!file_exists($xml_path)){
// if our xml file didn't exist, create it
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->preserveWhiteSpace = FALSE;
$root = $xml->createElement('NEWSCONTENT');
$xml->appendChild($root);
} else {
// if our xml file exists, load it
$xml = new DOMDocument();
$xml->preserveWhiteSpace = FALSE;
$xml->load($xml_path);
// get our root element : NEWSCONTENT
$root = $xml->documentElement;
}
// create our news element
$news = $xml->createElement('NEWS');
// add atributes : ID, IMG, TITLE, DATE and CONTENT
$attr_id = $xml->createAttribute('ID');
$attr_id->value = $_POST['id'];
$attr_img = $xml->createAttribute('IMG');
$attr_img->value = $_POST['img'];
$attr_title = $xml->createAttribute('TITLE');
$attr_title->value = $_POST['title'];
$attr_date = $xml->createAttribute('DATE');
$attr_date->value = $_POST['date'];
$attr_content = $xml->createAttribute('CONTENT');
$attr_content->value = $_POST['content'];
$news->appendChild($attr_id);
$news->appendChild($attr_img);
$news->appendChild($attr_title);
$news->appendChild($attr_date);
$news->appendChild($attr_content);
$root->appendChild($news);
$xml->formatOutput = true;
// save our new xml file
$xml->save($xml_path);
}
?>
ActionScript代码:
var btn:Btn = new Btn();
btn.addEventListener(MouseEvent.CLICK, onClicked);
addChild(btn);
function onClicked(e:MouseEvent):void {
var url:String = 'phpXML.php';
var variables:URLVariables = new URLVariables();
variables.id = idTxt.text;
variables.img = sourceTxt.text;
variables.title = titleTxt.text;
variables.date = dateTxt.text;
variables.content = contentTxt.text;
var request:URLRequest = new URLRequest(url);
request.data = variables;
request.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onPHPLoaded);
loader.load(request);
function onPHPLoaded(e:Event):void {
statusTxt.text = "Dane zostaly zaktualizowane!"
}
}
当然,这个极简主义的工作代码可以完成这项工作,它每次发送新闻并将其保存在news.xml
中,但您应该添加控件......