Show only part of a record from mySQL?

Discussion in 'PHP' started by mokimofiki, Jul 28, 2009.

  1. #1
    If I have a paragraph stored in a mySQL database can I just have it show so many lines of it in the output for example:


    DATABASE RECORD:
    This is an article about showing only part of a mysql database record. I hope this helps more than just me. Thank you for all of your help and I hope it works because this would be a great help to a website I am working on.

    DISPLAY ONLY:
    This is an article about showing only part of a mysql databaserecord, I hope this helps more than just ....

    Then I will have a link to read more.

    Thank you in advance :)
     
    mokimofiki, Jul 28, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Here's a truncate function that I use for purposes like this:

    
    function truncate($data, $length = 150)
    	{
    		$data = strip_tags($data);	
    		
    		return substr($data,0,$length.'...');
    	}
    
    PHP:
    You could modify this to include a link instead of ...

    You can also do this on a database level.

    SELECT LEFT(text_field,150) AS truncated_text FROM my_table;

    Or even something like:

    SELECT
    CONCAT(
    '<p>',
    LEFT(text_field,150),
    ' <a href="link_to_page.php?id=',
    page_id,
    '"> Continue reading...</a></p>') AS formatted_string FROM my_table;
     
    Last edited: Jul 28, 2009
    jestep, Jul 28, 2009 IP
  3. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #3
    Aparently you have helped me quite a bit +rep although it won't let me it says I must spread around first. Thank you :)
     
    mokimofiki, Jul 28, 2009 IP