+ Reply to Thread
Results 1 to 7 of 7

Thread: Get 'Posts' from an database and display them

  1. #1
    Bioshox is offline Programmer
    Join Date
    Oct 2009
    Location
    Manchester, UK
    Posts
    196
    Rep Power
    10

    Get 'Posts' from an database and display them

    Okay, so this little tutorial could be implemented into a blog, or forum script, so here goes:

    You will need a table set up called CMS, and rows within that called Title, Body, Name & ID (ID should be a primary key and auto increment)

    Okay so first we need to create a file called getposts.php

    First of all open your PHP tags and define the function name and parameters

    Code:
    <?php

            
    //Defines the function name
            
    function get_posts () {
    First we need to create a query and an IF statement that checks if the post exists

    Code:
    //Select everything from the the table
    $sql "SELECT * FROM cms ORDER BY id DESC";

    $res mysql_query($sql) or die(mysql_error());

    if(
    mysql_num_rows($res) != 0):

    //Reads the $res variable 
    while($row mysql_fetch_assoc($res)) { 
    If the number returned was a 1, it means there is a post in the database available to show. We must now tell PHP how to display it, and what error message to display if the post did not exist

    Code:
    //Makes posts titles into links
    echo  htmlspecialchars((stripslashes($row['title'])));

    echo 
    nl2br(stripslashes(($row['body'])));

    echo
    '<br /><h6>Posted By: ' stripslashes(($row['name'])) . '</h6>';

    }

    //If there were no rows, and the post didnt exist, then display an error message
    else:

    echo 
    '<p>We cant seem to find that post!</p>';

    endif;

    //Ends the function
    }    
    ?> 
    Okay so there you have it, we have created the function in order to get the posts, but we now need to declare the function and get the posts,

    Code:
        <?php
            get_posts
    ();
            
        
    ?>
    Just include this file on the page you want the posts to show up on and there you have it!

    (Don't forget to include your connection info)
    Last edited by Bioshox; 03-04-2010 at 01:24 PM. Reason: replace [code] tags with [php] tags

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Nov 2009
    Location
    London
    Posts
    866
    Blog Entries
    3
    Rep Power
    14

    Re: Get 'Posts' from an database and display them

    This is a good tut esp for those wanting to make there own forum / blog script. + rep

  4. #3
    Bioshox is offline Programmer
    Join Date
    Oct 2009
    Location
    Manchester, UK
    Posts
    196
    Rep Power
    10

    Re: Get 'Posts' from an database and display them

    Thank's mate !

  5. #4
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: Get 'Posts' from an database and display them

    Just thinking. why the $id? you don't use it? this code will show ALL posts in the table, not just one, as I understood seemed to be the meaning from the comments. Additionally, the code is plottered with loads of usually unnecessary stripslashes(), and this means that this is extremely dependent on how your data was stored to the database, on where there is nothing shown...
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  6. #5
    Bioshox is offline Programmer
    Join Date
    Oct 2009
    Location
    Manchester, UK
    Posts
    196
    Rep Power
    10

    Re: Get 'Posts' from an database and display them

    Quote Originally Posted by Orjan View Post
    Just thinking. why the $id? you don't use it? this code will show ALL posts in the table, not just one, as I understood seemed to be the meaning from the comments. Additionally, the code is plottered with loads of usually unnecessary stripslashes(), and this means that this is extremely dependent on how your data was stored to the database, on where there is nothing shown...
    That actually doesn't need to be there D:
    Thank's for pointing that out.

  7. #6
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: Get 'Posts' from an database and display them

    I'd also be happier if I saw a good indentation and whitespace usage so the code is more readable :-)
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  8. #7
    Bioshox is offline Programmer
    Join Date
    Oct 2009
    Location
    Manchester, UK
    Posts
    196
    Rep Power
    10

    Re: Get 'Posts' from an database and display them

    Quote Originally Posted by Orjan View Post
    I'd also be happier if I saw a good indentation and whitespace usage so the code is more readable :-)
    Your wish, is my command

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 12-18-2010, 10:13 AM
  2. Image Display / Database query
    By theslever in forum General Programming
    Replies: 1
    Last Post: 11-23-2010, 09:06 AM
  3. Display Data from a MySQL Database
    By hudbarnett in forum PHP Development
    Replies: 9
    Last Post: 08-31-2010, 02:37 AM
  4. Replies: 2
    Last Post: 04-27-2010, 12:06 AM
  5. Display Access database
    By jashsayani in forum Visual Basic Programming
    Replies: 11
    Last Post: 11-08-2009, 03:04 PM

Tags for this Thread

Bookmarks

Posting Permissions

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