Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP How to truncate array?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/7985416/php-how-to-truncate-an-array">PHP how to truncate an array</a> </p> </blockquote> <p>I have an array which displays text from a MySQL table. I want to truncate the data to a certain length (undecided) and add "... Read more" at the end of the truncation. </p> <p>Here is the code block that is relevant:</p> <pre><code>while($rows = mysql_fetch_array($getNews)){ $id = $rows['id']; $title = strip_tags(stripslashes($rows['title'])); $editor1 = $show-&gt;news_article = stripslashes(html_entity_decode($rows['editor1'])); $poster = $rows['poster']; $date = $rows['date']; </code></pre> <p>(The script works and does display the data, here is the entire script:)</p> <pre><code>&lt;?php function getColour($number){ switch ($number) { } return $colour; } // For the header $colour = 1; if($_POST['submit']){ // Clean the submitted form $comment = cleanTag($_POST['comment']); $name = cleanTag($_POST['name']); $tstamp = time(); $ip = getenv("REMOTE_ADDR"); $id = cleanTag($_POST['id']); if(!$id){ die("Oops: Unexpected error!"); } if($name == "" || $comment == ""){ die("You cannot leave any fields blank"); } // Insert the comment mysql_query("INSERT INTO comments_articles (comment, name, article, tstamp, ip) VALUES ('{$comment}', '{$name}', '{$id}', '{$tstamp}', '{$ip}')") or die(mysql_error()); // Tell them the form was submitted echo("Your comment has been posted!"); # Insert action log $date = date("d/m/y - h:ia"); $insertLog = "INSERT INTO `logs` ( `log` , `date` ) VALUES ('&lt;strong&gt;$name ($ip)&lt;/strong&gt; has posted a comment&lt;strong&gt;$title&lt;/strong&gt;', '$date')"; mysql_query($insertLog) or die('MySQL Error.'); } else if($_GET['id']){ // Get the id and clean it $id = cleanTag($_GET['id']); // Get the news and the comments $getNews = mysql_query("SELECT * FROM news WHERE category = 1 AND approved = 1 AND id = {$id} ORDER BY id DESC") or die(mysql_error()); $newsRows = mysql_fetch_array($getNews); $getCom = mysql_query("SELECT * FROM comments_articles WHERE article = {$id} ORDER BY tstamp DESC") or die(mysql_error()); // Show the news article again $id = $newsRows['id']; $title = strip_tags(stripslashes($newsRows['title'])); $editor1 = $show-&gt;news_article = stripslashes(html_entity_decode($newsRows['editor1'])); $poster = $newsRows['poster']; $date = $newsRows['date']; $headColour = getColour($colour); echo("&lt;h1&gt;{$title}&lt;/h1&gt; $editor1 &lt;i&gt;Posted by $poster on $date&lt;/i&gt;&lt;br&gt; &lt;hr&gt;"); // Get the amount of comments $numrows = mysql_num_rows($getCom); // Echo out the comments if(!$numrows){ // There are no comments echo("&lt;i&gt;There are no comments for this article&lt;/i&gt;"); } else { // Show the comments echo("&lt;i&gt;There are $numrows comment(s) for this article&lt;/i&gt;&lt;br&gt;&lt;br&gt;"); while($rows = mysql_fetch_array($getCom)){ $comment = strip_tags($rows['comment']); $name = $rows['name']; $article = $rows['article']; $ip = $rows['ip']; $timestamp = date("m/d/y - h:ia", $rows['tstamp']); echo("$comment &lt;br&gt;Posted by $name on $timestamp &lt;br&gt; &lt;br&gt;&lt;hr&gt;"); } } // Show the form to submit a comment echo("&lt;form method=\"post\"&gt; Comment: &lt;textarea name=\"comment\" id=\"comment\" cols=\"45\" rows=\"5\"&gt;&lt;/textarea&gt;&lt;br&gt; Name:&lt;input type=\"text\" name=\"name\"&gt;&lt;br&gt; &lt;input type=\"hidden\" name=\"id\" value=\"{$id}\"&gt; &lt;input type=\"submit\" name=\"submit\" value=\"Post comment!\"&gt;&lt;br&gt; &lt;/form&gt;"); } else { // Show the main news articles $getNews = mysql_query("SELECT * FROM news WHERE category = 1 AND approved = 1 ORDER BY id DESC") or die(mysql_error()); // If there are no articles if(mysql_num_rows($getNews) == 0){ echo("&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=\"warning\"&gt;&lt;center&gt;&lt;b&gt;There are currently no news articles. Please check back later!&lt;/center&gt;&lt;/b&gt;&lt;/div&gt;"); } else { // Otherwise show the articles while($rows = mysql_fetch_array($getNews)){ $id = $rows['id']; $title = strip_tags(stripslashes($rows['title'])); $editor1 = $show-&gt;news_article = stripslashes(html_entity_decode($rows['editor1'])); $poster = $rows['poster']; $date = $rows['date']; { $headColour = getColour($colour); $commentQ = mysql_query("SELECT * FROM comments_articles WHERE article='{$id}'"); $commentR = mysql_num_rows($commentQ); echo("&lt;h1&gt;{$title}&lt;/h1&gt; $editor1 &lt;a href=\"index.php?id=$id\"&gt;$commentR comment(s)&lt;/a&gt; &lt;i&gt;Posted by $poster on $date&lt;/i&gt;&lt;hr&gt;"); $colour ++; // If the colour count is 5 (we only have four colours), reset it back to 1 if($colour == 5){ $colour = 1; } } } } } ?&gt; </code></pre> <p>Thanks for any help in advance! Daniel Minett</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload