1

What are the different ways to separate an echo of single long text value MySQL table?

As I already found here, I can query the table by rows and limit them, like that:

<?php
// Perform query.
$sql = "SELECT * FROM table limit 4";
$result = $conn->query($sql);

// Fetch results
while (true) {
    $row = $result->fetch_assoc();
    if (!$row) {
        break;
    }

    $rows[] = $row;
}

But it is not fetching me as a solution because I have a single table variable as text, and it is big ( ~100 words). So I cannot delimit it or query by raws since it is a single table variable.

I am looking for a solution since I got this JavaScript onclick function read more/read less (button at bottom of the article)

<script>
function readPost() {
  var dots = document.getElementById("dots");
  var moreText = document.getElementById("more");
  var btnText = document.getElementById("myBtn");

  if (dots.style.display === "none") {
    dots.style.display = "inline";
    btnText.innerHTML = "Read more"; 
    moreText.style.display = "none";
  } else {
    dots.style.display = "none";
    btnText.innerHTML = "Read less"; 
    moreText.style.display = "inline";
  }
}
</script>

Here i added per request the simple blog html/php part ,hope it helps.

<?php 
    $query_articles = mysqli_query($link, "SELECT title,pubdate,text FROM `articles`");
    $fetch_post = mysqli_fetch_assoc($query_articles);
?>
<div class="card mb-4">
  <div class="card-body">
    <h2 class="card-title">
    <?php
      echo $fetch_post['title'];
    ?>
    </h2>
    <p class="card-text">
    <?php
      echo $fetch_post['text'];
    ?>
    </p>
    <a href="#" class="btn btn-primary" id="read_button" onclick="readMore()">Read More</a>
  </div>
  <div class="card-footer text-muted">
    <?php
     echo $fetch_post['pubdate'];
    ?>
  </div>
</div>
Wise Crow
  • 43
  • 4
  • 1
    Welcome to StackOverflow. Could you please post example of the data/payload? Its hard from this context to answer your question without additional info. – Cninroh Sep 28 '20 at 14:37
  • Don't use infinite loop. It's a bad practice. `while ($row = $result->fetch_assoc())` – Jsowa Sep 28 '20 at 14:38
  • Also, if you are going to use a loop it would be easier with `foreach($result as $row)` but in your case, there is absolutely no reason for a loop there. You can simply do `$rows = $result->fetch_all(MYSQLI_ASSOC)` – Dharman Sep 28 '20 at 14:40
  • Are you trying to take first N characters and place "read more" button or something similar ? – pavelbere Sep 28 '20 at 14:40
  • Hello Cninroh, you want to see the content from the text value from mysql table ? (it is just a text value with words). The index.php is just some divs with a procedural php mysql query with echos with mysql_fetch_assoc() that is making echo the title ,then echo the post text and then the post creation date. – Wise Crow Sep 28 '20 at 14:45
  • What do you want do? Do you need to show half of the article and show the rest when some one clicking on the Read More button?. Please update your question with proper questions/needs. – Duke Sep 28 '20 at 14:54
  • @RintoGeorge George,yes i actualy just looking the way to echo that long text value and separate it with divs to have a possibility to use that js function. I don t know if it does make sense – Wise Crow Sep 28 '20 at 15:01
  • Guys i don t know if it technicaly possible in PHP ,as i sayed i am looking for basic and simple solution to integrate that read more/ read less button.. – Wise Crow Sep 28 '20 at 15:03
  • i added the html/php part to the question ,i hope it may simplify my question... – Wise Crow Sep 28 '20 at 15:09
  • One simple solution would be use ```strip_tags()``` to remove all the html and use a php function to show certain number of characters . Read this for better idea https://stackoverflow.com/questions/1233290/making-sure-php-substr-finishes-on-a-word-not-a-character – Duke Sep 28 '20 at 15:12
  • @RintoGeorge thank you George ,will read and check that strip_tags() function and hope will make something of it. – Wise Crow Sep 28 '20 at 15:15

0 Answers0