MyBB Community Forums

Full Version: Add '...' if length of text is to long
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have this now (from the 'last 5 topics'):

<?php
require_once 'rss_fetch.inc';
$url = 'http://www.nintendo-gamer.nl/forum//rss.php?type=rss2.0&limit=5'; // URL of the feed
$MAGPIE_CACHE_ON = true; // Enables caching
$MAGPIE_CACHE_AGE = 900; // Time of caching (here: 15 minutes)
$rss = fetch_rss($url);

foreach ($rss->items as $item ) {
$title = $item[title];
$descr = $item[description];
$url = $item[link ];
echo "
<img src=\"../images/pijl2.gif\" border=\"0\"><a href=\"$url\" target=\"_blank\">$title</a><br>
";
}
?>

But if $title is to long, longer than 24 signs, i want 20 signs and then '...' behind that. How can i add this feature?
I haven't tested it but this should work

if(strlen($title) > 24)
	{
		$title = substr($title, 0, 20) . "....";
	}
and you'd do much better if you used php and mySQL to get the 5 latest threads (topics). Its probably faster, and really easy. Using Dempsey's code, this should work:
<?php
$username ="[b]mySQL Username[/b]"; $password ="[b]mySQL Password[/b]"; $database="[b]mySQL DB name[/b]";
$connection = mysql_connect("localhost","$username","$password");
@mysql_select_db("$database", $connection);

$sql = "SELECT * FROM mybb_threads ORDER BY `tid` DESC LIMIT 0,5";
$query = mysql_query($sql, $connection);

while ($item = mysql_fetch_assoc($query)) {
if (strlen($item['title']) > 24) { $title = substr($item['title'], 0, 20)."...."; } else { $title = $item['title']; }
$url = $item['link'];
echo "<img src=\"../images/pijl2.gif\" border=\"0\"><a href=\"$url\" target=\"_blank\">$title</a><br>";
}
?>
or if you have global.php of the forum is a nearby folder, for eg: ./forum/ you could use
<?php
require("./forum/global.php");

$sql = "SELECT * FROM ".TABLE_PREFIX."threads ORDER BY `tid` DESC LIMIT 0,5";
$query = $db->query($sql, $connection);

while ($item = $db->fetch_assoc($query)) {
if (strlen($item['title']) > 24) { $title = substr($item['title'], 0, 20)."...."; } else { $title = $item['title']; }
$url = $item['link'];
echo "<img src=\"../images/pijl2.gif\" border=\"0\"><a href=\"$url\" target=\"_blank\">$title</a><br>";
}
?>
so it loads everything needed about the forum and its database structure!
If i use the first code of you, k776, i get no results Confused I do get the image (../images/pijl1.gif), but I don't get the threadtitle + link Confused
Hmm. oh, right, made a few mistakes :$ Try this adapted second code. It will work better since myBB already had the data you need to get the threads.
<?php
require("./forum/global.php");

$query = $db->query("SELECT * FROM ".TABLE_PREFIX."threads ORDER BY `tid` DESC LIMIT 0,5");
while ($item = $db->fetch_array($query)) {
if (strlen($item['subject']) > 24) { $subject = substr($item['subject'], 0, 20)."...."; } else { $subject = $item['subject']; }
$tid = $item['tid'];
echo "<img src=\"../images/pijl2.gif\" border=\"0\"><a href=\"http://www.nintendo-gamer.nl/forum/showthread.php?tid=$tid\" target=\"_blank\">$subject</a><br>";
}
?>
That 'require' doesn't work Confused Can you change the code so it works without require'ing a file or can you change this please:

<?php
require_once 'rss_fetch.inc';
$url = 'http://www.nintendo-gamer.nl/forum//rss.php?type=rss2.0&limit=5'; // URL of the feed
$MAGPIE_CACHE_ON = true; // Enables caching
$MAGPIE_CACHE_AGE = 900; // Time of caching (here: 15 minutes)
$rss = fetch_rss($url);

foreach ($rss->items as $item ) {
$title = $item[title];
$descr = $item[description];
$url = $item[link ];
echo "
<img src=\"../images/pijl2.gif\" border=\"0\"><a href=\"$url\" target=\"_blank\">$title</a><br>
";
}
?>

Cause this didn't work:

if(strlen($title) > 24)
{
$title = substr($title, 0, 20) . "....";
}
Please, anyone Sad? It's a bit urgent Toungue
Nintendo-Gamer Wrote:Cause this didn't work:

if(strlen($title) > 24)
{
$title = substr($title, 0, 20) . "....";
}
where were you putting that bit of code? it should go after
$title = $item[title];
I've tested it and it works the way I showed you before. You must have it in another directory. Anyway, try this then:
<?php
$username ="[b]mySQL Username[/b]"; $password ="[b]mySQL Password[/b]"; $database="[b]mySQL DB name[/b]";
$connection = mysql_connect("localhost","$username","$password");
@mysql_select_db("$database", $connection);

$query = $db->query("SELECT * FROM [b]mybb_[/b]threads ORDER BY `tid` DESC LIMIT 0,5");
while ($item = $db->fetch_array($query)) {
if (strlen($item['subject']) > 24) { $subject = substr($item['subject'], 0, 20)."...."; } else { $subject = $item['subject']; }
$tid = $item['tid'];
echo "<img src=\"../images/pijl2.gif\" border=\"0\"><a href=\"http://www.nintendo-gamer.nl/forum/showthread.php?tid=$tid\" target=\"_blank\">$subject</a><br>";
}
?>
making sure you change the bold bits as needed!
o really thnx Smile now it works Smile