Jump to content

Count Number Of Words In Output And Display "..." Link If To Many Words


elite311

Recommended Posts

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

Link to comment
Share on other sites

$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 . '';

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.