2013-03-14 109 views
0

我不得不訴諸張貼在這裏,完全失去了這一個!substr在一個while循環PHP SQL

我有一個數組,我可以成功打印,因爲它應該,但我需要限制如何可能打印字符。我知道這可以用substr完成,但我似乎無法將substr放到我需要限制的值上。

我明白在一行中使用$ variable = substr,但我無法實現我正在使用的數組,並且我搜索了高和低的任何文檔,可能會澄清我的問題,但可惜無濟於事。

繼承人的代碼,在這種情況下它的數組$ info和值.info ['post_content']。

mysql_connect("localhost", "xxxxxxxx", "xxxxxxx") or die(mysql_error()); 
mysql_select_db("xxxxxxxxx") or die(mysql_error()); 
$data = mysql_query("SELECT * FROM posts WHERE hushfeed_showon = 1") 
or die(mysql_error()); 
    } 

while($info = mysql_fetch_array($data)) 
{ 


echo "<div> 
<ul class='blocks-thumbs thumbs-rollover'> 
    <li> 
     <a href='single.html' class='thumb' title='An image'><img src='img/dummies/282x150.gif' alt='Post' /></a> 
     <div class='excerpt'> 
      <a href='dnb/a_Post.php?postid=".$info['postid']."' class='header'>".$info['post_title'] ."</a> 
      <p class='para1'>" .$info['post_content']."</p> 

     </div> 
     <a href='single.html' class='link-button'><span>Read more &#8594;</span></a> 
    </li> 

</ul> 
</div>"; 



} 

林掙扎,因爲我無法找到有關回聲在使用期間的任何信息。「$」。

如果有誰能夠闡明其中i應側重於處理一些輕這個SUBSTR這將是更不勝感激!

謝謝!

斯圖

+0

'SUBSTR($信息[ 'POST_CONTENT'],0,number_of_chars_you_want_printed)'? – 2013-03-14 22:44:32

+0

「我正在努力掙扎,因爲我無法找到有關在回聲期間使用週期的任何信息。」 「'」 - 這是[字符串連接](http://de2.php.net/manual/en/language.operators.string.php) – 2013-03-14 22:46:27

+0

您是否嘗試過查看PHP文檔?有很多關於如何在那裏使用'substr'的​​例子。 – 2013-03-14 22:48:39

回答

0
<?php 
echo "[...]<p class='para1'>" . 
     (strlen($info['post_content'])>200? 
      substr($info['post_content'],0,200)."...": 
      $info['post_content']). 
     "</p>[...]"; 

和容易的版本:

<?php 
echo "[...]<p class='para1'>" . substr($info['post_content'],0,200) . "</p>[...]"; 
+0

乾杯約翰!作品很棒,我確切地知道我現在要去哪裏錯了,非常感謝! – Derple 2013-03-14 23:08:06