...

View Full Version : Taking the 1st 45 chars, and adding .. after



njfail
11-11-2012, 06:07 AM
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?


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,
));
}

Fumigator
11-11-2012, 06:28 AM
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...


if (strlen(trim($yourTitle)) > 46)
{
$yourTitle = substr($yourTitle, 0, 46) . "...";
}

minder
11-11-2012, 06:30 AM
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

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

function chopStr($str, $len, $ext){
return (strlen($str) > $len) ? substr($str, 0, $len) . $ext : substr($str, 0, $len);
}
?>

njfail
11-11-2012, 06:49 AM
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 :o

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

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..

minder
11-11-2012, 07:45 AM
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.

poyzn
11-11-2012, 08:08 AM
Instead of


$topic_title = $topics_row['topic_title'];

try this


$topic_title = substr( $topics_row['topic_title'], 0, 45 ) . ' some text';

minder
11-11-2012, 08:26 AM
$topic_title = substr( $topics_row['topic_title'], 0, 45 ) . ' 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.

firepages
11-11-2012, 11:11 AM
SELECT * is horrible... but anyway, you can use SQL to do the trimming for you and its faster, e.g.



$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';

poyzn
11-11-2012, 09:06 PM
$topic_title = ( strlen($topics_row['topic_title'] ) > 45 ) ? ( substr( $topics_row['topic_title'], 0, 45 ) . ' some text' ) : $topics_row['topic_title'];

njfail
11-12-2012, 05:25 AM
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:

<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>


SELECT * is horrible... but anyway, you can use SQL to do the trimming for you and its faster, e.g.



$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? :confused:
(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 :o

firepages
11-12-2012, 06:00 AM
.....
Why is Select * horrible? :confused:
(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 :o

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?



$topics = SELECT *,
IF( LENGTH( `topic_title` ) <40, `topic_title`, CONCAT( LEFT( `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';



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum