0

I would like to know how to shorten text generated from the mysql database. I tried doing this by following a few tutorials I found on the internet but failed.

Here is my code,

<?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 />';
}

?>
Smar
  • 8,109
  • 3
  • 36
  • 48

3 Answers3

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");

to

 $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");

To show limit of your comit

function limitContent($content,$number) {
 if(strlen($content)<=$number) { 
   $contents=$content;  
 } else {
   $contents=substr($content,0,$number) . '...';
 }
  echo $contents;
}

PHP

limitContent($category['category'],'10');
Punitha Subramani
  • 1,467
  • 1
  • 8
  • 6
0

You can use the MySQL substring function if I understand your question correctly:

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html

SELECT CONCAT(SUBSTRING(sometext,1,10), '...')
FROM sometable;

Where 1 is the startposition and 10 the number of chars.

This way you can do it directly and you don't need php to do it. In your case something like:

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
randomizer
  • 1,619
  • 3
  • 15
  • 31
0

I have a little class cutting long text on a word boundary. So that you don't get something like Your ass... when shortening Your assignment.

Example:

$f = new EllipsisFilter(15);
echo $f->filter('Lorem ipsum dolor sit amet'); // 'Lorem ipsum…'

Feel free to use it.

Ivan Krechetov
  • 18,802
  • 8
  • 49
  • 60