Limit Text Display

Discussion in 'PHP' started by vikingbear_ds, Dec 29, 2007.

  1. #1
    Hi, Using Dreamweaver, PHP and mySQL..
    calling data from the database and displaying it on page, but would like to only display 2 lines or say 200 characters (which ever is easiest) followed by ...

    Here is my code, can anyone shed light on how to do this.. Cheers

    <td>
    <h3>
    <a href="tour-details.php?<?php echo $MM_keepURL.(($MM_keepURL!="")?"&":"")."ID=".urlencode($row_rsNSWSS['ID']) ?>" class="linkblue"><?php echo $row_rsNSWSS['Synopsis']; ?></a>
    </h3>
    </td>

    PS: I am not a programmer, hence teh use of Dreamweaver to do all the hard work :)
     
    vikingbear_ds, Dec 29, 2007 IP
  2. DarkMindZ

    DarkMindZ Guest

    Messages:
    175
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    So you need to trim it?

    <?php
    function short_name($str, $limit)
    {
    // Make sure a small or negative limit doesn't cause a negative length for substr().
    if ($limit < 3)
    {
    $limit = 3;
    }

    // Now truncate the string if it is over the limit.
    if (strlen($str) > $limit)
    {
    return substr($str, 0, $limit - 3) . '...';
    }
    else
    {
    return $str;
    }
    }
    ?>
     
    DarkMindZ, Dec 29, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    
    SELECT `ID`, SUBSTRING(Synopsis, 1, 200) FROM `table`
    
    Code (sql):
    Change your query to something similar to this.
     
    nico_swd, Dec 29, 2007 IP
  4. vikingbear_ds

    vikingbear_ds Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Tried the substring option, but I couldn't get it working..
    The text did not display at all..
    Here is the SELECT statement I have and the DISPLAY statement I use..

    mysql_select_db($database_RDVF, $RDVF);
    $query_rsNSWSE = "SELECT ID, City, GroupDivider, TourTitle, Synopsis FROM tourdetails WHERE GroupDivider = 'NSWSE' ORDER BY TourTitle ASC";
    $rsNSWSE = mysql_query($query_rsNSWSE, $RDVF) or die(mysql_error());
    $row_rsNSWSE = mysql_fetch_assoc($rsNSWSE);
    $totalRows_rsNSWSE = mysql_num_rows($rsNSWSE);


    <a href="tour-details.php?<?php echo $MM_keepURL.(($MM_keepURL!="")?"&":"")."ID=".urlencode($row_rsNSWSE['ID']) ?>" class="linkblue"><?php echo $row_rsNSWSE['Synopsis']; ?> </a>

    Any input greatly appreciated..

    regards, Ken
     
    vikingbear_ds, Dec 29, 2007 IP
  5. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #5
    
    <td>
    <h3>
    <a href="tour-details.php?<?php echo $MM_keepURL.(($MM_keepURL!="")?"&":"")."ID=".urlencode($row_rsNSWSS['ID']) ?>" class="linkblue"><?php echo substring($row_rsNSWSS['Synopsis'],0,300); ?>...</a>
    </h3>
    </td>
    
    PHP:
    Peace,
     
    Barti1987, Dec 29, 2007 IP
  6. selling vcc

    selling vcc Peon

    Messages:
    361
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    function split_text($text, $number_of_words){
    	return implode(' ',array_slice(explode(' ',$text),0,$number_of_words));
    }
    PHP:
    This function return the first X words of a string assuming that the word separator is ' ' (space), this function is very basic of course and may cause problems if your string contains html

    
    echo split_text("Hey! What's up?", 1) 
    //  Output: Hey!
    
    
    echo split_text('<a href="#">This is a link</a>', 5) 
    //  Output:   <a href="#">This is a link 
    //  (you have a missing </a> tag)
    PHP:
     
    selling vcc, Dec 30, 2007 IP
  7. vikingbear_ds

    vikingbear_ds Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi,
    I tried Azizny's solution, and the page failed to display anything from theat uploaded line of code onwards..

    Here is teh link to the page without restricting the FIELD
    http://www.rdvaustralie.com/nsw-tours.php

    Here is a link to a page that have one line of AZIZNY's code inserted
    http://www.rdvaustralie.com/nsw-tours-1.php

    Happy New Year to all..

    Regards, Ken
     
    vikingbear_ds, Dec 30, 2007 IP
  8. kendo1979

    kendo1979 Peon

    Messages:
    208
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    strange, Azizny should work. could you paste your code, with the Azizny solution?
     
    kendo1979, Dec 30, 2007 IP