2012-12-19 46 views
0

我觉得我有部分工作,但由于某些原因,它没有得到文章的内容..STRL缩短字符

<?php 

     $amount_get = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "'"); 
     $comments = mysql_num_rows($amount_get);    

     $grab = mysql_query("SELECT * FROM articles WHERE id='" . mysql_real_escape_string($_GET['id']) . "' LIMIT 1");  
     $grab = mysql_query("SELECT id, news_title, news_content, news_author, news_day, news_month, news_year, news_date FROM articles ORDER BY id DESC limit 3"); 
     $news_day =$row['news_day']; 
     $news_month =$row['news_month']; 
     $news_year =$row['news_year']; 

     if (mysql_num_rows($grab)==0) { 
     echo "<div class='alert alert-note-x'>Sorry, it looks like their are no articles posted!</div>"; 
     } 

     else if(strlen($row['news_contents']) > 135) { 
        $news_content = substr($news_content, 0, 135) . "...."; 
     } 

     while($row = mysql_fetch_array($grab)){ 

    ?> 

    <div class="post-container"> 
     <div class="post"> 
     <div class="left"> 
     <div class="date"><span class="day"><?php echo $row['news_day'] ?></span> <span class="month"><?php echo $row['news_month'] ?></span> <span class="year"><?php echo $row['news_year'] ?></span></div></div> 
     <div class="postcontent"><h5 class="posttitle"><a href="#/media/<?php echo $row['id'] ?>.<?php echo stripslashes(str_replace(" ", "-", $row['news_title'])); ?>"><?php echo stripslashes($row['news_title']); ?></a></h5>      

       <?php echo (strlen($row['news_content']) > 135); ?> 

      </div> 
    </div>​ 
    </div> 

    <?php } ?> 

这是什么样子:http://puu.sh/1CD32

你们是否知道为什么它显示的是“1”而不是数据库中的内容?

<?php echo (strlen($row['news_content']) > 135); ?> 
+0

U可以使用三元运算符或语句中 –

回答

1

你问它,如果变量$ row ['news_content']大于135个字符。它将打印1为真,0打印为假。

如果你要打印的变量的第135个字符您正在寻找SUBSTR不是strlen的

echo substr($row['news_content'], 0, 135); 
+0

啊..。那是SUBSTR ..我需要学习这个shizzle ..非常感谢,我得到了它的工作。 – TrippedStackers

3

您正在要求它打印表达式的结果“(strlen的($行[‘news_content’])> 135)”,这将是1(如在真)如果字符串长于135字符和0(如虚假),如果不是。

0

正因为如此:

<?php echo (strlen($row['news_content']) > 135); ?> 

此函数返回一个true或false

呼应真实结果在1

和假只会是空