A little about myself, I've been creating websites for about 6 years now and finally have gotten around to learning PHP. Coming from a background of mostly mud object oriented code and javascript I'm finding the learning process easy enough. My only problem is that I need a few examples of each particular function in use before I have a good enough grasp on it to use it to it's full extent.

In that, the tutorials I have found have been excellent for giving me a good overall picture of how to write in php and I find cranking out dynamic pages quite fun. However, there are from what I can tell some less common uses of PHP that are not as well documented in tutorials or even in the PHP manual's description of individual functions.

My particular problem at the moment is that I am trying to build a php script that will when executed with the correct password and input, take a html page and update a particular part of it's content. (eg: switch one image with another, add an image, add a link, add new text etc.) The interface on this script will be quite user friendly so that someone who knows practically nothing of html can update the content of a page easily.

For the most part this script is easy enough to design and in fact I have it at least 50% complete. The problem that is holding me up however is this. I need a way to get the posted data from the user side of the script into the html file. To that end I have come up with three different ways to do just that.

Option (1)
I put commented out unique tags into the html files to determine the beginning and end of a particular piece of content. I then with the script read the html file chosen by the user into a string that I then search for the beginning and ending tags with whatever might be between the two probably represented by a wildcard character. I replace that and only that part of the string with the input I got from the user enclosed in two tags identical to the ones I just found and then open the html file for writing and write the string to the file, closing it when done.

Option (2)
Same as above, but instead of using tags I keep the particular bit of content I want to be able to replace on a certain line in the html file I then replace that line and only that line with the user's input and save the file. This is a method I would rather avoid as it is more complex and potentially more unstable with regards to updates to the html files.

Option (3)
I create three different files for each html page I want on the site. The first is the actual page that will be the end result, the second the template design for that particular page and the third a file containing the content of each content section of that page. I read the template which contains variables for content into a string, read the content data from the not updated parts of the page into appropriate variables and then read the user's input into the variable that will be updated. I then write the whole thing to a html file that will be viewable and update the content file to include the newly updated input.

Any of these options would work but this is where I am having trouble. It is hard finding information on certain functions that I need to implement any of my ideas. I can get data from forms to the variables, I can read the html file into a string, I can save that string to a file. But what I can't do is replace only a very specific part of the string that could be of any length and is enclosed by tags. I also don't know how to edit only a specific line of a string if there is such a thing once a string is created. And I don't know effectively how to format a file so that I can put data into it and then retrieve each individual piece of data as a variable. I don't know the functions or there use. I have been experimenting with str_replace and preg_replace for the string replacing but I have yet to find success and I am not sure where to start with the data file. (Note I would really like to avoid the need to install and use mysql or another database filing system.)

If anyone could help me by outlining the functions I need and giving me a few basic examples of their use in the conditions I am describing I would be quite grateful as it will save me perhaps another week of searching for the answers I need.

Thanks in advance. - Naito

    the best way to do it is probably to embed all the html within a php file. Within this html place calls to php for your changing output at specified areas.

    If you are using user input use the: <form method="post" action="<?php echo $SERVER['PHP_SELF'];?>"> around the whole html form, so that when the user users a submit button the form itself can be refreshed, but user input can be validated, processed, and then output on the same page.
    For example use of if(isset($
    POST['submit'])) calls at tope can then allow you to amend /

      the users input, stor in say arrays , session varibales depending on what you want to do, and then output at specific areas.
      If you have multiple choices i.e. different pictures, just use nested if's or if elses at the php calls on the html so the proper selection is made.
      There are probably many other ways to do it, but from what i can understand this would probably be the best method for you to use.

        The only problem I can see with that method is storing data from previous edits. What I would like to do is set up someone who knows nothing about html with a website that I have created and customized for them, and then have them be able to update content on it without further assistance from me unless they need the layout changed. I have considered creating the templates in a php file and creating variables for each piece of content with default values that can be changed based off of the user (ie: admin's) input on the update script and then having it batch save to files. The problem is that as far as I'm aware I can't update my script itself when each piece of content changes so that the variables default values change to reflect what the user previously changed. I also am at the moment unsure on how to store multiple values of varying length in a data file outside the script that can be updated and yet retrieve them to use as default variables. I could make an individual data file for each piece of content. But we are probably talking about 30 html pages and atleast 3 probably as many as 5 content locations on each page. That would create a huge number of files in the html directory or a subdirectory there of and would be a pain to keep organized.

        -Naito

          is it me or am i reading more into this than whats needed.

          Basically you want a CMS, Content Management System of some kind, for someone to edit the content of your page(s).

          So why not hold your content in either a database or text file, then using the include command and a refference varible to pull the content needed to be displayed within the webpage.

          Then creating an admin area, you allow the user to interface with the content, which is held else where (database/file).

          the admin area pulled the data from which ever record or refference you want to edit, it then is displayed in editable text field/comment box. You make the changes you desire and click save. the new data is then wrote back to either the file or database and your content is then updated.

          You can extend this so when a user is logged in, the webpage showing content changes and adds a new link (EDIT PAGE) the user can then click this, where that corresponding page is loaded ready to edit.

          I do this for any website i wish for the owners to update, like new articles, jobs, press releases etc, and works well.. And these are users who have no web experience.

            Yes, that would be workable and pretty much exactally what I would want to do. The problem is I need to know the function/command eg ( file_get_contents(filename.extension) ) that I need to pull only the piece of data I would need from the database/file. I don't want to have to install mysql or anything other then just php on the server to get it to work. I can't seem to find anywhere the full function/command with paramaters to pull specifically what I need from a file.

              Naito wrote:

              I also am at the moment unsure on how to store multiple values of varying length in a data file outside the script that can be updated and yet retrieve them to use as default variables.

              easy, each record is held on its own line, you hold all content to that record on the same line, splitting each varbile if more than one is needed for that record but using a chr to define the the split of one varible and the start of the other. EG

              name#age#address

              then when you are reading in the file line by link, you make use of the explode and implode commands.

              Then the 3 different data sections are split of and placed in an array.

                I have considered using one of the many open source CMS systems out there but they are all too complex for what I need, most of them generate the site itself dynamically which takes up more cpu than what would be needed for this particular setup and I need to learn how to write this anyways. The only thing the site needs php for is simplicity and ease of use for the owner/administrator of the site to get the content they want updated quickly and without lots of offline editing and uploading.

                  ok use the following as an example to the ideas i am pointing you to.

                  create a file, display.php and use this

                  This is classed as your template and is mainly html, with some refferences to php.

                  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
                  <html>
                  <head>
                  <title>Untitled Document</title>
                  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
                  </head>
                  
                  <body>
                  <? include('content.php');?>
                  <table width="100%"  border="1" cellspacing="0" cellpadding="0">
                    <tr>
                      <td><? echo $title; ?></td>
                    </tr>
                    <tr>
                      <td><? echo $content; ?></td>
                    </tr>
                  </table>
                  </body>
                  </html>
                  
                  

                  Now create the next file. content.php

                  <?
                  switch ($page) {
                  case "0.0":
                  	$title = "Page1";
                  	$page_content = "Content1";
                      break;
                  
                  case "1.0":
                  	$title = "page2";
                  	$page_content = "Content2";
                      break;
                  
                  default:
                  	$title = "none found";
                  	$page_content = "none found";
                  }
                  ?>
                  

                  now type in your address bar display.php?page=1.0

                  then chage the 1.0 to 2.0 then try without.

                  Doing the above will show you how the content is made dynamic, and can change easy using just a refference in the address bar. the content file is read in at the start and only takes the data and sets the varibles needed which are equal to the page refference.

                  The data is then dropped where it needs to be with the echo statements.

                    I see, that is helpful as it gives me a few more ideas on dynamic pages, but it still doesn't tell me how to create an updatable database file, specifically how to save the varied length values and retrieve them. / assign them to variables that I can use. I'm sorry to be so troublesome but I am just learning php and those are the things left out of the tutorials I've read.

                      ok, i am pushed for time, so cant find some things you will need but can point in right place

                      <?
                      $line = "neil#28#uk";
                      
                      $content = explode("#",$line);
                      
                      echo "name :" . $content[0] . "<br>";
                      echo "age :" . $content[1] . "<br>";
                      echo "from :" . $content[2] . "<br>";
                      
                      ?>
                      

                      try this, and imaging the $line has already been taken from the file.

                        Ahhh.... Thank you. I'll have to test it out, but I believe you just gave me everything I need to finish the script.

                          n_wattam wrote:

                          here try this link..

                          this should help you a lot on how to read write to files using them as databases.

                          http://www.designdetector.com/archives/04/10/FlatFileDatabaseDemo.php

                          Once again thanks alot, I believe that completely solves my question. =) You've saved me perhaps another week or so of googling and reading random php tutorials to find out how to use datafiles and thats a wonderful thing. Thank you so much.

                            np 🙂 , bookmarked that page my self, as i've never done database flat files before.

                              Write a Reply...