hi fellows
I have a form that has four fields, when i submit I writes all the fields value to an XML file, its not writing but APPENDING so the new record comes to the end of file,

here is the code,

$joint = time();
$fname = "profiles/DB".".xml";
if($fp = fopen($fname,"a")) {
//fputs($fp,"<!xml version 1.0>\r\n");
fputs($fp,"\r\n\r\n<!-######## Profile for ".$POST['cmpname']." Starts here########-->\r\n\r\n");
fputs($fp,"<stdName>".$
POST['cmpname']."</stdName>\r\n");
fputs($fp,"<class>".$POST['cmpclass']."</class>\r\n");
fputs($fp,"<rollno>".$
POST['cmproll']."</rollno>\r\n");
fputs($fp,"<course>".$POST['cmpcourse']."</course>\r\n");
fputs($fp,"\r\n\r\n<!-######## Profile for ".$
POST['cmpname']." ends here########-->\r\n\r\n");
echo "\n\nProfile for <b>". $_POST['cmpname']."</b> is created \r\n\r\n";
}
else
echo "sorry, file is not created";

fclose($fp);

this code is working perfect and every time a new record is apended to the end of file, now i want to search this file on conditional basis,

the fours fields are
1 - student name
2 - class
3 - roll no.
4 - course

i want to search and pick the data on the basis of COURSE tag in xml file, e.g for student WEBMASTER i have data in XML like

<name>WEBMASTER</name>
<class>C-MOD</class>
<rollno>12121</rollno>
<course>PHP, HTML</course>

now if some one writes PHP and HTML in the search form it should pick this record up and display, if someone writes PHP or HTML only then also this record must be picked and if someone writes PHP,ASP,JAVA, HTML then also this record is desired to be picked,,,, and if the search words match more than one students profile then it must pick all of them and display in organized form.

I am just stuck with this searching trick and not getting how to do it, please give me ideas how can i search XML file using php implementing the above logic.

thanks in advance

    If I understood you right, you want to get data out of your XML based on different conditions. Right????
    If so why not use XSL

    Thanks in advance
    regards

      phparion wrote:

      its not writing but APPENDING so the new record comes to the end of file,

      That's because in your fopen() statement you set the file handle to append ('a') instead of write ('w'). Needless to say, the result is invalid XML, but then your XML is invalid anyway, as it lacks a root element. Also, it lacks any element to distinguish between one record and the next (not necessary for the document to be valid, but it would make using the file a lot simpler - leave it out and you might as well not use XML).

      <profiles>
          <profile>
              <name>WEBMASTER</name>
              <class>C-MOD</class>
              <rollno>12121</rollno>
              <course>PHP, HTML</course>
          </profile>
          <profile>
              <name>Widgery</name>
              <class>C-BASS</class>
              <rollno>42</rollno>
              <course>POP3, XML</course>
          </profile>
      </profiles>
      

        SimpleXML is what you would need to search the xml doc it comes standard with php5...

          Weedpacket wrote:

          That's because in your fopen() statement you set the file handle to append ('a') instead of write ('w'). Needless to say, the result is invalid XML, but then your XML is invalid anyway, as it lacks a root element. Also, it lacks any element to distinguish between one record and the next (not necessary for the document to be valid, but it would make using the file a lot simpler - leave it out and you might as well not use XML).

          <profiles>
              <profile>
                  <name>WEBMASTER</name>
                  <class>C-MOD</class>
                  <rollno>12121</rollno>
                  <course>PHP, HTML</course>
              </profile>
              <profile>
                  <name>Widgery</name>
                  <class>C-BASS</class>
                  <rollno>42</rollno>
                  <course>POP3, XML</course>
              </profile>
          </profiles>
          

          thanks for such an helpful reply thats what i was thinking and have structured very same code as you suggested,
          in addition, i want to store more than one profile in xml file thats why i used APPEND option....

          secondly i dont want anything in XML to work for me just want to store values in it and then read it from php so its just like a txt file to me HOWEVER in my assignment i must have to validate it from the online validator therefore i am a little worried about my syntax, once the search idea is clear then i will write DTD for it

          so dear, I am using your suggested style for writing profiles in XML file now how can i search it, my understanding is that i would have to EXPLODE it by PROFILE TAG and store in an array... after that again confusion that how would be i searching and what sort of regular expression should i use to search XML file and pick the related profiles only from the XML file ,
          please help me in this

          thank u very much for your help

            Okay; but if you want it to validate you will need a root element (I called mine "profiles").
            Oh........lesseee........

            $file = array_map('trim',file($xmlfilename));

            So you can have as much indentation as you like, it gets stripped out when you read the file and you don't have to worry about it any more.

            $file=join('', $file);

            And stick it all into one big string.

            preg_match_all('<profile>(.*?)</profile>', $file, $profiles);

            So now you've got an array containing everything that's between a pair of <profile>..</profile> tags (one array element per pair).

            See how you get with that idea.

              I am short with words to thank you for this great help, I am working over it and will get back to you with results
              once again thank you very much

                5 days later

                hi weedpacket

                i have tried this following code,

                CODE OF THE ACTION PAGE WHEN MY SEARCH FORM IS SUBMITTED

                <?php
                $file = array_map('trim',file("DB.xml"));
                $file=join('', $file);
                preg_match_all('<profile>(.*?)</profile>', $file, $profiles); 
                
                ?>

                and its giving this error

                Warning: Unknown modifier '(' in c:\apache\htdocs\sme\search_xml.php on line 11

                please note that line 11 here is the one hacing PREG_MATCH command its not allowing to use paranthesis and neither dot operation

                secondly i am also not able to bring up with some logic of searching it, because upto this step i will store values into the array profile-wise but then how would be i searching only the COURSE tag in xml file and then if any term matches my search form entery then to display who profile???

                i am uploading the search form please remove extra (txt) extension from this i added it to make it valid to upload here. sir, please help me to find out this solution my assignment date is very near to end..

                let me tell you the goal again, i want to search the COURSE tag which can have one or more than one courses listed in it, e.g it has ASP, HTML, PHP and if my user writes ASP only in search form it should display this students complete Profile and if someone writes even ASP, PHP, JAVA, FLASH ... as ASP and PHP matches in the COURSE tag of xml file so it should display all info in the profile of this student

                thank you very much

                  Oh, that's because I was just running something off the top of my head. Note from the documentation and examples that come with the function in the manual that there should be pattern delimiters.

                  /<profile>(.*?)<\/profile>/

                    Hi Weedpacket,

                    i have done the following coding and my problem has been solved though there is a logical error in it and its not giving me desired results. please have a look at the code

                    <?php
                    $file = array_map('trim',file("profiles/DB.xml")); 
                    $file=join('', $file); 
                    preg_match_all('/<profile>(.*?)<\/profile>/', $file, $profiles,PREG_SET_ORDER); 
                    print "<b>The Following Matches have been found</b><br><br>";
                    foreach ($profiles as $val) {
                       $temp = $val[0];
                    
                     if (preg_match_all('/'.strtolower($_POST['competence']).'/',$temp,$out) ) {
                         print "<br /><br /><pre>".$temp."</pre>";	
                     }
                    
                    
                    }
                    
                    ?> 

                    now it has the following problems

                    1 - the comparison is case sensitive and if i have PHP in xml file and some writes php in small letter it doesnt pick it so i was compelled to use STRTOLOWER function,,, is there any other way to remove the case-sensitive problem there?

                    2 - when i display the $temp it give me perfect result however after showing three tags its mixing the tags with each other i mean its not printing it one TAG value in one line...

                    e.g the output is like

                    Name : Haroon
                    Class : c-233
                    rollno : 11Courses:ASP, PHPExamDate:2005Fee:notpaid

                    so after printing it upto Roll no. it then start to write all the lines in one line and i want to show it in seperate lines

                    please tell me how to pring the Profile Data from $temp line by line to present it to user..

                    thanks u very much in advance.

                      Write a Reply...