|
Posted by NC on 11/17/05 20:10
Jonathan wrote:
> I want to be able to take a description from a database, row called
> {car_description} and cut it to a certain number of words and display
> only that for the main gallery of cars before you click to get the full
> description ... like many other sites do with the short description and
> the "more" button next to it.. I can do the mroe button but how do I
> right the rest of the tag..
>
> Assume I already have a db connection...so from inserting
> into html template engine which gets crunched by php what would I do?
Assuming you also have a car_id field in the table, you can do
something like this:
$n = 50; // (number of words you want to output)
$select = "SELECT car_id, car_description FROM cars WHERE [condition]";
$result = mysql_query($select)
or die('Query failed');
while ($car = mysql_fetch_array($result, MYSQL_ASSOC)) {
$words = explode(' ', $car['car_description']);
for ($i=0; $i<$n; $i++) {
echo $words[$i], ' ';
}
$id = $car['car_id'];
echo "\r\n<a href='more.php?id=$id'>More...</a>\r\n";
}
In more.php, you need to retrieve the full description again, but this
time show it in its entirety.
Cheers,
NC
Navigation:
[Reply to this message]
|