Jump to content

display first 4 lines of an article


php_newB

Recommended Posts

Hi

 

I am a php newbee and I am building a CMS which stores articles in a database.

I know how to display the whole article but what I want to do is to get an article from the database and display the first 4 lines of that article with a link for readers to read the whole article. so I was wondering is there anyway way to do this.

 

Thanks in advance

 

Link to comment
https://forums.phpfreaks.com/topic/43239-display-first-4-lines-of-an-article/
Share on other sites

sure their is, no problem xD

because you did not provide any code, i will be using assumptions

 

<?php
// assuming we are connected to a database with type MySQL
// and we have already queried the database, and saved that in a variable called $result...
while ($row = mysql_fetch_assoc($result)) {
    echo substr($row['article_body'], 0, 186) . "..."; // displays 186 characters from your article
    printf("<p><a href='article.php?id=%s'>Read More</a></p>", $row['article_id']); // the read more link
}

// adjust to your own needs, it is not the best example but it should get you started
// from programmer to programmer, i would also give you the advice to "abuse" the search function option
// on www.php.net, so you can look up functions and what they do, like substr() for exampe!
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.