You are here: Re: I want to display only a certain number of words from a database entry « PHP SQL « IT news, forums, messages
Re: I want to display only a certain number of words from a database entry

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация