Results 1 to 5 of 5

Thread: Writing a CMS

  1. #1

    Thread Starter
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Writing a CMS

    To create a CMS in PHP, is the best way to do it to save sections of the html to either say either a db or text file. This seems a bit messy but maybe the best way, for example say i have this html and i want to have a user be able to add more images to their page by just uploading an image and clicking a button ........,

    HTML Code:

    HTML Code:
    <table>
       <tr>
        
         <td> <img src = "i1.jpg" ></td>
    
       </tr>
    </table>

    Now i could save,

    HTML Code:

    HTML Code:
    <table>
       <tr>
    to say text1.txt

    and

    HTML Code:

    HTML Code:
    </tr>
    </table>
    to text2.txt,

    and the images to say text3.txt,

    HTML Code:

    HTML Code:
     <td> <img src = "i1.jpg" ></td>

    i could then say something like,


    PHP Code:
    <?php

    $fname 
    "text1.txt";

    $fo fopen($fname,r);

    $fr1 fread($fo,filesize($fname));

    fclose($fo);


    $fname "text2.txt";

    $fo fopen($fname,r);

    $fr2 fread($fo,filesize($fname));

    fclose($fo);


    $fname "text3.txt";

    $fo fopen($fname,r);

    $fr3 fread($fo,filesize($fname));

    fclose($fo);

    $f_final=   $fr1.$fr3.$fr2;

    echo 
    $f_final;

    ?>
    This would put the contents of all 3 text files into a single variable $f_final so that all i would need to do if i needed a new image added is add that images html to text3.txt.

    This seems like a pretty good way of doing things, i assume this is how CMS systems are made pretty much??

    So basically i could have a user frontend "addimg.html" which is for adding the image,




    HTML Code:
    <form method="post" action ="newimage.php">
    
    <input tpye = "text" name = "img1">
    <input type = "submit">
    
    </form>
    and then a modified version of the above php,


    PHP Code:
    <?php

    //save new image name to text3.txt ***********
    $newimgge_to_ADD $_POST["img1"];

    $fname "text3.txt";

    $fo fopen($fname,a);

    fwrite($fo,"\r\n".'<td> <img src = "'.$newimgge_to_ADD.'.jpg"> </td>');

    fclose($fo);
    //**********************

    //Open all the text files one by one and save their contents to a variable *****
    $fname "text1.txt";

    $fo fopen($fname,r);

    $fr1 fread($fo,filesize($fname));

    fclose($fo);


    $fname "text2.txt";

    $fo fopen($fname,r);

    $fr2 fread($fo,filesize($fname));

    fclose($fo);


    $fname "text3.txt";

    $fo fopen($fname,r);

    $fr3 fread($fo,filesize($fname));

    fclose($fo);

    //******************

    //Combine the contents of all 3 varables to one varable f_final  **********

    $f_final=   $fr1.$fr3.$fr2;

    //*********************

    //Print out the result *********************

    echo $f_final;

    //**************************


    ?>
    This would need a good bit of tweaking but i just want to know if i am on the right tracks..

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Writing a CMS

    that's a terrible way to manage content. super duper simple way of doing things:

    index.php:
    PHP Code:
    <?php
      
    //whatever page the user wants
      
    $page = (isset($_GET['page'])) ? $_GET['page'] : "main";

      
    //header page
      
    include("header.php");

      
    //include content
      
    if(file_exists($page ".php")){
        include(
    $page ".php");
      }else{
        
    //file doesn't exist, error page
        
    include("error.php");
      }

      
    //footer page
      
    include("footer.php");
    ?>
    header.php/footer.php/error.php can all contain actual PHP code and will be executed like a regular PHP script. then, if you requested index.php?page=main, "main.php" would be included along with the header/footer files. if you need to change images dynamically, use a database to store image URLs, rather than trying to store pieces of html in text files.

    hope that makes sense and was what you might have been looking for.

  3. #3

    Thread Starter
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: Writing a CMS

    I am ok with your header and footer parts as those parts are static code and never change but say i create a page for someone and they want to modify the page and the part they need to modify is burried deep in the pages html in some table or td or div or span etc.....how do i cleanly add to that section, all you said was,

    PHP Code:
    if(file_exists($page ".php")){
        include(
    $page ".php"); 
    but the point is that somewhere in the $page....php there need to be changes made.




    In the 'Welcome to Pastries' section "This is Pastries , a free, fully ...." of text i need to be able to let the user change that text using a type and click front end. I know you know what i am talking about but your answer didn't explain to me how i actually do this.

    So the section i am talking about has html,

    HTML Code:
    <div class="post">
                <h1 class="title"><a href="#">Welcome to Pastries </a></h1>
                <div class="entry">
                    <p>This is <strong>Pastries </strong>, a free, full      <---- this text
    I need the best way to "get in there" and append, or change, the text...
    Thanks again for your answers.

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Writing a CMS

    what I said in my earlier pos can also apply to any text at all:
    if you need to change images dynamically, use a database to store image URLs, rather than trying to store pieces of html in text files.
    whatever page you're including just needs to get all of the information that can be edited from the database.

    have you ever worked with MySQL?

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Writing a CMS

    Quote Originally Posted by kows View Post
    that's a terrible way to manage content. super duper simple way of doing things:

    index.php:
    PHP Code:
    <?php
      
    //whatever page the user wants
      
    $page = (isset($_GET['page'])) ? $_GET['page'] : "main";

      
    //header page
      
    include("header.php");

      
    //include content
      
    if(file_exists($page ".php")){
        include(
    $page ".php");
      }else{
        
    //file doesn't exist, error page
        
    include("error.php");
      }

      
    //footer page
      
    include("footer.php");
    ?>
    header.php/footer.php/error.php can all contain actual PHP code and will be executed like a regular PHP script. then, if you requested index.php?page=main, "main.php" would be included along with the header/footer files. if you need to change images dynamically, use a database to store image URLs, rather than trying to store pieces of html in text files.

    hope that makes sense and was what you might have been looking for.
    Watch out for file inclusion vulnerabilities with that code.

    The only proper way to mange content is to use a template system. You can write your own simple one very easily (I demonstrated with some code here) or use a whole pre built templating language such as Smarty.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width