elite311 Posted October 8, 2012 Share Posted October 8, 2012 Hello, I have been working on my own news system and have got almost everything working perfect including limiting the amount of words shown on the main page. Problem is I'm not sure and haven't been able to figure out how to show "..." at the end if the output has more words than 25 and if it has more than 25 words show the readmore link. What I'm doing right now is counting 25 spaces in the article column of the query to limit the output to 25 words and display it as article1. Not sure if this is the best way or not just seemed the most logical way to me, however I'm having trouble figuring out how to show the "..." at the end of the output and the readmore link if it's more than 25 spaces. I'm counting the words like this in my query: $MySQL = 'SELECT SQL_CALC_FOUND_ROWS SUBSTRING_INDEX(article," ",25) AS article1, id, date, postedby FROM news ORDER BY date DESC LIMIT ' . (($pagination->get_page() - 1) * $records_per_page) . ', ' . $records_per_page . ''; Then displaying it like this: <?php echo wordwrap($row['article1'], 46, "\n", true);?> I'm not sure if I have posted this in the right section but hoping someone could help me figure this out. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/269211-count-number-of-words-in-output-and-display-link-if-to-many-words/ Share on other sites More sharing options...
Barand Posted October 8, 2012 Share Posted October 8, 2012 $MySQL = 'SELECT SQL_CALC_FOUND_ROWS SUBSTRING_INDEX(article," ",25) AS article1, id, date, postedby, IF(SUBSTRING_INDEX(article," ",25) = article, "", "...") AS readmore FROM news ORDER BY date DESC LIMIT ' . (($pagination->get_page() - 1) * $records_per_page) . ', ' . $records_per_page . ''; Quote Link to comment https://forums.phpfreaks.com/topic/269211-count-number-of-words-in-output-and-display-link-if-to-many-words/#findComment-1383644 Share on other sites More sharing options...
fenway Posted October 8, 2012 Share Posted October 8, 2012 Personally, I've never been a fan of SQL_CALC_FOUND_ROWS; it's so rare that (a) you need an exact count and (B) users need to be able to get to the last page. Quote Link to comment https://forums.phpfreaks.com/topic/269211-count-number-of-words-in-output-and-display-link-if-to-many-words/#findComment-1383723 Share on other sites More sharing options...
ManiacDan Posted October 8, 2012 Share Posted October 8, 2012 SQL_CALC_FOUND_ROWS can also seriously slow down the query, since the entire data set needs to be parsed even if you're just looking for the first 10. Though this query is ordered, so it shouldn't have any effect on processing time. Quote Link to comment https://forums.phpfreaks.com/topic/269211-count-number-of-words-in-output-and-display-link-if-to-many-words/#findComment-1383726 Share on other sites More sharing options...
elite311 Posted October 8, 2012 Author Share Posted October 8, 2012 Barand - Thanks worked great! now I just need to figure out how to show the link button when it's greater than 25. Thanks guys, I suppose I could just use a COUNT instead of the SQL_CALC_FOUND_ROWS but I was thinking the table would never really get that large it would become an issue. Quote Link to comment https://forums.phpfreaks.com/topic/269211-count-number-of-words-in-output-and-display-link-if-to-many-words/#findComment-1383728 Share on other sites More sharing options...
ManiacDan Posted October 8, 2012 Share Posted October 8, 2012 You can't use COUNT() along with LIMIT, you have to use SQL_CALC_FOUND_ROWS, that's what it's for. Quote Link to comment https://forums.phpfreaks.com/topic/269211-count-number-of-words-in-output-and-display-link-if-to-many-words/#findComment-1383734 Share on other sites More sharing options...
elite311 Posted October 8, 2012 Author Share Posted October 8, 2012 So the sql query Barand posted di just what I wanted and displays the "..." after the text but I'm still stuck on how to show a "more" button if there is more words. My current code looks like this: <p class="txt-2"><?php echo wordwrap($row['article1'], 46, "\n", true);?><a href="#" class="news-readmore-link"><?php echo $row['readmore']; ?></a></p> <div class="p14"><a href="#" class="btn-1">more <span></span></a></div> What I would like to do is only show the more button if there is more then 25 words like the "..." I'm not sure how to do this using php. Quote Link to comment https://forums.phpfreaks.com/topic/269211-count-number-of-words-in-output-and-display-link-if-to-many-words/#findComment-1383837 Share on other sites More sharing options...
Barand Posted October 8, 2012 Share Posted October 8, 2012 if there is more text than the 25 words then "readmore" will contain '...' if ($row['readmore'] == '...') { // show link to more } Quote Link to comment https://forums.phpfreaks.com/topic/269211-count-number-of-words-in-output-and-display-link-if-to-many-words/#findComment-1383838 Share on other sites More sharing options...
elite311 Posted October 8, 2012 Author Share Posted October 8, 2012 Thank you very much! worked perfect! Quote Link to comment https://forums.phpfreaks.com/topic/269211-count-number-of-words-in-output-and-display-link-if-to-many-words/#findComment-1383844 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.