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 Quote Link to comment Share on other sites More sharing options...
Alex Posted September 27, 2009 Share Posted September 27, 2009 SELECT ... LEFT(Article, 300) ... Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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); Quote Link to comment 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 Quote Link to comment 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.