updating xml file using php -
hey there have xml file has need add event same location name
<timetable> <location name="a" > <event > <title>event title </title> <subtitle>amman -text </subtitle> <description>amman -text </description> </event> </location> </timetable> so can
<timetable> <location name="a" > <event > <title>event title </title> <subtitle>amman -text </subtitle> <description>amman -text </description> </event> <event > <title>event title </title> <subtitle>amman -text </subtitle> <description>amman -text </description> </event> </location> </timetable> i got stuck in php code want cheack if event created or not if created modify xml name of event , add new 1
this full php inserting code `
if(isset($_get['coname'])){ $coid = $_get['id']; $cname=$_get['coname']; $title = $_post ['title']; $sub = $_post ['sub']; $description = $_post ['description']; $location = $_post ['location']; $event = $_post ['event'] ; $str =$_post ['str'] ; $end =$_post ['end'] ; $topic = $_post ['topic'] ; $sql="insert timeline (title,sub,description,location,event,str,end,topic,coid) values ('$title','$sub','$location','$location','$event','$str','$end','$topic','$coid')"; if (!mysqli_query($con,$sql)) { die('error: ' . mysqli_error($con)); } echo "1 record added"; $q = mysqli_query($con,"select * timeline coid = $coid") or die(mysqli_error()); $xml = '<timetable start="'.$st.'" end="'.$en.'" interval="'.$in.'" title="'.$da.'">'; while($r = mysqli_fetch_array($q)){ $loc=$r['topic']; $evns=$r['str']; $evne= $r['end']; $xml .= '<location name="'.$loc.'" subtext=" ">'; $xml .= '<event start="'.$evns.'" end="'.$evne.'">'; $xml .= "<title>".$r['title']."</title>"; $xml .= "<subtitle>".$r['location']."</subtitle>"; $xml .= "<description>".$r['description']."</description>"; $xml .= "</event>"; $xml .= "</location>"; } $xml .= "</timetable>"; $sxe = new simplexmlelement($xml); $sxe->asxml('xml/'.$cname.'.xml'); `
amer, rather creating xml strings, i'd use simplexml methods:
inserting new <event> in existing xml:
$xml = simplexml_load_string($x); // assume xml in $x $loc = $xml->xpath("location[@name = 'a']")[0]; // select <location> name = $event = $loc->addchild("event"); $event->addattribute("start", "2013-05-20 10:00:00"); $event->addattribute("end", "2013-05-20 14:30:00"); $event->addchild("title", "some title"); $event->addchild("subtitle", "some subtitle"); $event->addchild("description", "some description"); see working: http://codepad.viper-7.com/12xtvd
Comments
Post a Comment