May 1, 200917 yr 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";; }}}
May 1, 200917 yr 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.
May 1, 200917 yr 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.
May 1, 200917 yr 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.
May 1, 200917 yr Are you trying to limit the amount of words in a string? If so I think your doing it totally the wrong way!?
May 2, 200917 yr 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!?
May 2, 200917 yr 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?
May 2, 200917 yr Author Thanks buddy, that did the job. 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