开发者

How to just show few lines from a whole blog-post on a certain page?

开发者 https://www.devze.com 2023-04-05 22:03 出处:网络
I am making a blog in which I want to show just the summary of all posts on the home page rather than whole posts.

I am making a blog in which I want to show just the summary of all posts on the home page rather than whole posts. Below is the function that I use to get the posts on my home page.

function get_content($id = '')
{
    if ($id != ""):

        $id = mysql_real_escape_string($id);
        $sql = "SELECT * from cms_content WHERE id = '$id'";
        $return = '<p><a href="http://localhost/www/">Go back to Home page</a></p>';
        echo $return;
    else:

        $sql = "select * from cms_conten开发者_如何学Ct ORDER BY id DESC";
    endif;

    $res = mysql_query($sql) or die(mysql_error());
    if (mysql_num_rows($res) != 0):

        while ($row = mysql_fetch_assoc($res)) {
            echo '<h1><a href="index.php?id=' . $row['id'] . '">' . $row['title'] . '</a></h1>';
            echo '<p>' . "by: " . $row['author'] . ", posted on: " . $row['date'] . '<p>';
            echo '<p>' . $row['body'] . '</p><br />';
        }
    else:
        echo '<p>We are really very sorry, this page does not exist!</p>';
    endif;
}

And then simply I use this php code to get the posts:

<?php

if (isset($_GET['id'])) :
    $obj->get_content($_GET['id']);
else :
    $obj->get_content();
endif;
?>

So how to get the summary only using php function or mysql as I know that we can use mysql query to get limit the words?


function sumarize($your_string, $limit_lines){
   $count = 0;
   foreach(explode("\n", $your_string) as $line){
       $count++;
       echo $line."\n";
       if ($count == $limit_lines) break;        
   }
} 
sumarize($row['body'], 10);

will show first ten lines


1-lined:

echo '<p>'.implode("\n",array_slice(explode("\n",$row['body']),0,10)).'</p>';

instead of line with $row['body']

0

精彩评论

暂无评论...
验证码 换一张
取 消