Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Simple PHP CMS

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #29427
    alexcoady
    Participant

    Here’s a really quick and easy program you can create using PHP – no database necessary for this (which can be a useful.. especially when learning PHP)

    When you load the page, you’ll see the contents from two .txt files nicely formatted. There’s a button that lets you edit the text, and then save it again. very easy, but a nice way to do it. Check the code working here: http://alexandercoady.co.uk/PHP/PHP_2/

    So the first file is index.php – a small file used to run the other PHP pages.

    Code:


    <br /> File Writer<br />


    Unless you’re totally new to PHP, that should all be very self explanatory.. However, if you’re not, read the next few lines and let me explain what each section of the code is doing.

    <html> shows that we’re writing in HTML
    <head> is a section of the HTML page we’re looking at
    <title> represents a variable that names the current page of HTML in a web browser. (The text at the very top! ^)
    </title> and </head> both signify that we’ve finished everything that needed to be in these sections.
    <link rel="stylesheet" href="style.css" type="text/css"> is the link to the cascading style sheet i’m using for this project
    <body> opens the main section of the code
    <?php shows that we’ve started using PHP code
    ($_GET == ‘edit’) ? include(‘edit.php’) : include(‘root.php’); is a chunk of PHP code that works as an IF STATEMENT. it’s saying: "If the suffix of page in the web URL is equal to the string ‘edit’, then include the php page edit.php, otherwise include the php page root.php

    And that’s index.php sorted! It’s an easy one, so let’s get to the slightly more intuitive bit.

    ***********************************************************

    root.php is the page that will show by default when we visit index.php

    Code:
    ‘;
    echo $title = file_get_contents(‘title1.txt’);
    echo ‘

    ‘;

    echo ‘

    ‘;
    echo $content = file_get_contents(‘content1.txt’);
    echo ‘

    ‘;
    }
    else if (file_exists(‘content1.txt’))
    {
    echo ‘

    No title

    ‘;
    echo ‘

    ‘;
    echo $content = file_get_contents(‘content1.txt’);
    echo ‘

    ‘;
    }
    ?>

    Edit this text externally

    It’s another easy one really.

    the first if statement is checking if 2 files exist in the directory. if they DO, it prints the title in <h2> tags for formatting, and the content in <p> tags.

    Note:

    Code:
    file_exists(‘content1.txt’)

    Checks if the file exists.

    Code:
    $title = file_get_contents(‘title1.txt’)

    assigns the contents of the file title1.txt to a variable called $title

    The 2nd If statement is for if just the content has been supplied, it prints the content, and says NO TITLE where the title should be.

    This adds page=edit to the url, so when passed the if statement from index.php knows which other php page to include.

    ********************************************

    The final page is the complicated one: edit.php

    Code:







    Current Preview

    Finished Editing

    But it’s actually dead nice! the first bit checks if a file is already in existence (content1.txt), if it is, it creates a variable called $content and sets the contents from content1.txt to $content.

    The next bit checks to see if you’ve entered stuff into the HTML form, it’s checking if both fields have been entered into.
    if this is true, so if you’ve typed stuff into the title field and the content field, then it writes the data you entered to those files.

    Note: You don’t need to create these files yourself, the FILE_PUT_CONTENTS() function creates one for you if it doesn’t already exist.

    Let me just explain file_put_contents() too,
    the first parameter to the function is the string name of the file you want to write to (ie. ‘content1.txt’) – the 2nd part (after the comma) is the content you want writing to that location.

    the content is handled using the $_POST[] option, which assigns any information added to a field to a variable (ie. $content = $_POST; gets the content added to content1 (the name given to one of the form fields) and applies that data to $content)

    the following if statements are for if only one of the 2 fields were entered in to.

    The next bit’s just a html form, i won’t go into that too much, just note that for content, I’ve included the actual current content in the text area, so when this page opens any data in content1.txt will be in the textarea, ready to be edited.

    The very bottom bit is just a section to preview the current entries.

    the following is the CSS to make it all look a bit nicer:

    Code:
    body
    {
    color: #4c4c4c;
    background-color: white;
    }

    h1
    {
    text-transform: uppercase;
    font-size: 20px;
    width: auto;
    padding: 5px;
    color: #e6e6e6;
    background-color: #0080ff;
    }

    h2
    {
    margin-bottom: -17px;
    font-size: 20px;
    text-transform: uppercase;
    padding: 5px;
    color: #e6e6e6;
    width: auto;
    background-color: #333333;
    }p.entry
    {
    padding: 5px;
    border-color: #333333;
    border-style: solid;
    border-width: 1px;
    }

    a:link
    {
    margin-top: -20px;
    float: right;
    font-size: small;
    text-transform: none;
    text-decoration: none;
    color: white;
    background-color: #b3b3b3;
    padding: 5px;
    border-color: #333333;
    border-style: solid;
    border-width: 1px;
    }

    a:visited
    {
    margin-top: -17px;
    float: right;
    font-size: small;
    text-transform: none;
    text-decoration: none;
    color: white;
    background-color: #b3b3b3;
    padding: 5px;
    border-color: #333333;
    border-style: solid;
    border-width: 1px;
    }

    a:hover
    {
    margin-top: -17px;
    float: right;
    font-size: small;
    text-decoration: none;
    text-transform: none;
    color: white;
    background-color: #cccccc;
    padding: 5px;
    border-color: #333333;
    border-style: solid;
    border-width: 1px;
    }

    hope that helps!

    Oh, & check it out here: http://alexandercoady.co.uk/PHP/PHP_2/

    – Alex Coady

    #79252
    The-Marshal
    Member

    wow that’s awesome thank you very much…

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Other’ is closed to new topics and replies.