Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2016
    Posts
    14

    Unanswered: limit to 20 characters and enable HTML entities help

    Hi all, I'm having some slight difficulty with my code. Wonder if someone can help.

    Following script runs fine, except not sure how to do two things. I want to incorporate 'Message' to only display the first 20 characters and also enable HTML entities (paragraph spacing, special characters). I'm quite new to this. Your help would be most appriciated.

    PHP Code:
        <?php
    $sql 
    "
    SELECT feed.id, feed.heading, feed.message, feed.status, feed.by, feed.to, feed.date, users.username
    FROM feed
    LEFT JOIN users ON feed.by = users.id
    ORDER BY id DESC LIMIT 8
    "
    ;

    $results $con->query($sql);

    if(
    $results->num_rows) {
        While(
    $row $results->fetch_object()) {
            echo 
    "
                            <div class='post-message'>
                                
    {$row->message}
                            </div>

            
    "
    ;
        }
    } else {
        echo 
    'No Results';
    }
    ?>

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    so if you want to use the PHP function HTMLENTITIES then you call it BEFORE saving the data inside the db, or wherever the data is going to reside. when you need to consume the data remember to unpack / unencode encoded data witht he PHJP function HTML_ENTITY_DECODE as to

    how you display ONLY the first 20 characters in a column called message thats up to you.
    you could truncate the string in the DB or in PHP
    ..inside PHP use the SUBSTR function
    ..inside the DB you could use the same named function SUBSTR

    But understand that although the function are named the same and seemingly work the same one is a PHP fucntion (IE only works inside a PHP script, and the other is a MySQL function and only works inside a query

    inside a query
    Code:
    select my, column, list, substr(mycolumn,1,20) as myshortenedcolumn from mytable
    inside PHP
    PHP Code:
    myvar substr($row->message,1,20); 
    if message contains embedded HTML tags then you will need to close off those tags to display message properly
    HTML_ENTITY_DECODE before running the substr that may also adjust the number of characters actually displayed

    eg say you column message has '[b]this is emboldened text[ / b ]'
    then running substr($row->message,1,20); woudl return '[b]this is emboldened te', but depending on the browser that was displayed that may dispaly as 'this is emboldened te' OR '[b]this is emboldened te' or soemthing else
    I'd rather be riding on the Tiger 800 or the Norton

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •