Jump to content

Recommended Posts

Alright, i'm making a news system atm. I want to take out like 5 lines of a 10 line news post. I'd like to show 5 first lines, og the totalt 10 lines on the main page, and then you can click and view the entire post. but the question is, how do i pick out the 5 first lines from the database ?

many thanks

<?php

$lines = file_get_contents("lines.txt");
$temp  = explode("\n",$lines);

for($j=0;$j<5;$j++){echo $temp[$j];}

?>

 

i did this quick, it gets the first 5 lines

 

I used the file_get_contents, but you can use

 

 

<?php

$lines = fopen("lines.txt","r");
$lines2 = fread($lines,1204);
$temp  = explode("\n",$lines2);

for($j=0;$j<5;$j++){
  
echo $temp[$j] . "<br>";

}
fclose($lines);
?>

Assuming each line of the news item should be a max of 80 characters

<?php
$sql = "SELECT item FROM news";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res))
{
    $five_lines = array_slice(explode ("\n", wordwrap($row['item'], 80)), 0, 5 );
    echo nl2br($five_lines) , '<br/>';
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.