RE: [PHP] Displaying first 100 characters of an article
| From: | Tim Converse | Date: | Wed, 04 Oct 2000 05:16:36 +0000 |
| Subject: | RE: [PHP] Displaying first 100 characters of an article | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-18522@lists.php.net to get a copy of this message | ||
Ben ---
What role do you want the variable $retstring to be playing here? Should it
be holding the title? In your current code, it is just having more "stuff"
concatenated onto the end of it on each trip through your while loop.
If $retstring is not holding anything important as you come into the code
fragment you posted, then you could just do this:
if (strlen($stuff)<125) {
$retstring = $stuff;
}
else {
$retstring = substr($stuff,0,125) . '...';
}
print "$retstring";
That is, cut out the successive concatenations to the end of $retstring, and
assign it anew with each trip through the loop.
--tim
On Tue, 03 Oct 2000 23:56:17 -0500, Ben Weinberger wrote:
> Hi~
> Everything works great except for one aspect. Each time the next article
> is displayed, it takes the previous article's first 125 characters and
> prepends them to the current article's first 125 characters. To see what
I
> mean, go to http://www.siualumni.com/news.php. Any ideas?
> The code I
> ended up using is:
> <?
> // connect to database here
> $mySQL = "SELECT * FROM news order by date limit 4";
> $results = mysql_query($mySQL);
> while ($myrow=mysql_fetch_array($results)) {
>
> $stuff = "$myrow[message]";
>
> if (strlen($stuff)<125) {
> $retstring = $retstring . $stuff;
> }
>
> else {
> $retstring = $retstring . substr($stuff,0,125) . '...';
> }
> print "$retstring";
> }
> ?>
>
> Thanks,
> -Ben
>
> At 03:30 AM 10/3/2000 -0400, Lawrence.Sheed@dfait-maeci.gc.ca wrote:
> >How about:
> >
> >$stuff = "my long string here";
> >
> >if (strlen($stuff)<100) {
> > $retstring = $retstring . $stuff;
> >}
> >else {
> > $retstring = $retstring . substr($stuff,0,100) . "...";
> >}
> >
> >cheers,
> >
> >Lawrence.
> >
> >-----Original Message-----
> >From: Ben Weinberger [mailto:ben@digitalsmiths.com]
> >Sent: October 3, 2000 03:27 PM
> >To: php-general@lists.php.net
> >Subject: [PHP] Displaying first 100 characters of an article
> >
> >
> >Hi~
> >I'm pulling data out of a MySQL db and displaying it on a web page...
the
> >data is like a news article with a title and a body. Currently, I can
> >display the title and I have it linked to a javascript pop-up window
that
> >displays the full article. I would like to add the first 100 characters
of
> >the article under the title as well... something like follows:
> >
> >Version 6.0 just Released
> >We've worked hard to bring you a new and improved version 6.0 software
> >package so that you....
> >
> >Is there a way to make those words under the title appear and only
display
> >the first 100 characters followed by a "..."?
> >
> >Thanks,
> >-Ben
----
Support me and PHP at the same time. :)
Order the PHP 4 Bible using http://www.php.net/books.php.
_______________________________________________________
Say Bye to Slow Internet!
http://www.home.com/xinbox/signup.html