I'm hoping someone can point me in the right direction with this. I have a website and on the front page is a list of news posts which have been posted by users. At the moment the news page shows the whole news post. I would prefer it to only show the first 75 characters of the post. I have written the following MySQL code but it only prints one result even though I have 6 news articles in my database.
<?
// format MySQL DATETIME value into a more readable string
function formatDate($val)
{ $arr = explode("-", $val);
return date("d M Y", mktime(0,0,0, $arr[1], $arr[2], $arr[0])); }
// database configuration
$db_name = "news";
$connection = mysql_connect ("localhost", "user", "password") or die(mysql_error());
$db = mysql_select_db($db_name, $connection);
$sql = "SELECT id, slug, LEFT(content,75), contact, timestamp FROM news ORDER BY timestamp DESC LIMIT 0, 5";
$result = mysql_query($sql,$connection) or die(mysql_error());
// if records present
if (mysql_num_rows($result) > 0)
{
// iterate through resultset
// print article titles
{
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$slug = $row['slug'];
$content = $row['LEFT(content,75)'];
$contact = $row['contact'];
$timestamp = $row['timestamp']; } ?>
<li><b><font color="#000000"></font><? echo $slug; ?> </b>-
<? echo formatDate($timestamp); ?></font> <br>
<div align="justify"> <? echo $content; ?></div>
<p>
<?
}
}
// if no records present
// display message
else
{
?>
No news items currently available
<?
}
// close database connection
mysql_close($connection);
?>
Many thanks for your help.
Stuart