Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QUESTION Foreach how?
#1
hello, I have a problem with using foreach function.

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($xmlsimpleDATATEST);
        }
    }    



tnx,
Reply
#2
does your input data change also
"$atts= $data->children();" instead of attributes , is that correct for the input data ?

It looks ok at a glance, hmm

also you are modifying your foreach value variable $data
$data = $xml->addChild('data');
use another varname like $node


PHP Code:
foreach ($testfile->data as $data
    {
                    
        
$atts$data->children();
        
$node$xml->addChild('data');
        
$node->addChild('name'$atts['name']);
                                    
    } 
NEW: SA Admin Toolbar Plugin | View All My Plugins
- Shawn A aka Tablatronix
Reply
#3
(2016-11-24, 01:01:02)shawn_a Wrote: does your input data change also
"$atts= $data->children();" instead of attributes , is that correct for the input data ?

It looks ok at a glance, hmm

also you are modifying your foreach value variable $data
$data = $xml->addChild('data');
use another varname like $node




PHP Code:
foreach ($testfile->data as $data
 
   {
 
                   
        $atts
$data->children();
 
       $node$xml->addChild('data');
 
       $node->addChild('name'$atts['name']);
 
                                   
    


Thank shawn_a for your reply.

After your suggestion looks like this:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<test><data><name/></data><data><name>6</name></data></test>

previous entries are empty only displays the last!?
Reply
#4
I found a solution to store data.
PHP Code:
foreach ($testfile->item as $prox
    {          
        $node 
$xml->addChild('item');
        $node->addChild('name'$prox->name);
        $node->addChild('something'$prox->something);
                                 
    

        $node $xml->addChild('item');
        $node->addChild('name'$_POST['name']);
        $node->addChild('something'$_POST['something']);

XMLsave($xmlsimpleGSECCONTENT); 


Code:
<?xml version="1.0" encoding="UTF-8"?>
<monthly>
    <item>
        <name>First name<name/>
        <somenthing>Bla bla<somenthing/>
    </item>
    
    <item>
        <name>Second name</name>
        <startdate>Bla bla 2</startdate>
    </item>
</monthly>  

Now I have a problem with editing existing data.

Can someone help?
Reply




Users browsing this thread: 1 Guest(s)