Jump to content

editing XML file using PHP ... help


Nazirul

Recommended Posts

Hello All

 

this is the code for outputting XML file using PHP..

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Adding Songs...</title>
<style type="text/css">
em {
text-align:center;
}
</style>
</head>
<body>
<p>
<?php
$students = array();
$playlist_string = file_get_contents( 'student.xml' );
$parser = xml_parser_create();
xml_set_element_handler( $parser, 'start_element', 'end_element' );
xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse( $parser, $playlist_string) or die( 'Error parsing XML document.' );
echo '<br />';
if( $_POST['action'] == 'ins' ) {
    array_push( $students, array(
			'room_number' => $_POST['room_number'],
			'Student_Number' => $_POST['Student_Number'],
			'student_name' => $_POST['name'],
			'course' => $_POST['course'],
			'semester' => $_POST['semester'],
			'address' => $_POST['address'],
			'under' => $_POST['under'])
			);
    $students_final = $students;
}

else if($_POST['action'] == "del")
{
$students_final = array();
foreach($students as $student)
{
	if($student['Student_Number'] != $_POST['Student_Number']){
		array_push($students_final, $student);
	}
}
}

//Write XML vers and enctype
$write_string .= '<?xml version="1.0" encoding="iso-8859-1"?>';

// Write root tag open tag <root>
$write_string .= '<Perindu>';
$write_string .= '<Student>';
foreach( $students_final as $student )
{
    $write_string .= '<Room number="Perindu">';
    $write_string .= '<Student_Number>'.$student[student_Number].'</Student_Number>';
    $write_string .= '<Student_Name>'.$student[student_name].'</Student_Name>';
    $write_string .= '<Course>'.$student[course].'</Course>';
    $write_string .= '<Semester>'.$student[semester].'</Semester>';
    $write_string .= '<Undergraduated_From>'.$student[under].'</Undergraduated_From>';
    $write_string .= '<Address>'.$student[address].'</Address>';
$write_string .= '</Room>';  
}

// Write root tag close tag </root>
$write_string .= '</Student>';
$write_string .= '</Perindu>';

$fp = fopen('student.xml', 'w+');
fwrite($fp, $write_string) or die('Error writing to file');
fclose($fp);
echo '<em>News Article inserted or deleted successfully <em><br />';
echo '<a href="addstudent.php" title="return">Return</a>';

function start_element( $parser, $name, $attrs ){
    global $students;
    if( $name == 'student' ){
        array_push($students, $attrs);
    }
}
function end_element ($parser, $name){}
?> 
</p>
</body>
</html>

 

the problem was... it keep overwriting my XML file and it can only store 1 data... i've working for it for an hour but no luck... Master plz tell me where the errors..  ;D

 

thank you

Link to comment
https://forums.phpfreaks.com/topic/154879-editing-xml-file-using-php-help/
Share on other sites

havnt looked over the entire code, but you probably want

<?php
$fp = fopen('student.xml', 'a+'); //sets the file pointed at the end of the file after, and adds any content at that point...

 

if you want the new records at the top, then your gona have to read your file, then store it in a php variable/array, write your new information and append the old information or concatenate new and old to write it to a file....just a though...

 

PS- From the PHP Manual:

 

'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

 

w -will always replace your file contents... ;-)

+a.. it will formulate the invalid xml form...

 

i mean the <?xml.... will be twice or ++

 

ive change the code... but i still cant insert the XSL

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Adding Songs...</title>
<style type="text/css">
em {
text-align:center;
}
</style>
</head>
<body>
<p>
<?
$songs = Array();
function start_element($parser, $name, $attrs){
global $songs;
if($name == "song"){
	array_push($songs, $attrs);
}
}
function end_element ($parser, $name){}
$playlist_string = file_get_contents("student.xml");
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "end_element");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse($parser, $playlist_string) or die("Error parsing XML document.");
print "<br />";
if($_POST['action'] == "ins"){
array_push($songs, Array(
			"Student_Number" => $_POST['Student_Number'],
			"title" => $_POST['name'],
			"artist" => $_POST['artist'],
			"path" => $_POST['path'],
			"address" => $_POST['address'],
			"under" => $_POST['under']));
$songs_final = $songs;
}else if($_POST['action'] == "del"){
$songs_final = Array();
foreach($songs as $student){
	if($student['title'] != $_POST['name']){
		array_push($songs_final, $student);
	}
}
}
$write_string = "<?xml-stylesheet type="text/xsl" href="viewstudent.xsl"?>"
$write_string = "<Students>";
foreach($songs_final as $student){
$write_string .= "<song Student_Number=\"$student[student_Number]\" title=\"$student[title]\" artist=\"$student[artist]\" path=\"$student[path]\" address=\"$student[address]\" under=\"$student[under]\" />";
}
$write_string .= "</Students>";
$fp = fopen("student.xml", "w+");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
print "<em>Song inserted or deleted successfully </em><br />";
print "<a href=\"index.php\" title=\"return\">Return</a>";
?>
</p>
</body>
</html>

 

it result an error with message :

Parse error: syntax error, unexpected T_STRING in C:\wamp\www\collegeXML2\processForm.php on line 45

 

where line 45 is $write_string = "<?xml-stylesheet type="text/xsl" href="viewstudent.xsl"?>"

 

is it incorrect syntax? lol.. im tired..  ;D , helpp

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.