Jump to content

Showing a piece of requested text from db


Gazan

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/>';
}
?>

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.