How to display recent posts from database into php homepage?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yateesh
    New Member
    • Dec 2012
    • 36

    How to display recent posts from database into php homepage?

    I want is to display post details ( title, description, username ) on my home page within a div tag in a proper format like all websites have . The problem im facing is that all the data from database are getting displayed as a plain text , one below the other . I am new to php , so please guide me to achieve the result . Here is my code .

    I want to display in this tag :
    Code:
    <div id='display'>
         <h3 class='name'></h3>
         <h1 class='title' ></h1>
         <p class='desc'></p>
         <p class='cat'></p>
         <p class='sub_cat'></p>   
    </div>
    And my php code is :
    Code:
    <?php
         $row="";
    
         $link = mysql_connect("localhost","username","password");
         mysql_select_db("database");
         $query = "SELECT * from posts ORDER by post_id DESC limit 0,5";
         $result = mysql_query($query);
         $result = mysql_query($query) or die("Query to get blah  failed with error: ".mysql_error());
    
         while($row = mysql_fetch_array($result)) { 
         echo "<div id='display'>";
         echo "<h3 class='name'>".$row['username']."</h3>";
         echo "<h1 class='title' >".$row['post_title']."</h1>";
         echo "<p class='cat'>".$row['cat']."</p>";
         echo "<p class='sub_cat'>".$row['sub_cat']."</p>";
         echo "<p class='desc'>".$row['post_desc']."</p>";
         echo "</div>";
    	} 
    
    mysql_close($link);
    ?>
    I need the title to be a link . Thanks in advance .
    Last edited by Rabbit; Dec 3 '12, 04:37 PM. Reason: Use code tags <code/> around your code
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    What you have there should work. If you are getting just plain text, you probably don't have your styles set up correctly. Just because it's between certain elements doesn't mean it's going to look the way you want. You need to set up the styles correctly as well. You should post what the HTML looks like to the client to make sure that it's outputting the correct HTML.

    As for your second question, we only allow one question per thread in this forum. Please create a new thread for it.

    Comment

    Working...