Hello,
I'm currently working on a Non-Mysql CMS and i am doing the search engine feature right now. I'm going to make it everytime u do a news entry, forum post, download, image, chatbox, etc. that it will submit the information of that into a file for the search engine. The only thing im stuck on figuring out is how to do this for the database file for the search engine. I already got it using fopen and fwrite etc. for the information but i need it to add +1 to the number of the entry each time a new entry is made. Like it starts at s[0] (for search number 0) then s[1], s[2], s[3], etc. I needa know how to make a script that will add +1 to the last entry eachtime a new one is made... anyone got any ideas. Sorry if im a little rusty i been working at other job for nearly 3 months just to pay bills >.<. Any help is apreciated

Thanks,
Parabola

    any ideas? someone suggested i use CRLF's to count lines and then give me the number but i dont know how to do that really anyone know?

      i created a new way of doing it using XML

      <site>
      <page>
      <title>$title</title>
      <url>$url</url>
      <text>text</text>
      <content>$content</content>
      <rank>10</rank>
      </page>
      </site>

      it posts and edits that. but i gotta change it so when it makes a new post it deletes the previous line that says "</site>" so it can add it after the new one.. anyone know how i can make it delete previous line b4 i write?

        str_replace('</site>','',$string) then add what you need and insert </site> at the end

          The last line? Sure, load the file into an array using [man]file/man, use [man]array_pop/man to remove the last array piece (last line of file), and then from there you can use [man]implode/man to join the array pieces into one string.

            jmr i tryed urs couldnt get it to work

            brad could u show me the script to how u explained and ill give that a go.

            P.S. i counted the lines i hadda backspace and was gunna use ASCII backspaces to go back 8 spaces which would delete it but it wasnt working

            <?php
            $filename = 'search_data.xml';
            $somecontent = chr(8) . chr(8) . chr(8) . chr(8) . chr(8) . chr(8) . chr(8) . chr(8) . " <page>
            <title>$subject</title>
            <url>$url</url>
            <text>text</text>
            <content>$news</content>
            <rank>10</rank>
            </page>
            </site>
            ";

            if (!$handle = fopen($filename, 'a')) {
            echo "Cannot open file ($filename)";
            exit;
            }

            if (fwrite($handle, $somecontent) === FALSE) {
            echo "Cannot write to file ($filename)";
            exit;
            }

            echo "Success, wrote ($somecontent) to file ($filename)";

            fclose($handle);

            ?>

              Yeah... here's what I mean:

              $data = file('search_data.xml');
              array_pop($data);
              
              $data = implode('', $data);

              After that, $data will be a string with the last line of the .xml file removed.

                I notice that in your script there is a return after </site>. I am wondering if bradgrafelman's solution is just getting rid of the blank line.

                What didn't work with my way?? I have used this for a similar project and it worked fine. The function can't be applied to a file so you have to read the file into and array, apply the function to the array and then put it back in the file.

                $string = file_get_contents('filename');
                
                str_replace('</site>', '', $string);
                
                $string .= 'whatever you are adding and end with </site>';
                
                file_put_contents('filename', $string);

                This will replace the current contents of the file.

                  jmr i got this

                  Fatal error: Call to undefined function: file_put_contents()

                  brad wut u mean by this?

                  After that, $data will be a string with the last line of the .xml file removed.

                    I mean exactly what it sounds like. The entire file's contents will be loaded into that variable, with the exception of the last line of the file.

                    As jmrouleau points out, however, if you have an extra line break after the last line of data in your file, all that will be removed is that last EMPTY line. If this isn't what you want... then either remove any line breaks after the last true line, OR use something like a for() loop to keep removing the last line of data until the last line contains at least 3 characters or something like that. For example:

                    $data = file('search_data.xml'); 
                    
                    for($i = (count($data) - 1); strlen($data[$i]) > 3; $i--)
                         array_pop($data);
                    
                    $data = implode('', $data);

                    As to your fatal error when trying the other possible solution, you should look at the manual page for [man]file_put_contents/man and realize that this function is native to PHP5 only. Since it appears that you do not have PHP5, simply write the data into the file using the way you have been doing in PHP4 (fopen, fwrite, fclose -- or whatever you like to do).

                      Write a Reply...