annie76 Posted January 5, 2007 Share Posted January 5, 2007 hi there! im relatively new to php so i apologise in advance if the answer is staring me in the face. i am trying to make a content mangement system where through the use of forms someone can edit the information in the xml document. below i have included the code for the three php pages i have so far. i was wondering if anyone could help me change the code on the third page so that i can edit the xml document according to what "storytype" is selected in stepone?i hope someone can help, thanks in advance!annie[b]page1: stepone.php[/b] <?phpif( ! ($fp = fopen( "./sponsor1.xml" , "r" )) ) die("Couldn't open xml file!");$person_counter = 0;$person_data = array();$xml_current_tag_state = '';function startElementHandler( $parser, $element_name, $element_attribs ){ global $person_counter; global $person_data; global $xml_current_tag_state; if( $element_name == "STORY" ) { // $person_data[$person_counter]["author"] = $element_attribs["AUTHOR"]; } else { $xml_current_tag_state = $element_name; }}function endElementHandler( $parser, $element_name ){ global $person_counter; global $person_data; global $xml_current_tag_state; $xml_current_tag_state = ''; if( $element_name == "STORY" ) { $person_counter++; }}function characterDataHandler( $parser , $data ){ global $person_counter; global $person_data; global $xml_current_tag_state; if( $xml_current_tag_state == '' ) return;if( $xml_current_tag_state == "STORYTYPE" ) { $person_data[$person_counter]["storytype"] = $data; } if( $xml_current_tag_state == "HEADLINE" ) { $person_data[$person_counter]["headline"] = $data; } if( $xml_current_tag_state == "ABSTRACT" ) { $person_data[$person_counter]["abstract"] = $data; } if( $xml_current_tag_state == "BODYTEXT" ) { $person_data[$person_counter]["bodytext"] = $data; }}if( !($xml_parser = xml_parser_create()) ) die("Couldn't create XML parser!");xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler");xml_set_character_data_handler($xml_parser, "characterDataHandler");while( $data = fread($fp, 4096) ){ if( !xml_parse($xml_parser, $data, feof($fp)) ) { break; // get out of while loop if we're done with the file }}xml_parser_free($xml_parser);?></p><p> <?phpfor( $i=0 ; $i < $person_counter ; ++$i ) { $link = $person_data[$i]["storytype"]; echo "<a href=\"steptwo.php?infoItem=$link\">$link </a><br><p>"; } ?> </p></body></html>[b]Page 2 steptwo.php[/b]<?php echo $infoItem; ?><p> </p><form METHOD=GET action="stepthreeA.php"> <p><br> one:<br> <textarea cols="20" rows="3" name="headlineTagContent">Enter headline here</textarea> </p> <p>two: <input type="text" name="abstractContent" value="enter story type" size="20" maxlength="20"> </p> <p>three:<br> <textarea cols="20" rows="3" name="bodyTextInput">Enter content for story here</textarea> <input type="submit" name="submit" value="SUBMIT"> </p></form></p>[b]page3: stepthreeA.php[/b]<?phpif( ! ($fp = fopen( "./sponsor1.xml" , "r" )) ) die("Couldn't open xml file!");$person_counter = 0;$person_data = array();$xml_current_tag_state = '';function startElementHandler( $parser, $element_name, $element_attribs ){ global $person_counter; global $person_data; global $xml_current_tag_state; if( $element_name == "STORY" ) { // $person_data[$person_counter]["author"] = $element_attribs["AUTHOR"]; } else { $xml_current_tag_state = $element_name; }}function endElementHandler( $parser, $element_name ){ global $person_counter; global $person_data; global $xml_current_tag_state; $xml_current_tag_state = ''; if( $element_name == "STORY" ) { $person_counter++; }}function characterDataHandler( $parser , $data ){ global $person_counter; global $person_data; global $xml_current_tag_state; if( $xml_current_tag_state == '' ) return;if( $xml_current_tag_state == "STORYTYPE" ) { $person_data[$person_counter]["storytype"] = $data; } if( $xml_current_tag_state == "HEADLINE" ) { $person_data[$person_counter]["headline"] = $data; } if( $xml_current_tag_state == "ABSTRACT" ) { $person_data[$person_counter]["abstract"] = $data; } if( $xml_current_tag_state == "BODYTEXT" ) { $person_data[$person_counter]["bodytext"] = $data; }}if( !($xml_parser = xml_parser_create()) ) die("Couldn't create XML parser!");xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler");xml_set_character_data_handler($xml_parser, "characterDataHandler");while( $data = fread($fp, 4096) ){ if( !xml_parse($xml_parser, $data, feof($fp)) ) { break; // get out of while loop if we're done with the file }}xml_parser_free($xml_parser);?> <?php$lengthOfArray = count($person_data); //echo $lengthOfArray; for( $i=0 ; $i < $lengthOfArray ; ++$i ) //echo $i; $thisName = $person_data[$i];//print_r ($thisName); $person_data[1]['headline'] = $headlineTagContent; $person_data[1]['bodytext'] = $bodyTextInput; $person_data[1]['abstract'] = $abstractContent; $filename = 'sponsor1.xml'; if (!$fp = fopen($filename, 'w+')) { print "Cannot open file ($filename)"; exit; } //$lengthOfArray = count($person_data); //echo $lengthOfArray; $somecontent = "<?xml version=\"1.0\"?>\n"; $somecontent .= "<sponsor_tab>\n"; for( $i=0 ; $i < $person_counter ; ++$i ) { $somecontent .= "<story>\n"; $thisName = $person_data[$i]; //print_r ($thisName); foreach($thisName As $myKey => $myContent) { $somecontent .= "<".$myKey . ">".$myContent . "</". $myKey .">". "\n"; } $somecontent .= "</story>\n\n"; } $somecontent .= "</sponsor_tab>\n"; //echo $somecontent; if (!fwrite($fp, $somecontent)) { print "Cannot write to file ($filename)"; exit; } //remembering to close the file. fclose($fp); ?>View the XML file you have just written to <a href="sponsor1.xml">sponsor1.xml</a></p> Link to comment https://forums.phpfreaks.com/topic/33022-editing-xml-document-through-php/ Share on other sites More sharing options...
EKINdesigns Posted January 6, 2007 Share Posted January 6, 2007 Whats wrong with generating the XML page using PHP? Link to comment https://forums.phpfreaks.com/topic/33022-editing-xml-document-through-php/#findComment-153894 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.