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 Quote 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); ?> Quote 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); ?> Quote 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/>'; } ?> Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.