Page 1 of 1

Controling the size of a post

Posted: Wed Nov 20, 2002 3:59 pm
by oldtimer
On a certain page I print out the contents of posts submitted. The post DB field is a medium text. However on this one page I dont want it to display a long post. Only like the first so many lines of it. Then have a link for them read more about it. Is this a possablity with PHP or do I need to use javascript?

Posted: Wed Nov 20, 2002 5:43 pm
by JPlush76
this should hook you up

Code: Select all

<?php
if(strlen($VARIABLE)) > 27)
			{
				$new_cat_name = substr($VARIABLE,0,27) . '...';
			}

?>

basically if the field is bigger than 27 characters, I chop it off and add ...
at the end so people know it continues

then the link could just be

Code: Select all

<a href="linktopage.php"><?=$new_cat_name?></a>

Posted: Wed Nov 20, 2002 6:27 pm
by oldtimer
I tried. Here is what I have.


<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<?
include ("config.php");

// fetch the results from the database.
mysql_connect($DBhost,$DBuser,$DBpass) or
die("Unable
to connect to database");
@mysql_select_db("$DBname") or die("Unable to select
database");
$sqlquery = "SELECT * From posts where approved='Y' order by 'post_id' DESC";
$result=mysql_query($sqlquery);
$num=mysql_num_rows($result);
$i=0;
while ($i < $num) {
$poster=mysql_result($result,$i,'poster');
$poster=htmlspecialchars($poster);
$post=mysql_result($result,$i,'post');
$title=mysql_result($result,$i,'title');
$timestamp=mysql_result($result,$i,'timestamp');
$type=mysql_result($result,$i,'type');
if ($i < 5) { ?>
<tr>
<td valign="top"><font color="green" size="4"><? echo "$type"; ?>: </font>
<font size="4" class="capital"><? echo "$title"; ?><br></font>
<font color="#999999">Posted by:</font>
<font color="#FF9933"><? echo "$poster"; ?></font>
<font color="#999999">on</font>
<font color="#999999"> <? echo "$timestamp"; ?><br>
</font> <br>
<?
if(strlen($post)) > 100)
{
$new_post = substr($post,0,100) . '...';
echo $new_post; echo "<br>";
}
?>
<hr style="COLOR: sienna" noshade><BR>
</td></tr> <? } ?>
<?
++$i;
}
?>
</table>


When I try to look at the page this is on I get parse error on line 33 which is this one

if(strlen($post)) > 100)

Posted: Wed Nov 20, 2002 6:30 pm
by JPlush76
got an extra ) in there, sorry about that

use:

Code: Select all

&lt;?php
if(strlen($post) &gt; 100)

?&gt;

Posted: Wed Nov 20, 2002 6:43 pm
by oldtimer
Boy I missed that one too. Thanks. Now I got to find what computer I had the original index in. Seems I uploaded an older version. :(