2016-11-24, 00:50:10
hello, I have a problem with using foreach function.
the following code works ok when I use addAttribute name= ...
My question is how to get results using foreach when I us addChild -> name? I need this:
I've tried various combinations, but always returns an empty result.
plugin -> test.php
tnx,
the following code works ok when I use addAttribute name= ...
PHP Code:
foreach ($testfile->data as $data)
{
$atts= $data->attributes();
$data = $xml->addChild('data');
$data->addAttribute('name', $atts['name']);
}
$data = $xml->addChild('data');
$data->addAttribute('name', $_POST['name']);
Code:
<?xml version="1.0" encoding="UTF-8"?>
<test>
<data name="1"/>
<data name="2"/>
<data name="3"/>
</test>
My question is how to get results using foreach when I us addChild -> name? I need this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<test>
<data>
<name>1</name>
...
</data>
<data>
<name>2</name>
...
</data>
<data>
<name>3</name>
...
</data>
</test>
I've tried various combinations, but always returns an empty result.
plugin -> test.php
PHP Code:
$thisfile=basename(__FILE__, ".php");
# register plugin
register_plugin(
$thisfile, //Plugin id
'Name of plugin', //Plugin name
'1.0', //Plugin version
'', //Plugin author
'', //author website
'Description', //Plugin description
'pages', //page type - on which admin tab to display
'test_content' // administration
);
add_action('nav-tab','createSideMenu',array($thisfile,'TEST'));
define('simpleDATATEST', GSDATAOTHERPATH . 'test_page.xml');
function test_content() {
if(isset($_GET['test-add']))
{
add();
}
else
{
echo '<h3 class="clearfix">SOME TITLE</h3>';
echo '<p><a href="load.php?id=test&test-add">Add Data</a></p>';
}
}
# end function
function add(){
echo '<p><a href="load.php?id=test">Back</a></p>';
if(file_exists(simpleDATATEST))
{
$testfile = getXML(simpleDATATEST);
}
$xml = new SimpleXMLExtended('<?xml version="1.0" encoding="UTF-8"?><test></test>');
if(isset($_GET['test-add']))
{
?>
<div class="add-new">
<p><a href="load.php?id=test&test-add">Add New</a></p>
<h3 class="clearfix">Some Title</h3>
<form method="post" class="add" action="load.php?id=test&test-add">
<label for="name">Name</label>
<p><input class="text short" name="name" type="text" value="<?php if(isset($_POST['name'])){ echo $_POST['name'];}?>" /></p>
<p><input type="submit" class="submit" name="save" value="Save" /></p>
</form>
</div>
<?php
if(isset($_POST['save']))
{
foreach ($testfile->data as $data)
{
$atts= $data->attributes();
$data = $xml->addChild('data');
$data->addAttribute('name', $atts['name']);
}
$data = $xml->addChild('data');
$data->addAttribute('name', $_POST['name']);
/* EXAMPLE -> NOT WORKING
foreach ($testfile->data as $data)
{
$atts= $data->children();
$data = $xml->addChild('data');
$data->addChild('name', $atts['name']);
}
$data = $xml->addChild('data');
$data->addChild('name', $_POST['name']);
*/
XMLsave($xml, simpleDATATEST);
}
}
}
tnx,