phpocean Posted June 9, 2009 Share Posted June 9, 2009 Hi, What I want: How can I limit the number of characters shown from mysql database? I wanted it to incorporate it into my current query: $query = "SELECT * FROM home ORDER BY id DESC LIMIT 5"; Say I called for $row[title]; and wanted not all the title characters to show but only about 15 characters. Much appreciated. Edited: by the way, is white space count as a character? Ocean Quote Link to comment https://forums.phpfreaks.com/topic/161575-solved-limit-number-of-characters-shown-from-mysql/ Share on other sites More sharing options...
Maq Posted June 9, 2009 Share Posted June 9, 2009 substr. Quote Link to comment https://forums.phpfreaks.com/topic/161575-solved-limit-number-of-characters-shown-from-mysql/#findComment-852631 Share on other sites More sharing options...
ldougherty Posted June 9, 2009 Share Posted June 9, 2009 $title = substr('$row[title]',0,15); the first letter of the string is 0 and yes white spaces do count Quote Link to comment https://forums.phpfreaks.com/topic/161575-solved-limit-number-of-characters-shown-from-mysql/#findComment-852635 Share on other sites More sharing options...
phpocean Posted June 9, 2009 Author Share Posted June 9, 2009 $title = substr('$row[title]',0,15); the first letter of the string is 0 and yes white spaces do count Can I do this: <?php // while there are rows to be fetched... while ($title = mysql_fetch_assoc($result) substr('$row[title]',0,15)) { ?> Quote Link to comment https://forums.phpfreaks.com/topic/161575-solved-limit-number-of-characters-shown-from-mysql/#findComment-852640 Share on other sites More sharing options...
Maq Posted June 9, 2009 Share Posted June 9, 2009 I wanted it to incorporate it into my current query: If you want it in your query, MySQL also has a substring function - SUBSTRING(). You should post in the appropriate section so people will give you appropriate feedback. Quote Link to comment https://forums.phpfreaks.com/topic/161575-solved-limit-number-of-characters-shown-from-mysql/#findComment-852641 Share on other sites More sharing options...
Maq Posted June 9, 2009 Share Posted June 9, 2009 $title = substr('$row[title]',0,15); the first letter of the string is 0 and yes white spaces do count Can I do this: // while there are rows to be fetched... while ($list = mysql_fetch_assoc($result) substr('$row[title]',0,15)) { ?> Have you tried it? Quote Link to comment https://forums.phpfreaks.com/topic/161575-solved-limit-number-of-characters-shown-from-mysql/#findComment-852642 Share on other sites More sharing options...
phpocean Posted June 9, 2009 Author Share Posted June 9, 2009 I have but didn't work. Ok i will next time, post in right section. Ok i'm stuck, here is my complete query: <?php // Connect to database. // get the info from the db $sql = "SELECT * FROM home ORDER BY id DESC LIMIT 5"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); ?> <?php // while there are rows to be fetched... while ($list = mysql_fetch_assoc($result)) { ?> <tr> <td class="tal vam"><div style="margin:7px 0 5px 3px;"><a href="<?php print $list['link']; ?>" style="color:white">[<?php print $list['class']; ?>] <?php print $list['title']; ?></a></div></td> <td class="tar" style="color:white"><?php print $list['date']; ?></td> </tr> <tr style="height:1px;"><td colspan="2" style="background:white;"></td></tr> <?php } // end while ?> Quote Link to comment https://forums.phpfreaks.com/topic/161575-solved-limit-number-of-characters-shown-from-mysql/#findComment-852649 Share on other sites More sharing options...
Maq Posted June 9, 2009 Share Posted June 9, 2009 Try changing this part to: Quote Link to comment https://forums.phpfreaks.com/topic/161575-solved-limit-number-of-characters-shown-from-mysql/#findComment-852655 Share on other sites More sharing options...
phpocean Posted June 9, 2009 Author Share Posted June 9, 2009 Ok it work like charm. Just on the side, MySQL doesn't read < and > so I used < and > instead, well that kinda takes away 8 characters instead of 2, is there a way to solve this issue? Also I wanted it to add ... to the end when the character is at 15, something like this: 012345678912345... Maybe I am asking for too much but please bear with me a little while. Ocean Quote Link to comment https://forums.phpfreaks.com/topic/161575-solved-limit-number-of-characters-shown-from-mysql/#findComment-852677 Share on other sites More sharing options...
Maq Posted June 10, 2009 Share Posted June 10, 2009 You can decode the entities, although I'm not sure this is the best way. Quote Link to comment https://forums.phpfreaks.com/topic/161575-solved-limit-number-of-characters-shown-from-mysql/#findComment-852823 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.