我想知道如何缩短从mysql数据库生成的文本。我尝试按照我在互联网上找到的一些教程但是失败了。
这是我的代码,
<?php
$dbh=mysql_connect("localhost","root","root") or die ('Cannot connedt to the Database' .mysql_errno());
mysql_select_db("myinfo_db");
$res_query = mysql_query("SELECT A.cat_id as cat_id, count(A.cat_id) as cnt, B.category as category FROM listings A, category B WHERE A.cat_id=B.cat_id GROUP BY A.cat_id");
while ($category = mysql_fetch_assoc($res_query) )
{
echo '<a href="page.php?cat_id='.$category['cat_id'].'">'.$category['category'].' ('.$category['cnt'].')</a><br />';
}
?>
答案 0 :(得分:0)
$res_query = mysql_query("SELECT A.cat_id as cat_id, count(A.cat_id) as cnt, B.category as category FROM listings A, category B WHERE A.cat_id=B.cat_id GROUP BY A.cat_id");
到
$res_query = mysql_query("SELECT A.cat_id as cat_id, count(A.cat_id) as cnt, B.category as category FROM listings A LEFT OUTER JOIN category B ON A.cat_id=B.cat_id GROUP BY A.cat_id");
显示你的comit限制
function limitContent($content,$number) {
if(strlen($content)<=$number) {
$contents=$content;
} else {
$contents=substr($content,0,$number) . '...';
}
echo $contents;
}
PHP
limitContent($category['category'],'10');
答案 1 :(得分:0)
如果我正确理解你的问题,你可以使用MySQL子串函数:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html
SELECT CONCAT(SUBSTRING(sometext,1,10), '...')
FROM sometable;
其中1是起始位置,10是字符数。
这样你就可以直接做到,而且你不需要php来做。 在你的情况下像:
SELECT A.cat_id as cat_id, count(A.cat_id) as cnt, CONCAT(SUBSTRING(B.category,1,10), '...') as category FROM listings A, category B WHERE A.cat_id=B.cat_id GROUP BY A.cat_id
答案 2 :(得分:0)
我有a little class在单词边界上剪切长文本。因此,在缩短Your ass...
时,您不会得到类似Your assignment
的内容。
示例:
$f = new EllipsisFilter(15);
echo $f->filter('Lorem ipsum dolor sit amet'); // 'Lorem ipsum…'
随意使用它。