Jump to content

Recommended Posts

Hi,

 

I can't figure out how to limit the word count fetched from the database.

 

I simply want to show 10 words from the 'body' column.

 

I'm using the below echo code to display the information.

 

  echo '<div class="menu"><div class="item">';
  echo '<h1><a href="news/display/'.$row['id'].'">'.$row['title'].'</a></h1>';
  echo '<img src="../'.$row['image'].'"/>';
  echo '<p>'.$row['body'].'</p>';
  echo '</div></div>';

 

Any help is appreciated.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/270565-php-word-count-limit/
Share on other sites

You can limit the result set that you get back from your database by using MySQL's LIMIT clause.

 

SELECT * FROM MyTable LIMIT 0, 10

 

Edit: I misunderstood the question; I thought each row had one word and didn't realize that all of the words are in the same column.

Edited by Andy123

I'd be inclined to limit it in the query itself, using SUBSTRING_INDEX().

 

Never used this before. How would that be implemented?

 

Here's my code. I'm guessing it would go within the mysql_query?

 


<?php
$con = mysql_connect("localhost","XXX","XXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("XXX", $con);

$result = mysql_query("SELECT * FROM `articles` WHERE `id` NOT LIKE '%$id%' and `category` NOT LIKE '%paddock%' ORDER BY `date` DESC LIMIT 0,10");

while($row = mysql_fetch_array($result))
{
echo '<div class="menu"><div class="item">';
echo '<h1><a href="news/display/'.$row['id'].'">'.$row['title'].'</a></h1>';
echo '<img src="../../'.$row['image'].'"/>';
echo '<p>'.$row['body'].'</p>';

echo '</div></div>';
}

mysql_close($con);
?>

Edited by ryanwood4
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.