Jump to content

[SOLVED] Word limit on sql results.


Cliftron

Recommended Posts

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

$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.

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.