TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
  • Total Members: 3,963
  • Latest: BiZaJe
Stats
  • Total Posts: 195,917
  • Total Topics: 21,308
  • Online today: 790
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 423
  • Total: 423

Random article in Frontpage block

Started by sanax, February 02, 2007, 08:52:11 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

RogerLewin

How do i show only the first 30 characters of the body?

I tried this


global $db_prefix, $scripturl;

$query = db_query(
    "SELECT id, intro, subject
     FROM {$db_prefix}tp_articles
     ORDER BY rand()
     LIMIT 1", __FILE__, __LINE__);

$row = mysql_fetch_assoc($query);

echo '<b>' , $row['subject'] , '</b><br />';
echo substr($row['body'],1,30);
echo '<a href="' , $scripturl , '?page=' , $row['id'] , '">Den vollen Artikel lesen...</a>';

RogerLewin

OK, i got it, but the following code shows also the html tags. How do i get the text shown after being interpreted?


global $db_prefix, $scripturl;

$query = db_query(
    "SELECT id, intro, subject, body
     FROM {$db_prefix}tp_articles
     ORDER BY rand()
     LIMIT 1", __FILE__, __LINE__);

$row = mysql_fetch_assoc($query);

echo '<b>' , $row['subject'] , '</b><br />';
echo substr($row['body'],1,300) ,'....', '<br/>';
echo '<a href="' , $scripturl , '?page=' , $row['id'] , '">Alles lesen...</a>';

IchBin

Not sure what you're talking about. Care to post a link so we can see what is happening?

RogerLewin

Yes, of course. The Box 'Wissen' is showing the html tags also, not the text after being interpreted by the webserver

http://portal.dasaugederhorde.de/ in the upper left corner

JPDeni

This code


echo substr($row['body'],1,300) ,'....', '<br/>';


is cutting off the first character and probably causing the problem. Try


echo substr($row['body'],0,300) ,'....', '<br/>';

RogerLewin


RogerLewin


JPDeni

I'm pretty sure, now that I think more about it, that it has to do with the fact that you have open html tags, but you're cutting off the text before the closing tags are printed. I know that you don't want it this way all the time, but for testing purposes, try printing out the whole article. Use


echo $row['body'] , '<br/>';


instead of


echo substr($row['body'],0,300) ,'....', '<br/>';


The result of that will give us a clue as to what's going on.

JPDeni

Doing more research....

Try


echo html_entity_decode(substr($row['body'],0,300)) ,'....', '<br/>';

RogerLewin

Wonderful - this worked. Thank you very much and especially because this was a php question and not a TP question :)

This website is proudly hosted on Crocweb Cloud Website Hosting.