1
votes

I'm making a news block for a website. I want if a news message has more than 64 words it shows a read more button. And if it has less then 64 words that just the message is shown. Now I already have the part with the read more button, but i can't get the part that if it has less then 64 words that the read more button dissapears. So the read more button is visible whether it has less or more than 64 words.

This is the piece of code

<?php
$query="SELECT * FROM seo_nieuws ORDER BY id DESC LIMIT 1";
$stmt = $pdo->query($query);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($result as $row) {
?>
<h4><?php echo $row['titel']; ?></h4> 

<?php
}
?>  
<?php
function limit_words($string, $word_limit){
$words = explode(" ",$string);
return implode(" ",array_splice($words,0,$word_limit));
}
$start = limit_words($row['bericht'],64);                       
?>

<div class="nieuws-tekst"><?php echo $start; ?><?= '...' ?></div>
    <a class="button" href="http://www.linkofwebsite.nl/laatste-nieuws/nieuwsbericht.php?id=<?php echo $row['id']; ?>">Lees meer</a>

Update:

<?php
function limit_words($string, $word_limit){
$limitReached = FALSE;
$words = explode(" ",$string);
$content = implode(" ",array_splice($words,0,$word_limit));
if (count($words) >= $word_limit) {
$limitReached = TRUE;
}

return array($limitReached, $content);
}
list($limitReached, $content) = limit_words($row['bericht'],64);
?>

<div class="nieuws-tekst">
<?= $content; ?>
<?php if ($limitReached): ?>
... <a class="button" href="http://www.linkofwebsite.nl/laatste-nieuws/nieuwsbericht.php?id=<?= $row['id']; ?>">Lees meer</a>
<?php endif ?>
</div>

This is what I got now after a reaction. But the content shows but the read more button does not.

Output after var_dump($row['bericht]);

string(817) "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. xercitation ullamco laboris nisi ut aliquip ex ea "

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt

4
count the number of words using str_word_count() function and use the conditional statements.bhansa

4 Answers

1
votes

To make this dynamic, change your function to something like this :

function limit_words($string, $word_limit){
    $limitReached = FALSE;
    $words = explode(" ",$string);
    $content = implode(" ",array_splice($words,0,$word_limit));
    if (count($words) >= $word_limit) {
        $limitReached = TRUE;
    }

    return array($limitReached, $content);
}

Change your calling part to

list($limitReached, $content) = limit_words($row['bericht'],64);

and then check for it:

<div class="nieuws-tekst">
    <?= $content;
  if ($limitReached) { ?>
       <a class="button" href="http://www.linkofwebsite.nl/laatste-nieuws/nieuwsbericht.php?id=<?= $row['id']; ?>">Lees meer</a>
    <?php } ?>
</div>
1
votes

as jitendrapurohit already said, you can update your function to count the words.

if $string is html you have to make sure your functions handles that properly. otherwise you might end up with invalid html.

for example

$string = '<div>some long text</div>';

then when you truncate it after 64 words the div will not be closed.

here's an updated version of @jitendrapurohit's function which will also remove html from the string.

<?php

function limit_words($string, $word_limit)
{
    $limitReached = false;
    $words = explode(' ', $string);
    if (count($words) >= $word_limit) {
        $limitReached = true;
    }

    $content = implode(' ', array_splice($words, 0, $word_limit));

    return [$limitReached, $content];
}

list($limitReached, $content) = limit_words($row['bericht'], 64);

?>

<div class="nieuws-tekst">
    <?= $content; ?>
    <?php if ($limitReached): ?>
        <a class="button" href="http://www.linkofwebsite.nl/laatste-nieuws/nieuwsbericht.php?id=<?= $row['id']; ?>">
            Lees meer
        </a>
    <?php endif ?>
</div>

update:
array_slice changes its input. so after calling $content = implode(" ",array_splice($words,0,$word_limit)); $words contains all words except the first 64. i updated the function.

0
votes

just put your button code in an if

<?php
    if (limit_words > 64) {
        echo "<a class="button" href="http://www.linkofwebsite.nl/laatste-nieuws/nieuwsbericht.php?id=<?php echo $row['id']; ?>">Lees meer</a>"}
?>
0
votes
<?php
   $string= 'this is a good choice';
   if(str_word_count($string)>64){
 ?>
 <a href="#"><?php echo substr($string,0,63)?> Read More</a>
 <?php
   }else{
      echo $string;
   }
?>

Use str_word_count () function for count words. And use substr() function to show just 64 words.