Jump to content

[SOLVED] Use a certain amount of words on a sql query


johntp

Recommended Posts

Hey guys,

 

I have a query that shows a row in the table but i have it filtered to show only the first 10 words of one column. This works fine however I am trying to make it do a ... IF it had to cut off, but im lost on how to do so. Below is the code i use:

 

                                <?php
$query2 = "SELECT * FROM news WHERE Viewable = 'Yes' AND Section = '$department' ORDER BY RECNO DESC";
$result2 = mysql_query($query2);
while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC))
{
$body = $row2['News'];
$length = 200; // The number of words you want
$text = $body;
$words = explode(' ', $text); // Creates an array of words
$words = array_slice($words, 0, $length);
$text = implode(' ', $words);
echo "$text";
?>

 

Any help would be appreciated.

Link to comment
Share on other sites

Here is an easier way using substr().

 

<?php
$query2 = "SELECT * FROM news WHERE Viewable = 'Yes' AND Section = '$department' ORDER BY RECNO DESC";
$result2 = mysql_query($query2);
while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC))
{
$body = $row2['News'];
$length = 200; // The number of words you want
$text = $body;

$text = substr($text, 0,$length)."...";
echo $text;
?>

Link to comment
Share on other sites

Thanks for the clean up code, it looks alot better less lines less hassle :) , but I still cannot get it to do what I want.

I realize that it wasnt that clear in my original post.

 

What i need is it to show the ... if it's more than 200 words, and not show the ... if it's less then 200 words.

Link to comment
Share on other sites

<?php
$query2 = "SELECT * FROM news WHERE Viewable = 'Yes' AND Section = '$department' ORDER BY RECNO DESC";
$result2 = mysql_query($query2);
while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC))
{
  $body = $row2['News'];
  $length = 200; // The number of words you want
  if (strlen($body) > $length) { echo substr($body, 0, $length)."..."; } 
  else { echo $text; }
}
?>

 

That what you mean?

Link to comment
Share on other sites

Perfect. I had to change it a little because i have a table that i echo no matter what the result is, so here it is incase somone else has this problem

 

                                <?php
$query2 = "SELECT * FROM news WHERE Viewable = 'Yes' AND Section = '$department' ORDER BY RECNO DESC";
$result2 = mysql_query($query2);
while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC))
{
$body = $row2['News'];
$length = 200; // The number of words you want
$text = $body;
$text = substr($text, 0,$length)."...";
if (strlen($body) > $length) { $news = $text; } 
  else { $news = $body; }

echo "<table style='border-bottom: solid 1px #CCCCCC' width='100%' border='0' cellspacing='0' cellpadding='0'>
                                      <tr>
                                        <td>
                                          <table width='100%' border='0' cellspacing='0' cellpadding='0'>
                                            <tr>
                                              <td width='10%'><span class='g'><strong>$row2[Date]</strong></span></td>
                                              <td width='80%'><div align='center'><span class='style24'><strong>$row2[Title]</strong></span></div></td>
                                              <td width='10%'><div align='right'><span class='g'><strong>$row2[section]</strong></span></div></td>
                                            </tr>
                                          </table>
                                          <table width='100%' border='0' cellspacing='0' cellpadding='0'>
                                            <tr>
                                              <br><td style='padding-left:30px' colspan='2'><span class='style9'> <span class='g style4 style6'><span class='style4'>$news</span></span></span></td>
                                            </tr>
                                          </table><br>
                                        </td>
                                      </tr>
                                    </table>";
						}
?>

 

Thanks for everyone's help, I do appreciate it.

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.