Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Limiting word count?!

Featured Replies

Hi people, i'm having trouble with limiting the number of words with regards to an advertising list. Instead of limiting it keeps repeating.

 

Here is the code i've been tryin to get working, any help would be great thanks.

 

while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
if ($row) {

$description = nl2br($row['description']);
	$words = explode(" ", $description);
$small = array_slice($words, 0, 20);

for($x=0; $x<sizeof($small);$x++)
{


echo "<tr><td><img src=\"ad_images/{$row['image_1']}\" width=\"150px\" height=\"85px\"/></td></tr>\n\n";
echo "<tr><td><a href=\"buyer.php?classified_ad=car&id={$row['ad_id']}\" target=\"_self\">{$row['title']}</a></td></tr>";
echo "<tr><td>{$row['mileage']} miles</td></tr>";

echo $small[$x] . "$description";;
}}}

Why are you using an if loop in a while loop? I think you meant to put them the other way round:

if (isset($row)) {
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {

$description = nl2br($row['description']);
	$words = explode(" ", $description);
$small = array_slice($words, 0, 20);

etc etc?

 

Hope this helps.

  • Author

I tried that....works exactly the same but doesn't solve the issue unfortunately.

 

Why are you using an if loop in a while loop? I think you meant to put them the other way round:

if (isset($row)) {
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {

$description = nl2br($row['description']);
	$words = explode(" ", $description);
$small = array_slice($words, 0, 20);

etc etc?

 

Hope this helps.

while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {

if ($row) {

 

$description = nl2br($row['description']);

$words = explode(" ", $description);

$small = array_slice($words, 0, 20);

 

for($x=0; $x<sizeof($small);$x++)

{

 

 

echo "<tr><td><img src=\"ad_images/{$row['image_1']}\" width=\"150px\" height=\"85px\"/></td></tr>\n\n";

echo "<tr><td><a href=\"buyer.php?classified_ad=car&id={$row['ad_id']}\" target=\"_self\">{$row['title']}</a></td></tr>";

echo "<tr><td>{$row['mileage']} miles</td></tr>";

 

echo $small[$x] . "$description";;

}}}

 

 

Check out the character in red ( { )- I think that may be part of your problem?... Certainly looks like it shouldn't be there.

Are you trying to limit the amount of words in a string? If so I think your doing it totally the wrong way!?

  • Author

Yes that is what i'm trying to do. Do you have a solution?

 

Are you trying to limit the amount of words in a string? If so I think your doing it totally the wrong way!?

You can do it by limiting the number of characters like this:

 

if (strlen($row['description']) > 500) {
	// shorten string and add '...'
	$small = substr($row['description'], 0, 500) . '...';
}

 

Dunno if that would be any good?

  • Author

Thanks buddy, that did the job. :D

 

You can do it by limiting the number of characters like this:

 

if (strlen($row['description']) > 500) {
	// shorten string and add '...'
	$small = substr($row['description'], 0, 500) . '...';
}

 

Dunno if that would be any good?

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.