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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.