Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    May 2008
    Location
    Lost in Localhost...
    Posts
    702
    Thanks
    3
    Thanked 43 Times in 42 Posts

    Display more than one post

    I am making a blog for me in PHP. Although the actual part that displays the posts, only displays 1. I'm a newbie to PHP and I don't know quite what's wrong. The script pulls the posts out of a database. (No admin CP for me!)

    Can anyone help me, I would like it to display 6 posts.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="Description" content=" " />
    <meta name="Keywords" content=" " />

    <title>My Blog</title>
    <link href="style.css" rel="stylesheet" type="text/css" />

    </head>

    <body>


    <?php
    define
    ('IN_BLOG'true);
    require_once 
    "config.php";

    $sql mysql_connect$mysqlHost$mysqlUsername$mysqlPassword) or die(mysql_error());
    mysql_select_db($mysqlDatabase$sql) or die(mysql_error());
    $row mysql_query("SELECT * FROM blog_posts") or die(mysql_error());

    while ( 
    $row mysql_fetch_array$row ) )
    {
    echo 
    "<h2>" $row['title'] . "</h2>" "<p>" $row['content'] . "</p><p><small>Posted by - <b>" .$row['author']. "</b> - on the date -<b>" substr($row['timestamp'], -1911) .".</b></p></small>";
    }
    ?>
    </body>
    </html>
    Thanks

    (If you need any more scripts from the blog, then let me know.)

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You're assigning $row to the mysql result and then again when you fetch. Use a different variable for the result:
    Code:
    $result = mysql_query("SELECT * FROM blog_posts") or die(mysql_error());
    while ( $row = mysql_fetch_array( $result ) )
    Take a look at mysql's LIMIT to show only 6 records:
    http://dev.mysql.com/doc/refman/5.0/en/select.html

    Good luck;

  • Users who have thanked Brandoe85 for this post:

    Millenia (08-11-2008)

  • #3
    Regular Coder
    Join Date
    May 2008
    Location
    Lost in Localhost...
    Posts
    702
    Thanks
    3
    Thanked 43 Times in 42 Posts
    Quote Originally Posted by Brandoe85 View Post
    You're assigning $row to the mysql result and then again when you fetch. Use a different variable for the result:
    Code:
    $result = mysql_query("SELECT * FROM blog_posts") or die(mysql_error());
    while ( $row = mysql_fetch_array( $result ) )
    Take a look at mysql's LIMIT to show only 6 records:
    http://dev.mysql.com/doc/refman/5.0/en/select.html

    Good luck;
    Thanks, can you answer me another?

    The blog posts are displayed from the oldest at the top, to the newest at the bottom, is there a way to change that? So it's like a normal blog. The latest added posts are displayed at the top.

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Sure- look at ORDER BY in the same link. You probably have a blog date, order by that:
    select fields from table order by dateField desc limit 6

    Good luck;

  • #5
    Regular Coder
    Join Date
    May 2008
    Location
    Lost in Localhost...
    Posts
    702
    Thanks
    3
    Thanked 43 Times in 42 Posts
    Quote Originally Posted by Brandoe85 View Post
    Sure- look at ORDER BY in the same link. You probably have a blog date, order by that:
    select fields from table order by dateField desc limit 6

    Good luck;
    I am a SQL noob aswell

    If you don't mind answering,
    How can I do that?

    PHP Code:
    $result mysql_query("SELECT * FROM blog_posts ORDER BY timestamp;
    "
    ) or die(mysql_error()); 
    This didn't work

  • #6
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    I'll go with your timestamp field (looking at your original post), so run the query like:
    Quote Originally Posted by Brandoe85 View Post
    Sure- look at ORDER BY in the same link. You probably have a blog date, order by that:
    select * from table order by timestamp desc limit 6

    Good luck;

  • #7
    Regular Coder
    Join Date
    May 2008
    Location
    Lost in Localhost...
    Posts
    702
    Thanks
    3
    Thanked 43 Times in 42 Posts
    Quote Originally Posted by Brandoe85 View Post
    I'll go with your timestamp field (looking at your original post), so run the query like:
    Thank you so much,
    it works perfectly!


  •  

    Posting Permissions

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