Gazan Posted February 9, 2008 Share Posted February 9, 2008 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 Link to comment https://forums.phpfreaks.com/topic/90265-showing-a-piece-of-requested-text-from-db/ Share on other sites More sharing options...
phpSensei Posted February 9, 2008 Share Posted February 9, 2008 <?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); ?> Link to comment https://forums.phpfreaks.com/topic/90265-showing-a-piece-of-requested-text-from-db/#findComment-462773 Share on other sites More sharing options...
phpSensei Posted February 9, 2008 Share Posted February 9, 2008 i did this quick, it gets the first 5 lines <?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); ?> Link to comment https://forums.phpfreaks.com/topic/90265-showing-a-piece-of-requested-text-from-db/#findComment-462778 Share on other sites More sharing options...
Barand Posted February 10, 2008 Share Posted February 10, 2008 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/>'; } ?> Link to comment https://forums.phpfreaks.com/topic/90265-showing-a-piece-of-requested-text-from-db/#findComment-462946 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.