I'm trying to display a certain amount of words on one page from a database entry. For ex., on my main page I want to display the first 50 words of a story then a link will lead you to the rest of the story. I kind of know what I need to do but I don't know where to put the code. I want to display the first 50 words from 'story'. Here is my code.

<center>
			<table width="760"  border="3" bordercolor="ffffff" cellspacing="1" cellpadding="0">

<?
include("dbinfo.inc.php");
mysql_connect("nldesign.startlogicmysql.com",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM news ORDER BY ID DESC LIMIT 0, 1";
$result=mysql_query($query);

$num=mysql_numrows($result); 

mysql_close();

?>

<?
$i=0;
while ($i < $num) {
$headline=mysql_result($result,$i,"headline");   
$story=mysql_result ($result,$i,"story");   
$id=mysql_result($result,$i,"id");
?>

				<tr>
					<td bgcolor="ffffff" width="400" valign="top"><img src="mainpic.jpg"></img></td>
					<td bgcolor="ffffff" width="360" valign="top"><h4><? echo "$headline"; ?></h4><h3><? echo "$story"; ?></h3><a href="view_story.php?id=<? echo "$id"; ?>"  style="text-decoration:none"><h5>read more...</h5></a></td>
				</tr>
		
<?
++$i;
} 
echo "</table>";

?>
			</center><br>

Recommended Answers

All 6 Replies

instead of just

<h3><?php echo $story; ?></h3>
change it to 
<h3><?php echo substr($story,0,50);?></h3>

But TommyBs, that will be the first 50 charicters. I would do something like this

<tr>
<td bgcolor="ffffff" width="400" valign="top"><img src="mainpic.jpg"></img></td>
<td bgcolor="ffffff" width="360" valign="top"><h4><? echo "$headline"; ?></h4><h3><?
$Words = explode(" ", $story); //Split it into individual words

$i = 0; //Loop var
while ( $i <= 50 ) { //Before 50 words
$story[$i] = $Words[$i];//Put 50 words into origonal variable
$i++; //Add to our loop
}
echo implode(" ", $story)."..."; //Stick everything back together with spaces and add ... to the end
?></h3><a href="view_story.php?id=<? echo "$id"; ?>" style="text-decoration:none"><h5>read more...</h5></a></td>
</tr>

^^Havent actualy tested it^^
but get back on wether it works

oh yeah, sorry, morning brain, didn't read the question properly :S

But TommyBs, that will be the first 50 charicters. I would do something like this

<tr>
<td bgcolor="ffffff" width="400" valign="top"><img src="mainpic.jpg"></img></td>
<td bgcolor="ffffff" width="360" valign="top"><h4><? echo "$headline"; ?></h4><h3><?
$Words = explode(" ", $story); //Split it into individual words

$i = 0; //Loop var
while ( $i <= 50 ) { //Before 50 words
$story[$i] = $Words[$i];//Put 50 words into origonal variable
$i++; //Add to our loop
}
echo implode(" ", $story)."..."; //Stick everything back together with spaces and add ... to the end
?></h3><a href="view_story.php?id=<? echo "$id"; ?>" style="text-decoration:none"><h5>read more...</h5></a></td>
</tr>

^^Havent actualy tested it^^
but get back on wether it works

Not working :( Any other suggestions?

Member Avatar for soldierflup

Check the code on this page.

It works. I tried it out.

Grtz,

Soldierflup

commented: Good find, I'll be adding that sucker to my library +4
Member Avatar for diafol

Nice one soldier - well found. The array_slice is a beautiful function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.