Topic: Custom Page truncation

I managed to truncate the characters of the custom page to (25) to be displayed on the front page of the portal. How can I make it so that it displays '...'  if there is more text on the subject line? So if for example it says:

text to be read - <- end text
this is the text I want you to read whe...

Hope you follow what I am trying to say. Here is the code below if someone can assist me.

<?php

//get all pages info from the DB
    $result = $db->query('SELECT id, title FROM '.$db->prefix.'pages') or error('Unable to select pages from database', __FILE__, __LINE__, $db->error());
    if ($db->num_rows($result))
    {
$trunc_len = 20; // Set amount of text to be displayed in subject.

?>
<div class="block">
        <h2 class="block2"><span>Custom Page Links</span></h2>
        <div class="box">
            <div class="inbox">
                <ul>
<?php
        while($page_data = $db->fetch_assoc($result))
            echo "\t\t\t\t\t\t\t".'<li>'."\n\t\t\t\t\t\t\t\t".'<a href="page.php?id='.$page_data['id'].'">'.substr($page_data['title'], 0, $trunc_len).'</a></li>'."\n\t\t\t\t\t\t\t"."\n";
?>

                </ul>        
            </div>
        </div>
    </div>
 <?php
  }
?>

Re: Custom Page truncation

Shouldn't this code below work for what I want it to do?

<?php

//get all pages info from the DB
    $result = $db->query('SELECT id, title FROM '.$db->prefix.'pages') or error('Unable to select pages from database', __FILE__, __LINE__, $db->error());
    if ($db->num_rows($result))
    {

?>
<div class="block">
        <h2 class="block2"><span>Custom Page Links</span></h2>
        <div class="box">
            <div class="inbox">
                <ul>
<?php

        while($page_data = $db->fetch_assoc($result))



if ($trunc_len < 20) {
        echo "\t\t\t\t\t\t\t".'<li>'."\n\t\t\t\t\t\t\t\t".'<a href="page.php?id='.$page_data['id'].'">'.$page_data['title'].'</a></li>'."\n\t\t\t\t\t\t\t"."\n";
} else {
        echo "\t\t\t\t\t\t\t".'<li>'."\n\t\t\t\t\t\t\t\t".'<a href="page.php?id='.$page_data['id'].'">'.substr($page_data['title'], $trunc_len).'</a>...</li>'."\n\t\t\t\t\t\t\t"."\n";

}
?>

                </ul>        
            </div>
        </div>
    </div>
 <?php
  }
?>

Re: Custom Page truncation

$trunc_len isn't set anywhere in that code. It also has nothing to do with the actual size of the string, which is what you want in the if statement. You're looking for the strlen function.

4

Re: Custom Page truncation

Nope. You want:

if (strlen($page_data['title']) > $trunc_len)
{
    [truncated line]
}
else
{
    [full line]
}

Re: Custom Page truncation

Thank you both. It works great. Here is the working code.

<?php

//get all pages info from the DB
    $result = $db->query('SELECT id, title FROM '.$db->prefix.'pages') or error('Unable to select pages from database', __FILE__, __LINE__, $db->error());
    if ($db->num_rows($result))
    {
$trunc_len = 25; // Set amount of text to be displayed in subject.
?>
<div class="block">
        <h2 class="block2"><span>Custom Page Links</span></h2>
        <div class="box">
            <div class="inbox">
                <ul>
<?php

        while($page_data = $db->fetch_assoc($result))


if (strlen($page_data['title']) > $trunc_len)
{
            echo "\t\t\t\t\t\t\t".'<li>'."\n\t\t\t\t\t\t\t\t".'<a href="page.php?id='.$page_data['id'].'">'.substr($page_data['title'], 0, $trunc_len).'</a>...</li>'."\n\t\t\t\t\t\t\t"."\n";

}else {
            echo "\t\t\t\t\t\t\t".'<li>'."\n\t\t\t\t\t\t\t\t".'<a href="page.php?id='.$page_data['id'].'">'.$page_data['title'].'</a></li>'."\n\t\t\t\t\t\t\t"."\n";

}
?>

                </ul>        
            </div>
        </div>
    </div>
 <?php
  }
?>

Re: Custom Page truncation

I'm intrigued to know what you've been working on with all these modifications Bing. wink Is it a secret or just updates and new additions to MegaPun?

~James
FluxBB - Less is more

Re: Custom Page truncation

It is for Mega Pun 5.00 - I want to make it perfect. lol, well as close as I can get. smile