0

重複
の可能性: PHP 配列を切り捨てる方法

MySQL テーブルのテキストを表示する配列があります。データを一定の長さ(未定)に切り捨て、切り捨ての最後に「... Read more」を追加したい。

関連するコードブロックは次のとおりです。

while($rows = mysql_fetch_array($getNews)){
                  $id = $rows['id'];
                  $title = strip_tags(stripslashes($rows['title']));
                  $editor1 = $show->news_article = stripslashes(html_entity_decode($rows['editor1']));
                  $poster = $rows['poster'];
                  $date = $rows['date'];

(スクリプトは機能し、データを表示します。スクリプト全体を次に示します:)

<?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 ('<strong>$name ($ip)</strong> has posted a comment<strong>$title</strong>', '$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->news_article = stripslashes(html_entity_decode($newsRows['editor1']));
    $poster = $newsRows['poster'];
    $date = $newsRows['date'];

    $headColour = getColour($colour);

    echo("<h1>{$title}</h1>
        $editor1
        <i>Posted by $poster on $date</i><br>
    <hr>");


    // Get the amount of comments
    $numrows = mysql_num_rows($getCom);
    // Echo out the comments
    if(!$numrows){
        // There are no comments
        echo("<i>There are no comments for this article</i>");
    } else {
        // Show the comments
        echo("<i>There are $numrows comment(s) for this article</i><br><br>");
        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 <br>Posted by $name on $timestamp <br> <br><hr>");
        }

    }

    // Show the form to submit a comment
    echo("<form method=\"post\">
        Comment: <textarea name=\"comment\" id=\"comment\" cols=\"45\" rows=\"5\"></textarea><br>
        Name:<input type=\"text\" name=\"name\"><br>
        <input type=\"hidden\" name=\"id\" value=\"{$id}\">
        <input type=\"submit\" name=\"submit\" value=\"Post comment!\"><br>
          </form>");
} 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("<br /><br /><br /><div class=\"warning\"><center><b>There are currently no news articles. Please check back later!</center></b></div>");
      } else {
        // Otherwise show the articles
          while($rows = mysql_fetch_array($getNews)){
                  $id = $rows['id'];
                  $title = strip_tags(stripslashes($rows['title']));
                  $editor1 = $show->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("<h1>{$title}</h1>
        $editor1
        <a href=\"index.php?id=$id\">$commentR comment(s)</a> <i>Posted by $poster on $date</i><hr>");
    $colour ++;
        // If the colour count is 5 (we only have four colours), reset it back to 1
        if($colour == 5){
            $colour = 1;
        }
          } 

          }
    }
}
  ?>

事前に助けてくれてありがとう!ダニエル・ミネット

4

0 に答える 0