Cliftron Posted September 27, 2009 Share Posted September 27, 2009 How do you limit the words on a sql query?? $sql = "SELECT *, DATE_FORMAT(Date,'%e/%m/%y') as Date FROM news ORDER BY id DESC LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); while ($list = mysql_fetch_array($result)) { echo '<div class="left_box">'; echo '<div class="left_title"><span class="textheader">'; echo $list['Title']; echo '</span> - <span class="textauthor">'; echo $list['Author']; echo ' '; echo $list['Date']; echo ' </span></div> <div class="left_img"><img src="images/img.jpg" /> </div> </div> <div class="article"> <p>'; echo $list['Article']; echo '</p> I want to limit the article to something like 300 characters? Because I'm gonna add a link to read more. To display the whole article. i got that covered thou Link to comment https://forums.phpfreaks.com/topic/175660-solved-word-limit-on-sql-results/ Share on other sites More sharing options...
Alex Posted September 27, 2009 Share Posted September 27, 2009 SELECT ... LEFT(Article, 300) ... Link to comment https://forums.phpfreaks.com/topic/175660-solved-word-limit-on-sql-results/#findComment-925603 Share on other sites More sharing options...
Cliftron Posted September 27, 2009 Author Share Posted September 27, 2009 $sql = "SELECT *, LEFT(Article, 300), DATE_FORMAT(Date,'%e/%m/%y') as Date FROM news ORDER BY id DESC LIMIT $offset, $rowsperpage"; is that it? it dont work. Link to comment https://forums.phpfreaks.com/topic/175660-solved-word-limit-on-sql-results/#findComment-925611 Share on other sites More sharing options...
Alex Posted September 27, 2009 Share Posted September 27, 2009 $sql = "SELECT Author, Title, LEFT(Article, 300) as Article, DATE_FORMAT(Date,'%e/%m/%y') as Date FROM news ORDER BY id DESC LIMIT $offset, $rowsperpage"; Edit: Whoops, to make it easier just use: LEFT(Article, 300) as Article. That way you don't have to edit the following code. Link to comment https://forums.phpfreaks.com/topic/175660-solved-word-limit-on-sql-results/#findComment-925612 Share on other sites More sharing options...
Cliftron Posted September 27, 2009 Author Share Posted September 27, 2009 now none of the article is displayed. Link to comment https://forums.phpfreaks.com/topic/175660-solved-word-limit-on-sql-results/#findComment-925616 Share on other sites More sharing options...
redarrow Posted September 27, 2009 Share Posted September 27, 2009 That stupid really, What about you want to use, the same select, for showing, all info somewhere else, need new select wast off time. Php function substr http://uk3.php.net/substr Link to comment https://forums.phpfreaks.com/topic/175660-solved-word-limit-on-sql-results/#findComment-925619 Share on other sites More sharing options...
Alex Posted September 27, 2009 Share Posted September 27, 2009 That stupid rely, what about you want to use the same select for showing all somewhere else, need new select wast off time. php function substr http://uk3.php.net/substr Yea, he can just do that: echo substr($list['Article'], 0, 300); But it's usually a better idea to just let the query perform as much work as possible. And I really don't see why it isn't working. Link to comment https://forums.phpfreaks.com/topic/175660-solved-word-limit-on-sql-results/#findComment-925622 Share on other sites More sharing options...
redarrow Posted September 27, 2009 Share Posted September 27, 2009 can use the mysql SUBSTR aswell http://www.1keydata.com/sql/sql-substring.html what about , would it work. $sql = "SELECT *, DATE_FORMAT(Date,'%e/%m/%y') as Date FROM news ORDER BY id DESC LIMIT $offset, $rowsperpage WHERE news.article=SUBSTR(news.article,0,300)"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); Link to comment https://forums.phpfreaks.com/topic/175660-solved-word-limit-on-sql-results/#findComment-925625 Share on other sites More sharing options...
Cliftron Posted September 27, 2009 Author Share Posted September 27, 2009 okay thanks heaps echo substr($list['Article'], 0, 300); worked a treat Link to comment https://forums.phpfreaks.com/topic/175660-solved-word-limit-on-sql-results/#findComment-925630 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.