Announcement

Collapse
No announcement yet.

Another php question.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Another php question.

    I need to make a php script that can write to a file on the server.

    this file would need to be formulated like so:

    Code:
    <?php
    
    $mso="010000000000|1|Home|pic|articles/news_page.php?menustr=010000000000|
    020000000000|1|About|pic|articles/article_page.php?article=g1&menustr=020000000000|
    030000000000|1|FAQ|pic|articles/article_page.php?article=g7&menustr=030000000000|
    035000000000|1|Search|pic|search/search.php?menustr=035000000000|
    .
    .
    .
    150000000000|1|Submit|pic|articles/article_page.php?article=g94&menustr=100000000000|
    160000000000|1|Links|pic|articles/article_page.php?article=g93&menustr=160000000000|";
    ?>
    Does anyone have a hint on how to do so? (the "endlines" are important)

    Basically I need the PHP script to write another PHP file that will be included elsewhere.
    Ngreth Thergn

    Ngreth nice Ogre. Ngreth not eat you. Well.... Ngreth not eat you if you still wiggle!
    Grandmaster Smith 250
    Master Tailor 200
    Ogres not dumb - we not lose entire city to froggies

  • #2
    I don't know on the writing/including, but the endlines should be easy enough. By this page, you should be able to use the standard escape character \n for a newline (or if the system doesn't like you, \n\r for linefeed and carriage return combo) when you're writing out. So a string "My\nmany\nlines" would come out looking like
    "My
    many
    lines"

    Comment


    • #3
      The proably best way to do it would be to have $mso=file(somefile); to read it in, and then do depending how you stored it

      If you have it in an array (I hope you do)

      $fp=fopen("somefile","w+");
      fputs($fp,implode("|",$somearrayofdata)."|\n"));
      fclose($fp);

      Or if not in an array,

      $fp=fopen("somefile","w+");
      fputs($fp,$myvar1."|".$myvar2."|".$myvar3."|\n");
      fclose($fp);

      (With as many $myvars as you have swapping them for your variables of course)

      Comment


      • #4
        If you can build the array that you want to write to another file then you can use the serialize function to store it in a writeable format in another file that can be read (or stored in the DB if you Base64Encode it to make it SQL Safe).

        Basicly i would suggest you create your array of data, run the serialize and base64 encode functions on it and then write it whereever you want to. When you want to use it again, simply read it into a variable, base64decode and unserialize it.

        You'll take a bit of a speed hit but one that's actually not as bad as your would think. And it's probably actually not as bad as writing your own script to do the serialization yourself (which is basicly what you're looking for).
        Master Zaepho

        Comment


        • #5
          This is for the left hand menu... since it shows on every page, speed is of the essense.

          So I absoultely require it to avoid calling the DB to draw it.

          I am thinking of useing the db to store it, then if someone makes a change... re-write the file that is used for the left hand menu, keeping it fast.
          Ngreth Thergn

          Ngreth nice Ogre. Ngreth not eat you. Well.... Ngreth not eat you if you still wiggle!
          Grandmaster Smith 250
          Master Tailor 200
          Ogres not dumb - we not lose entire city to froggies

          Comment


          • #6
            Then the best way to achieve it is to have a template, with say {mso} where that $mso variable is so $mso={mso};

            And then when the db changes, use that template to read in the changes in from the database or file as I suggested earlier, so you write it but you have a template and a data file to form them, but you only do the process when its actually changed.

            Comment


            • #7
              Ok. I will repeat that I taught myself php

              I have no idea what you just said
              Ngreth Thergn

              Ngreth nice Ogre. Ngreth not eat you. Well.... Ngreth not eat you if you still wiggle!
              Grandmaster Smith 250
              Master Tailor 200
              Ogres not dumb - we not lose entire city to froggies

              Comment


              • #8
                Thats ok, I taught myself too. its kinda hard to explain without real files and stuff to show you (especially with such a small typing window) but..

                filea.txt = template
                fileb.txt = data

                write the changes to fileb as I suggested earlier to get your format. so fileb contains

                010000000000|1|Home|pic|articles/news_page.php?menustr=010000000000|
                020000000000|1|About|pic|articles/article_page.php?article=g1&menustr=020000000000|
                030000000000|1|FAQ|pic|articles/article_page.php?article=g7&menustr=030000000000|
                035000000000|1|Search|pic|search/search.php?menustr=035000000000|
                .
                .
                .
                150000000000|1|Submit|pic|articles/article_page.php?article=g94&menustr=100000000000|
                160000000000|1|Links|pic|articles/article_page.php?article=g93&menustr=160000000000|

                filea contains

                <? $mso="{mso}"; ?>

                filec.php (a new file) contains

                <?
                $template=file_get_contents("fileb.txt");
                $data=file_get_contents("filea.txt");

                $fp=fopen("filed.php","w+");
                $template=str_replace("{mso}",$data,$template);
                fputs($fp,$template);
                fclose($fp);
                ?>

                this writes filed.php - which is your menu, and contains

                <? $mso="010000000000|1|Home|pic|articles/news_page.php?menustr=010000000000|
                020000000000|1|About|pic|articles/article_page.php?article=g1&menustr=0200000000
                00|
                030000000000|1|FAQ|pic|articles/article_page.php?article=g7&menustr=030000000000
                |
                035000000000|1|Search|pic|search/search.php?menustr=035000000000|
                150000000000|1|Submit|pic|articles/article_page.php?article=g94&menustr=10000000
                0000|
                160000000000|1|Links|pic|articles/article_page.php?article=g93&menustr=160000000
                000|"; ?>

                PS - if you wanna send me a message so we can sort it out off list, Im more than happy, one thing you might like to consider is even though you arent in the uk, theres a PHP list who are always happy to help people.. We have non UK people there too

                New Mail ....:- mailto:PHP_UK@elliott.uk.com
                Subscribe ...:- mailto:PHP_UK.sub@elliott.uk.com
                Unsubscribe .:- mailto:PHP_UK.Unsub@elliott.uk.com
                Archive .....:- http://www.mail-archive.com/php_uk@elliott.uk.com

                Comment


                • #9
                  Thanks... That works
                  Ngreth Thergn

                  Ngreth nice Ogre. Ngreth not eat you. Well.... Ngreth not eat you if you still wiggle!
                  Grandmaster Smith 250
                  Master Tailor 200
                  Ogres not dumb - we not lose entire city to froggies

                  Comment


                  • #10
                    Glad to help. Im a kind PHP addict so.. Always happy to help.

                    Comment


                    • #11
                      Sorry to post to this so late, but I would suggest taking a look at http://www.massassi.com/bTemplate/ for options on making your site a little easier to update. Basically it's a stripped down (but still amazingly functional) templating system called bTamplate. We use it here at work almost exclusively(sp?) because of it's insane speed.

                      Also, if you're REALLY interested in a speedy, functional templating system, take a look at the article he references in his last update.
                      Raskii
                      author of Raskii's Exhaustive Tailored Armors Listing (site comments/suggestions)
                      Current character: Rathish (Stromm)

                      Comment

                      Working...
                      X