Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    133
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Taking the 1st 45 chars, and adding .. after

    I want to take the first 45 characters out of Topic_Title, and have .. after it if it cuts something off.

    I don't really know PHP, I followed a tutorial and did a lot of copy/pasting to put together the code below.

    Could someone help me?

    Code:
        define('IN_PHPBB', true);
        $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
        $phpEx = substr(strrchr(__FILE__, '.'), 1);
        include($phpbb_root_path . 'common.' . $phpEx);
    	include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
    	include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
    	
        // Start session management
        $user->session_begin();
        $auth->acl($user->data);
        $user->setup('viewtopic');
    
    /* create_where_clauses( int[] gen_id, String type )
    * This function outputs an SQL WHERE statement for use when grabbing 
    * posts and topics */
    
    function create_where_clauses($gen_id, $type)
    {
    global $db, $auth;
    
        $size_gen_id = sizeof($gen_id);
    
            switch($type)
            {
                case 'forum':
                    $type = 'forum_id';
                    break;
                case 'topic':
                    $type = 'topic_id';
                    break;
                default:
                    trigger_error('No type defined');
            }
    
        // Set $out_where to nothing, this will be used of the gen_id
        // size is empty, in other words "grab from anywhere" with
        // no restrictions
        $out_where = '';
    
        if( $size_gen_id > 0 )
        {
        // Get a list of all forums the user has permissions to read
        $auth_f_read = array_keys($auth->acl_getf('f_read', true));
    
            if( $type == 'topic_id' )
            {
                $sql     = 'SELECT topic_id FROM ' . TOPICS_TABLE . '
                            WHERE ' .  $db->sql_in_set('topic_id', $gen_id) . '
                            AND ' .  $db->sql_in_set('forum_id', $auth_f_read);
    
                $result     = $db->sql_query($sql);
    
                    while( $row = $db->sql_fetchrow($result) )
                    {
                            // Create an array with all acceptable topic ids
                            $topic_id_list[] = $row['topic_id'];
                    }
    
                unset($gen_id);
    
                $gen_id = $topic_id_list;
                $size_gen_id = sizeof($gen_id);
            }
    
        $j = 0;    
    
            for( $i = 0; $i < $size_gen_id; $i++ )
            {
            $id_check = (int) $gen_id[$i];
    
                // If the type is topic, all checks have been made and the query can start to be built
                if( $type == 'topic_id' )
                {
                    $out_where .= ($j == 0) ? 'WHERE ' . $type . ' = ' . $id_check . ' ' : 'OR ' . $type . ' = ' . $id_check . ' ';
                }
    
                // If the type is forum, do the check to make sure the user has read permissions
                else if( $type == 'forum_id' && $auth->acl_get('f_read', $id_check) )
                {
                    $out_where .= ($j == 0) ? 'WHERE ' . $type . ' = ' . $id_check . ' ' : 'OR ' . $type . ' = ' . $id_check . ' ';
                }    
    
            $j++;
            }
        }
    
        if( $out_where == '' && $size_gen_id > 0 )
        {
            trigger_error('A list of topics/forums has not been created');
        }
    
        return $out_where;
    }
    	
    	
    /* Latest Topics */
    $search_limita = 10;
    
        $forum_id = array(18, 7, 10, 17, 4);
        $forum_id_where = create_where_clauses($forum_id, 'forum');
    
    $topics = 'SELECT * FROM ' . TOPICS_TABLE . '
                ' . $forum_id_where . '
                  AND topic_status <> ' . ITEM_MOVED . '
                  AND topic_approved = 1
                ORDER BY topic_id DESC';
    
       $topics_result = $db->sql_query_limit($topics, $search_limita);
    
          while( $topics_row = $db->sql_fetchrow($topics_result) )
          {
             $topic_title       = $topics_row['topic_title'];
             $topic_author       = get_username_string('full', $topics_row['topic_poster'], $topics_row['topic_first_poster_name'], $topics_row['topic_first_poster_colour']);
             $topic_date       = $user->format_date($topics_row['topic_time']);
             $topic_last_post    = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $topics_row['forum_id'] . '&amp;t=' . $topics_row['topic_id'] . '&amp;p=' . $topics_row['topic_last_post_id']) . '#p' . $topics_row['topic_last_post_id'];
             $topic_last_author    = get_username_string('full', $topics_row['topic_last_poster_id'], $topics_row['topic_last_poster_name'], $topics_row['topic_last_poster_colour']);
             $topic_link       = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $topics_row['forum_id'] . '&amp;t=' . $topics_row['topic_id']);
    
             $template->assign_block_vars('announcementsa', array(
             'TOPIC_TITLEA'       => censor_text($topic_title),
             'TOPIC_AUTHORA'       => $topic_author,
             'TOPIC_DATE'       => $topic_date,
             'TOPIC_LAST_POST'    => $topic_last_post,
             'TOPIC_LAST_AUTHOR' => $topic_last_author,
             'TOPIC_LINK'       => $topic_link,
             ));
          }
    Last edited by njfail; 11-11-2012 at 06:10 AM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Here are the functions to use.

    trim(): this will remove any spaces or other empty characters from beginning and end of the title.

    strlen(): get the length of the title (after it's been trimmed).

    substr(): select just a portion of the title.

    Use a period to glue the portion of the title you want to "...".

    You can look up each of these functions up in the PHP manual at php.net. Your final code will be something like...

    PHP Code:
    if (strlen(trim($yourTitle)) > 46)
    {
        
    $yourTitle substr($yourTitle046) . "...";


  • #3
    Banned
    Join Date
    Oct 2012
    Posts
    81
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by njfail View Post
    I want to take the first 45 characters out of Topic_Title, and have .. after it if it cuts something off.
    Either you're not telling us everything you need or you are grossly over-scienceing a solution.

    To me this is all you need.

    PHP Code:
    <?php

    echo chopStr('qwertyuiop',5,'.....');

    function 
    chopStr($str$len$ext){
        return (
    strlen($str) > $len) ? substr($str0$len) . $ext substr($str0$len);
    }
    ?>
    Last edited by minder; 11-11-2012 at 06:34 AM.

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    133
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by minder View Post
    Either you're not telling us everything you need or you are grossly over-scienceing a solution.
    Haha, I actually have no idea how php really works

    So I was hoping if you could not just tell me the code I need to add, but also where to add it

    Basically, the way it is now, it shows up as
    This is the topic title of a very long topic that has a title that is pretty long and stretches my stuff

    Whereas I would like it to show up as:
    This is the topic title of a very long topi..

  • #5
    Banned
    Join Date
    Oct 2012
    Posts
    81
    Thanks
    0
    Thanked 4 Times in 4 Posts
    It appears you haven't posted the code which outputs the title to the browser. When you find the line of code, just first pass the value of the title string through the function I posted.

    Adjust the values of $len and $ext to suit what you want.

  • #6
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    Instead of

    PHP Code:
    $topic_title $topics_row['topic_title']; 
    try this

    PHP Code:
    $topic_title substr$topics_row['topic_title'], 045 ) . ' some text'

  • #7
    Banned
    Join Date
    Oct 2012
    Posts
    81
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by poyzn View Post

    PHP Code:
    $topic_title substr$topics_row['topic_title'], 045 ) . ' some text'
    That's going to add 'some text' even when the title is <= 45 chars long which is not what the op wants. 'some text' needs to be added only if the title is truncated.

  • #8
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    SELECT * is horrible... but anyway, you can use SQL to do the trimming for you and its faster, e.g.

    PHP Code:
    $topics 'SELECT *, LEFT(`topic_title`,45) as `topic_title` FROM ' TOPICS_TABLE '
                ' 
    $forum_id_where '
                  AND topic_status <> ' 
    ITEM_MOVED '
                  AND topic_approved = 1
                ORDER BY topic_id DESC'

    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #9
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    PHP Code:
    $topic_title = ( strlen($topics_row['topic_title'] ) > 45 ) ? ( substr$topics_row['topic_title'], 045 ) . ' some text' ) : $topics_row['topic_title']; 

  • #10
    Regular Coder
    Join Date
    Aug 2010
    Posts
    133
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by minder View Post
    It appears you haven't posted the code which outputs the title to the browser. When you find the line of code, just first pass the value of the title string through the function I posted.

    Adjust the values of $len and $ext to suit what you want.
    I didn't think that was necessary, here is the other code:
    Code:
    			<div class="xpostlistcontainer">
    				<div class="panel">
    					<div class="inner"><span class="corners-top"><span></span></span>
    						<!-- BEGIN announcementsa -->
    							<div class="xlastpost">
    								<a href="{announcementsa.TOPIC_LAST_POST}"><img src="/forum/images/post_new.png" title="Go to Last Post In Topic" alt="Last Post" /></a>
    							</div>						
    							<div class="xtopictitle">
    								<a href="{announcementsa.TOPIC_LINK}" alt="Go to first post in topic">{announcementsa.TOPIC_TITLEA}</a>
    							</div>
    							<div class="xtopictimestamp">
    								{announcementsa.TOPIC_DATE}
    							</div>
    							<div class="xtopicauthor">
    								{announcementsa.TOPIC_AUTHORA}
    							</div>
    						<!-- END announcementsa -->
    					<span class="corners-bottom"><span></span></span></div>
    				</div>
    			</div>
    Quote Originally Posted by firepages View Post
    SELECT * is horrible... but anyway, you can use SQL to do the trimming for you and its faster, e.g.

    PHP Code:
    $topics 'SELECT *, LEFT(`topic_title`,45) as `topic_title` FROM ' TOPICS_TABLE '
                ' 
    $forum_id_where '
                  AND topic_status <> ' 
    ITEM_MOVED '
                  AND topic_approved = 1
                ORDER BY topic_id DESC'

    Why is Select * horrible?
    (I didn't write the code, I just copied and pasted it from a tutorial).
    Also, will that code add an ellipses at the end ? ...

    I really don't know anything about PHP, so I need to know exactly what to paste and where to paste it
    Last edited by njfail; 11-12-2012 at 05:28 AM.

  • #11
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    Quote Originally Posted by njfail View Post
    .....
    Why is Select * horrible?
    (I didn't write the code, I just copied and pasted it from a tutorial).
    Also, will that code add an ellipses at the end ? ...

    I really don't know anything about PHP, so I need to know exactly what to paste and where to paste it
    I know you did not write the code and was not having a go or anythng, just a comment.
    Its not bad if you really need ALL the data from each row of the table (though some tell me thats still bad) , but normally you only want a small subset, so you end up pulling loads of data that you dont actually need or use.

    & sorry you are correct that that does not give you the ellipses it just truncates the output to 45 chars which means you wont know if the title is complete or not ..

    the below rather inelegant attempt should work, I am sure there is a prettier way to do it though?

    PHP Code:
    $topics SELECT *, 
              IF( 
    LENGTH( `topic_title` ) <40, `topic_title`, CONCATLEFT( `topic_title`, 40 ) , '...' ) ) AS `topic_title`
              
    FROM ' . TOPICS_TABLE . '
               ' . $forum_id_where . '
               
    AND topic_status <> ' . ITEM_MOVED . '
               
    AND topic_approved 1
               ORDER BY topic_id DESC

    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •