|
-
Mar 14th, 2007, 11:57 AM
#1
Thread Starter
Lively Member
copy xml data using php
Hello,
I am trying to copy data from 2 .xml files kai store them to a third .xml file usinh php. I want the data to be stored only once. I am using the follwing code
Code:
<?php
/* _________________________________________________________________________ */
include( 'includes/application_top.php' );
include( 'protection.php' );
$dc='http://purl.org/dc/elements/1.1/';
$dct='http://purl.org/dc/terms/';
// --------------------------------------------------
$cid = $_SESSION['lesson'];
$mtd=array();
$mtd=get_infos( $FILE_LIB_XML, $cid );
// set the page title
$PAGE_TITLE = 'FILE: '.$mtd["dc:title"][0];
// save the title
$_SESSION['title'] = $mtd['dc:title'][0];
$bShowExtraData = TRUE;
$existing_courses = get_infos($FILE_ARCHIVE_XML, $cid);
if ( !in_array( $_POST['cid'], $existing_courses )){
copy_course($FILE_LIB_XML, $FILE_ARCHIVE_XML, $FILE_USERS_XML, $cid );
}
// set the left column template
$content_column = 'usermenu.tpl.php';
include( $DIR_TEMPLATES.'main.tpl.php' );
?>
The get_infos function code is the follwing
Code:
function get_infos($xmlFile, $cid)
{
$mtd=array();
$visited=array();
$child= get_lesson($xmlFile, $cid);
if(!$child) die("Cannot find lesson ID==".$cid);
//get elements of course
$cnt = $child->childNodes;
foreach( $cnt as $cn ) {
if($cn->nodeType== XML_ELEMENT_NODE) {
if( !in_array( $cn->tagName, $visited ) ) {
$visited[] = $cn->tagName;
list($prefix, $tagname) = explode( ':', $cn->tagName );
$datas = $child->getElementsByTagName($tagname);
$mtd[$cn->tagName] = array();
foreach( $datas as $data ) {
$mtd[$cn->tagName][] = $data->textContent;
}
}
}
}
return $mtd;
}
The problem is that when i am trying to update the archive.xml file if the data don't exist i get the follwing warning
Code:
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\thalis\includes\functions.php on line 95
thought the file is being updated.
Anyone knows what's wrong?
I am using php 5
-
Mar 14th, 2007, 06:40 PM
#2
Re: copy xml data using php
what line is 95 in "functions.php"? is that the foreach() call in your function?
the only thing I can think of is that you're trying to call foreach() on a variable that isn't an array. so, you could check if $cnt is an array before doing all that stuff using is_array(), and just return false (or return nothing at all) if it doesn't come up as an array.
then, when you're calling your function, to check whether or not data exists you can do something like:
PHP Code:
$blah = get_infos($some_xml_file, $cid);
if(!$blah)
echo 'there was no data in that xml file';
else{
//do stuff with $blah because it returned correct information
}
that might work for you.
-
Mar 15th, 2007, 06:05 AM
#3
Thread Starter
Lively Member
Re: copy xml data using php
I've used your code and i get the echo and the same warning again.
Yes the line 95 is the foreach() call.
What i am trying to do is to compare two .xml files and if the first contains extra data i want them to be copied to the second .xml file. If data already exists then i don't want them to be copied again.
Thanks!
-
Mar 15th, 2007, 10:55 AM
#4
Re: copy xml data using php
show me how you implemented it. if you did it right, the foreach function should never be called unless $cnt is an array, so there shouldn't be any way for you to get that warning. if you're certain you did it right, then use print_r() to display your array and look at its contents to see what might be happening.
Last edited by kows; Mar 15th, 2007 at 11:00 AM.
-
Mar 15th, 2007, 12:59 PM
#5
Thread Starter
Lively Member
Re: copy xml data using php
That's the code i am using
Code:
if( !in_array( $_POST['cid'], $existing_courses)){
copy_course($FILE_LIB_XML, $FILE_ARCHIVE_XML, $FILE_USERS_XML, $cid );
And here is how i used your code to replace the above
Code:
$existing_courses = get_infos($FILE_ARCHIVE_XML, $cid);
if(!$existing_courses)
echo 'there was no data in that xml file';
else{
copy_course($FILE_LIB_XML, $FILE_ARCHIVE_XML, $FILE_USERS_XML, $cid );}
I can not understant why the archive.xml is being updaded and still the php returns the warning which disapears when i refresh the page
Thanks
-
Mar 15th, 2007, 01:26 PM
#6
Thread Starter
Lively Member
Re: copy xml data using php
I think i fnally understand why i get the warning but i have absolutely no idea how i can fix it. Take a look at the cide again
Code:
<?php
/* _________________________________________________________________________ */
include( 'includes/application_top.php' );
include( 'protection.php' );
$dc='http://purl.org/dc/elements/1.1/';
$dct='http://purl.org/dc/terms/';
// --------------------------------------------------
$cid = $_SESSION['lesson'];
$mtd=array();
$mtd=get_infos( $FILE_LIB_XML, $cid );
$PAGE_TITLE = 'Αρχειοθέτηση: '.$mtd["dc:title"][0];
$_SESSION['title'] = $mtd['dc:title'][0];
$bShowExtraData = TRUE;
$existing_courses=array();
$existing_courses = get_infos($FILE_ARCHIVE_XML, $cid);
if( !$existing_courses){
copy_course($FILE_LIB_XML, $FILE_ARCHIVE_XML, $FILE_USERS_XML, $cid );
mkdir("C:\arxeio\-Lesson id-$cid", 0700);
}
$content_column = 'usermenu.tpl.php';
include( $DIR_TEMPLATES.'main.tpl.php' );
?>
I use the code in the red to to see if the data exist in the archive.xml file. The don't and i get the warning and then in the if statement the data are being copied.
If i delete the red line i can't use the if statement. Any ideas?
-
Mar 15th, 2007, 01:39 PM
#7
Re: copy xml data using php
 Originally Posted by maki
That's the code i am using
Code:
if( !in_array( $_POST['cid'], $existing_courses)){
copy_course($FILE_LIB_XML, $FILE_ARCHIVE_XML, $FILE_USERS_XML, $cid );
And here is how i used your code to replace the above
Code:
$existing_courses = get_infos($FILE_ARCHIVE_XML, $cid);
if(!$existing_courses)
echo 'there was no data in that xml file';
else{
copy_course($FILE_LIB_XML, $FILE_ARCHIVE_XML, $FILE_USERS_XML, $cid );}
I can not understant why the archive.xml is being updaded and still the php returns the warning which disapears when i refresh the page
Thanks
no, you're doing that wrong. you are supposed to be using is_array inside your function and not calling foreach() if the $cnt variable is NOT an array. your function should be structured like this (of course, the "blah"s are simply filler that you must replace real code with -- this just gives you the general idea):
PHP Code:
function get_infos($xml, $id){ //<-- start function
//blah blah
$cnt = $child->childNodes;
if(is_array($cnt)){ //<-- this was added
foreach($cnt as $cn){
//blah blah blah
}
return $mtd;
}else //<-- this and the line below were also added
return false;
} //<-- end function
then, when you are calling get_infos(), you can use the code in my first post to see if the XML file had any information in it.
PHP Code:
$courses = getinfos($archive_xml, $id);
if(!$courses)
echo 'no data was found in the xml file';
else
copy_course($lib_xml, $archive_xml, $users_xml, $id);
-
Mar 15th, 2007, 01:42 PM
#8
Re: copy xml data using php
 Originally Posted by maki
I think i fnally understand why i get the warning but i have absolutely no idea how i can fix it. Take a look at the cide again
Code:
<?php
/* _________________________________________________________________________ */
include( 'includes/application_top.php' );
include( 'protection.php' );
$dc='http://purl.org/dc/elements/1.1/';
$dct='http://purl.org/dc/terms/';
// --------------------------------------------------
$cid = $_SESSION['lesson'];
$mtd=array();
$mtd=get_infos( $FILE_LIB_XML, $cid );
$PAGE_TITLE = 'Αρχειοθέτηση: '.$mtd["dc:title"][0];
$_SESSION['title'] = $mtd['dc:title'][0];
$bShowExtraData = TRUE;
$existing_courses=array();
$existing_courses = get_infos($FILE_ARCHIVE_XML, $cid);
if( !$existing_courses){
copy_course($FILE_LIB_XML, $FILE_ARCHIVE_XML, $FILE_USERS_XML, $cid );
mkdir("C:\arxeio\-Lesson id-$cid", 0700);
}
$content_column = 'usermenu.tpl.php';
include( $DIR_TEMPLATES.'main.tpl.php' );
?>
I use the code in the red to to see if the data exist in the archive.xml file. The don't and i get the warning and then in the if statement the data are being copied.
If i delete the red line i can't use the if statement. Any ideas? 
look at the post I made before this (above), and remove the line that defines $existing_courses as an array. ($existing_courses = array();)
edit: you should also remove the line defining $mtd as an array, because get_infos() will either return an array (if the XML file opened has information) or false (which cannot be an array). it'll be decided by the function itself -- so you don't need to worry about it.
-
Mar 15th, 2007, 02:05 PM
#9
Thread Starter
Lively Member
Re: copy xml data using php
That worked, the warning is disapeared. But now i get multi copies of the element in the new .xml file.
I've used your last code as seen below
Code:
$existing_courses=array();
$existing_courses = get_infosa($FILE_ARCHIVE_XML, $cid);
if(!$existing_courses){
copy_course($FILE_LIB_XML, $FILE_ARCHIVE_XML, $FILE_USERS_XML, $cid );
}
If the data don't exist i want them to be copied
i removed the $existing_courses=array() line but the same result (the$ mtd also)
Last edited by maki; Mar 15th, 2007 at 02:10 PM.
-
Mar 15th, 2007, 02:18 PM
#10
Re: copy xml data using php
you have to keep using your original method to check if the lessons exist in the file. and you can't put the call to copy_course() in that if() statement (as long as I'm following this all right), because if $existing_courses is FALSE, it means that $file_archive_xml has no data in it (and as far as I know, you don't want to try to copy information from an xml file that has no data in it, right?).
try something like this
PHP Code:
$existing_courses = get_infos($FILE_ARCHIVE_XML, $cid);
if(!$existing_courses)
echo 'no data in xml file';
elseif(!in_array($_POST['cid'], $existing_courses)){
copy_course($FILE_LIB_XML, $FILE_ARCHIVE_XML, $FILE_USERS_XML, $cid);
echo 'data has been copied';
}else
echo 'no data was copied';
edit: also, removing the part where you define $mtd and $existing_courses as an array wouldn't change anything, it was just useless to do that.
-
Mar 15th, 2007, 02:25 PM
#11
Thread Starter
Lively Member
Re: copy xml data using php
Maybe it is my mistake that i haven't posted the copy course function. Here it is
Code:
function copy_course($source_file, $dest_file, $users_file, $cid) {
// Open the XML and XSL files
$source = new DOMDocument;
$source->load($source_file);
// move down one level to the root's children
$children = $source->getElementsByTagName("course");
// iterate through the list of children
foreach ($children as $child) {
if( $child->getAttribute('ID') == $cid ) {
$dest = new DOMDocument;
$dest->load($dest_file);
// deletion date
$child->getElementsByTagName('modified')->
item(0)->nodeValue = date('Y-m-d');
$clone_child = $child->cloneNode(true);
// open user.xml and append the course's
// supervisors
$users = new DOMDocument;
$users->load($users_file);
$persons = $users->getElementsByTagName('person');
foreach( $persons as $person ) {
$lessons = $person->getElementsByTagName('lesson');
foreach( $lessons as $lesson ) {
if( $lesson->textContent == $cid ) {
$username_tag = $person->getElementsByTagName('username')->item(0);
$fullname = $username_tag->getAttribute('name').' '.
$username_tag->getAttribute('surname');
$user_node = new DOMElement('supervisor');
$user_node->nodeValue = $fullname;
$clone_child->appendChild($user_node);
}
}
}
$imported_node = $dest->importNode($clone_child, true);
$dest->documentElement->appendChild($imported_node);
$dest->save($dest_file);
return;
}
}
}
The archive.xml file is the destination file so even if there are no data in it no other file will be modified
-
Mar 15th, 2007, 03:54 PM
#12
Re: copy xml data using php
did you try the code that I posted? if you did, then what did it do?
-
Mar 15th, 2007, 03:56 PM
#13
Thread Starter
Lively Member
Re: copy xml data using php
It stores to archive.xml data more than once
-
Mar 15th, 2007, 04:06 PM
#14
Re: copy xml data using php
looking through the original code you posted, you aren't even using the $_POST variable. you store $cid by using $_SESSION. instead of calling in_array() with "$_POST['cid]", change it to "$cid" instead.
PHP Code:
$existing_courses = get_infos($FILE_ARCHIVE_XML, $cid);
if(!$existing_courses)
echo 'no data in xml file';
elseif(!in_array($cid, $existing_courses)){
copy_course($FILE_LIB_XML, $FILE_ARCHIVE_XML, $FILE_USERS_XML, $cid);
echo 'data has been copied';
}else
echo 'no data was copied';
if that doesn't work, could you just upload your project in a zip file so that I could look at it? specifically, just the XML files you're using for this problem, and all of the includes that have the functions you're using and the main include that is actually calling the functions. I don't need your entire website, or whatever, just the relevant stuff. It would be a LOT easier for me to look at on my own because as it stands, I have no clue what you're doing and what you're not doing.. and you don't seem to know PHP very well, either.
-
Mar 15th, 2007, 04:33 PM
#15
Thread Starter
Lively Member
Re: copy xml data using php
You are right. I don't know php.
I will upload some files but i don't think they will help you much. Meny data are in greek.
Last edited by maki; Mar 15th, 2007 at 05:57 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|