ryanwood4 Posted November 11, 2012 Share Posted November 11, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/270565-php-word-count-limit/ Share on other sites More sharing options...
Pikachu2000 Posted November 11, 2012 Share Posted November 11, 2012 (edited) I'd be inclined to limit it in the query itself, using SUBSTRING_INDEX(). Edited November 11, 2012 by Pikachu2000 Quote Link to comment https://forums.phpfreaks.com/topic/270565-php-word-count-limit/#findComment-1391705 Share on other sites More sharing options...
Andy123 Posted November 11, 2012 Share Posted November 11, 2012 (edited) 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 November 11, 2012 by Andy123 Quote Link to comment https://forums.phpfreaks.com/topic/270565-php-word-count-limit/#findComment-1391707 Share on other sites More sharing options...
ryanwood4 Posted November 11, 2012 Author Share Posted November 11, 2012 (edited) 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 November 11, 2012 by ryanwood4 Quote Link to comment https://forums.phpfreaks.com/topic/270565-php-word-count-limit/#findComment-1391708 Share on other sites More sharing options...
PFMaBiSmAd Posted November 11, 2012 Share Posted November 11, 2012 Pika's reply contains a link to the mysql documentation for that function. There's an example at that link. Quote Link to comment https://forums.phpfreaks.com/topic/270565-php-word-count-limit/#findComment-1391716 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.