Peosan Posted July 29, 2009 Share Posted July 29, 2009 Hello! I have a problem. Here's what I need help with; I have this PHP code, used for linking phpbb3 content to a website, which I have done successfully. But now the problem is when I type my content on the forums, it gets to long. So I'm wondering, is there a code for it to reduce the characters to a specific amount? Here's the code: <?php define('IN_PHPBB', true); $phpbb_root_path = (defined('forum/')) ? PHPBB_ROOT_PATH : './forum/'; // Replace with your own root path $phpEx = substr(strrchr(__FILE__, '.'), 1); include_once($phpbb_root_path . 'common.' . $phpEx); include_once($phpbb_root_path . 'includes/functions_display.' . $phpEx); #Check if these necessary files to be included. include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); #Start session management $user->session_begin(); $auth->acl($user->data); $user->setup(); #function used to convert bytes into kb or mb or gb. To be used later, you can make your own if you want, or even take it out, but you'll need to modify the php later on to remove the function call. function bytesize($bytes){ $size = $bytes / 1024; if($size < 1024){ $size = number_format($size, 2); $size .= ' KB'; }else{ if($size / 1024 < 1024){ $size = number_format($size / 1024, 2); $size .= ' MB'; }elseif($size / 1024 / 1024 < 1024){ $size = number_format($size / 1024 / 1024, 2); $size .= ' GB'; } } return $size; } /* * @param String $sort_order Has 13 options: (Default is s'ticky_recent_desc') * Options: 1. 'recent_asc' Gives oldest to newest topics 2. 'recent_desc' Vice versa 3. 'sticky_recent_desc' Gives newest to oldest topics, however it retains announcements and stickies on top 4. 'active_asc' Gives the early replied to recently replied 5. 'active_desc' Vice versa 6. 'subject_asc' Gives it in alphabetical order 7. 'subject_desc' Vice versa 8. 'author_asc' Gives authors in alphabetical order 9. 'author_desc' Vice versa 10. 'views_asc' Gives the least views to most views 11. 'views_desc' Vice versa 12. 'replies_asc' Gives the least replies to the most replies 13. 'replies_desc' Vice versa * @return String $sort_order_query The mysql coding to be used in the get_news function */ function sort_order_query($sort_order){ #switches for sort_order switch ($sort_order){ case 'recent_asc': $sort_order_query = 't.topic_time ASC'; break; case 'recent_desc': $sort_order_query = 't.topic_time DESC'; break; case 'sticky_recent_desc': $sort_order_query = 't.topic_type DESC, t.topic_time DESC'; break; case 'active_asc': $sort_order_query = 't.topic_last_post_id ASC'; break; case 'active_desc': $sort_order_query = 't.topic_last_post_id DESC'; break; case 'subject_asc': $sort_order_query = 't.topic_title ASC'; break; case 'subject_desc': $sort_order_query = 't.topic_title DESC'; break; case 'author_asc': $sort_order_query = 't.topic_first_poster_name ASC'; break; case 'author_desc': $sort_order_query = 't.topic_first_poster_name DESC'; break; case 'views_asc': $sort_order_query = 't.topic_views ASC'; break; case 'views_desc': $sort_order_query = 't.topic_views DESC'; break; case 'replies_asc': $sort_order_query = 't.topic_replies_real ASC'; break; case 'replies_desc': $sort_order_query = 't.topic_replies_real DESC'; break; default: $sort_order_query = 't.topic_type DESC, t.topic_time DESC'; } return($sort_order_query); } /** VERSION 4.09 * Retrieve the first post of each topic in a given forum * * @param Array $forum_ids An array containing all the forum ids of the forums that must be included * @param Array $parent_only_ids An array containg the forum ids that will only give you the topics inside that forum, and not any children. It is used to mix up with $forum_ids. However if $include_children is false, this will do nothing. If you do not want to use it, declare false on it when calling the function * @param Integer $limit The maximum number of topics to be returned * @param Boolean $include_children This variable specifies whether we should include the "child forums" of the given $forum_ids in the results * @param Integer $offset Set this variable if you don't want the result set begin on the first entry * @param String $sort_order It is mysql coding of the query order. This is given by the sort_order_query function (Default is t.topic_time DESC) * @param Boolean $allow_attachments (Default is true) Displays attachments (NOT INLINE IT WILL SHOW ALL ATTACHMENTS BUT INLINE WILL NOT BE PROCESSED) * @param Boolean $allow_ranks (Default is true) Display ranks * @param Boolean $allow_sigs (Default is true) Display signatures * @param Boolean $allow_avatar (Default is true) Display avatars * @return Array $posts An array holding all the posts and their data * NOTES: If you want forum posts + children (subforum) posts. Check include_children as true and check false for $parent_only_ids. * If you want forum posts + children (subforum) posts + forum posts not including children posts. Check include_children as true, and input values for $forum_ids and $parent_only_ids * If you only want forum posts, DO NOT check $forum_ids as false and use $parent_only_ids. You must check $include_children as false, and simply input values for $forum_ids, and check $parent_only_ids as false. * Remember that $parent_only_ids only work when $forum_ids are working regardless of $include_children. So $forum_ids is always the first one you have to use. * The function will not output topics which are not approved. */ function get_news($forum_ids = array(), $parent_only_ids = array(), $limit = 10, $offset = 0, $include_children = false, $sort_order = 't.topic_type DESC, t.topic_time DESC', $allow_attachments = true, $allow_ranks = true, $allow_sigs = true, $allow_avatar = true){ global $db, $phpbb_root_path, $phpEx; if (!is_array($forum_ids)){ $forum_ids = array($forum_ids); } if (!is_array($parent_only_ids)){ $parent_only_ids = array($parent_only_ids); } #If required merge the children into the $forum_ids if ($include_children){ #This can get very heavy when specifing a big array $forum_ids $child_forums = array(); foreach ($forum_ids as $parent){ $children = get_forum_branch($parent, 'children'); foreach ($children as $child){ $child_forums[] = $child['forum_id']; } } #Merge $forum_ids = array_merge($forum_ids, $child_forums, $parent_only_ids); } #Remove duplicates $forum_ids = array_unique($forum_ids); $post_query = 'p.post_id, p.post_time, p.post_text, p.bbcode_bitfield, p.bbcode_uid, p.enable_bbcode, p.enable_smilies, p.enable_magic_url, p.post_attachment, t.topic_id, t.forum_id, t.topic_title, t.topic_views, t.topic_replies_real, t.topic_type, t.topic_first_poster_name, t.topic_first_post_id, t.topic_last_post_time, u.user_id, u.user_colour'; /*if($allow_ranks){ $post_query .= ',u.user_rank, u.user_posts'; }*/ if($allow_sigs){ $post_query .= ',u.user_sig, u.user_sig_bbcode_uid, u.user_sig_bbcode_bitfield'; } if($allow_avatar){ $post_query .= ',u.user_avatar, u.user_avatar_type, u.user_avatar_width, u.user_avatar_height'; } #Now select the post data $post_sql_ary = array( 'SELECT' => $post_query, 'FROM' => array( POSTS_TABLE => 'p', TOPICS_TABLE => 't', ), 'LEFT_JOIN' => array( array( 'FROM' => array(USERS_TABLE => 'u'), 'ON' => 'u.user_id = p.poster_id', ), ), 'WHERE' => '(' . $db->sql_in_set('t.forum_id', $forum_ids) . ' AND p.post_id = t.topic_first_post_id AND t.topic_approved = 1)', 'ORDER_BY' => $sort_order, ); $sql = $db->sql_build_query('SELECT', $post_sql_ary); $result = $db->sql_query_limit($sql, $limit, $offset); #Set an array with the posts $posts = array(); while ($post = $db->sql_fetchrow($result)){ #Getting the Attachments if($allow_attachments && $post['post_attachment']){ unset($attachment_urls, $attachment_ids, $attachment_names,$attachment_downloads, $attachment_comments, $attachment_filesize, $num_of_attachment_ids); $attachments_sql_ary = array( 'SELECT' => 'a.attach_id, a.real_filename, a.download_count, a.attach_comment, a.filesize', 'FROM' => array( ATTACHMENTS_TABLE => 'a', ), 'WHERE' => '(a.post_msg_id = ' . $post['post_id'] . ' AND a.in_message = 0)', 'ORDER_BY' => 'a.attach_id DESC', ); $attachments_sql = $db->sql_build_query('SELECT', $attachments_sql_ary); $attachments_result = $db->sql_query($attachments_sql); while ($row = $db->sql_fetchrow($attachments_result)){ $attachment_ids[] = $row['attach_id']; $attachment_names[] = $row['real_filename']; $attachment_downloads[] = $row['download_count']; $attachment_comments[] = $row['attach_comment']; $attachment_filesize[] = $row['filesize']; } $db->sql_freeresult($attachments_result); $num_of_attachment_ids = count($attachment_ids); for($i=key($attachment_ids);$i<$num_of_attachment_ids;$i++){ $attachment_urls[$i] = $phpbb_root_path . 'download/file.' . $phpEx . '?id=' . $attachment_ids[$i]; } } #Getting the Breadcrumbs unset($breadcrumb_ids); if($post['forum_id'] > 0){ $breadcrumb_ids_sql_ary = array( 'SELECT' => 'f.forum_id, f.forum_name', 'FROM' => array( FORUMS_TABLE => 'f', ), 'WHERE' => '(f.left_id <= (SELECT left_id FROM ' . FORUMS_TABLE . ' WHERE forum_id = ' . $post['forum_id'] . ') AND f.right_id >= (SELECT right_id FROM ' . FORUMS_TABLE . ' WHERE forum_id = ' . $post['forum_id'] . '))', ); $bread_crumb_ids_sql = $db->sql_build_query('SELECT', $breadcrumb_ids_sql_ary); $bread_crumb_ids_result = $db->sql_query($bread_crumb_ids_sql); while ($row = $db->sql_fetchrow($bread_crumb_ids_result)){ $breadcrumb_ids[$row['forum_name']] = $row['forum_id']; } $db->sql_freeresult($bread_crumb_ids_result); } $posts[] = array( # FOR POSTS 'forum_id' => $post['forum_id'], 'post_id' => $post['post_id'], 'post_time' => $post['post_time'], 'post_text' => $post['post_text'], #PARSING 'bbcode_bitfield' => $post['bbcode_bitfield'], 'bbcode_uid' => $post['bbcode_uid'], 'enable_bbcode' => $post['enable_bbcode'], 'enable_smilies' => $post['enable_smilies'], 'enable_magic_url' => $post['enable_magic_url'], #TOPIC DETAILS 'topic_id' => $post['topic_id'], 'topic_title' => $post['topic_title'], 'topic_views' => $post['topic_views'], 'topic_replies_real' => $post['topic_replies_real'], 'topic_type' => $post['topic_type'], 'topic_first_post_id' => $post['topic_first_post_id'], 'topic_last_post_time' => $post['topic_last_post_time'], #BREADCRUMBS 'breadcrumb_ids' => $breadcrumb_ids, #FOR USER 'user_id' => $post['user_id'], 'topic_first_poster_name' => $post['topic_first_poster_name'], 'user_colour' => $post['user_colour'], #AVATAR 'allow_avatar' => $allow_avatar, 'user_avatar' => $post['user_avatar'], 'user_avatar_type' => $post['user_avatar_type'], 'user_avatar_width' => $post['user_avatar_width'], 'user_avatar_height' => $post['user_avatar_height'], #RANKS 'allow_ranks' => $allow_ranks, 'user_rank_id' => $post['user_rank'], 'user_posts' => $post['user_posts'], #SIGNATURES 'allow_sigs' => $allow_sigs, 'user_sig' => $post['user_sig'], 'user_sig_bbcode_uid' => $post['user_sig_bbcode_uid'], 'user_sig_bbcode_bitfield' => $post['user_sig_bbcode_bitfield'], ); } $db->sql_freeresult($result); // Return it return $posts; } /* FROM HERE, you can now change whatever you want in order to suit your needs. For example, you can build the datas to however you like. OR you can change the output variables. That is where the html gets outputted. If you know php variables and html, you can modify that to your own usage. */ $anotherarray = array(2); # Get the posts $posts = get_news($anotherarray, false, 3, 0, true, sort_order_query('sticky_recent_desc')); # Build the output $output = ''; foreach($posts as $post){ #First set all the data in vars $bbcode_options = (($post['enable_bbcode']) ? OPTION_FLAG_BBCODE : 0) + (($post['enable_smilies']) ? OPTION_FLAG_SMILIES : 0) + (($post['enable_magic_url']) ? OPTION_FLAG_LINKS : 0); #Actual Post $post_text = generate_text_for_display($post['post_text'], $post['bbcode_uid'], $post['bbcode_bitfield'], $bbcode_options); $post_text = bbcode_nl2br($post_text); $post_text = smiley_text($post_text); $post_link = append_sid($phpbb_root_path . 'viewtopic.' . $phpEx, array('f' => $post['forum_id'], 't' => $post['topic_id'])) . '#p' . $post['post_id']; $post_subject = censor_text($post['topic_title']); $post_time = $user->format_date($post['post_time']); $post_replies_count = $post['topic_replies_real']; $post_views = $post['topic_views']; #Post Tracking (Is it unread or not?) $topic_tracking_info = get_complete_topic_tracking($post['forum_id'], $post['topic_id'], false); $is_this_unread = (isset($topic_tracking_info[$post['topic_id']]) && $post['topic_last_post_time'] > $topic_tracking_info[$post['topic_id']]) ? true : false; /*#Post Icons $post_icon = $icon_alt_text = $topic_type = ''; topic_status($post, $post['topic_replies_real'], $is_this_unread, $post_icon, $icon_alt_text, $topic_type); $post_icon = $user->img($post_icon, $icon_alt_text, false, '', 'src'); $icon_alt_text = $user->lang[$icon_alt_text];*/ #Breadcrumbs unset($breadcrumb_parent_urls); $breadcrumbs_index_url = '<a href="' . append_sid($phpbb_root_path) . '">Board Index</a>'; if(isset($post['breadcrumb_ids'])){ foreach($post['breadcrumb_ids'] as $name => $id){ $url = append_sid($phpbb_root_path . 'viewforum.' . $phpEx, array('f' => $id)); $breadcrumb_parent_urls .= '<strong>‹</strong><a href="' . $url . '">' . $name . '</a>'; } } #User Information if($post['allow_avatar']){ $poster_avatar = get_user_avatar($post['user_avatar'], $post['user_avatar_type'], $post['user_avatar_width'], $post['user_avatar_height']); } $poster_link = append_sid($phpbb_root_path . 'memberlist.' . $phpEx, array('mode' => 'viewprofile', 'u' => $post['user_id'])); $poster_name = get_username_string('full', $post['user_id'], $post['topic_first_poster_name'], $post['user_colour']); #Ranks if($post['allow_ranks']){ if($post['user_id'] != ANONYMOUS){ unset($user_rank_title, $user_rank_image, $user_rank_image_relative_src); get_user_rank($post['user_rank_id'], $post['user_posts'], $user_rank_title, $user_rank_image, $user_rank_image_relative_src); } } #Signatures if($post['allow_sigs']){ $user_signature = generate_text_for_display($post['user_sig'], $post['user_sig_bbcode_uid'], $post['user_sig_bbcode_bitfield'], $bbcode_options); $user_signature = bbcode_nl2br($user_signature); $user_signature = smiley_text($user_signature); } #THE BUILDING BLOCKS ARE DONE! #ITS TIME TO START PUTTING THE BLOCKS TOGETHER INTO COHERENT HTML $output .= '<hr />'; $output .= '<img src="' . $post_icon . '" alt="' . $icon_alt_text . '" title="' . $icon_alt_text . '">'; $output .= '<h2><a '; if($is_this_unread){ $output .= 'class="unread"'; } $output .= ' href="' . $post_link . '" title="' . $post_subject . '">' . $post_subject . "</a></h2>\n"; $output .= '<div class="post_info"><em>Breadcrumbs:</em> ' . $breadcrumbs_index_url . $breadcrumb_parent_urls; $output .= '<br /><em>Number of replies: ' . $post_replies_count . "</em>\n"; /*$output .= '<br /><em>Number of views from forum: ' . $post_views . "</em>\n";*/ $output .= '<h4>Posted on: ' . $post_time . "</h4></div>\n"; $output .= '<div class="user_info"><h5>Published by: ' . $poster_name . '</h5>' . '<div class="avatar">' . $poster_avatar . "</div>\n"; $output .= (!empty($user_rank_title) ? '<br />Rank Title: ' . $user_rank_title . '<br />' . $user_rank_image : false) . '</div>'; $output .= '<div class="text">' . $post_text . '</div>'; if($is_there_attachments && $post['allow_attachments']){ $output .= '<div class="attachment_info">'; foreach($post_attachment_filesize as &$filesize){ $filesize = bytesize($filesize); } unset($filesize); #Breaking reference $output .= '<em>Attachments:</em><br />Number of Attachments: ' . $num_of_attachments; for($j=key($post_attachments);$j<$num_of_attachments;$j++) { $output .= '<br /><a href="' . $post_attachments[$j] . '">' . $post_attachment_names[$j] . '</a>' . (!empty($post_attachment_comments[$j]) ? '<br />Attachment Comments: ' . $post_attachment_comments[$j] : false) . '<br />Number of Downloads: ' . $post_attachment_downloads[$j] . ' (Does not count downloads from "here")<br />Filesize: ' . $post_attachment_filesize[$j]; } $output .= '</div>'; } $output .= (!empty($user_signature) ? '<div class="signatures">' . $user_signature . '</div>' : false); $output .= '<p><a href="' . $post_link . '">Read More</a></p>'; $output .= '<hr />'; } ?> <!-- Start posts --> <?php print ($output); ?> Quote Link to comment Share on other sites More sharing options...
.josh Posted July 29, 2009 Share Posted July 29, 2009 Not going to sift through all that code, but can you clarify: when you say "So I'm wondering, is there a code for it to reduce the characters to a specific amount?" Do you mean if the total character count in the content is for example 300 chars, you want to get the first 100 chars only? substr Quote Link to comment Share on other sites More sharing options...
Peosan Posted July 29, 2009 Author Share Posted July 29, 2009 Do you mean if the total character count in the content is for example 300 chars, you want to get the first 100 chars only? substr Here, I'll give you a example of my site: http://peosan.mine.nu/sedevacante/ As you can see, the first topic is bashing through it. I want to limit it to a specific amount. Since now, I'm only showing three topics, and I want them to have a specific character amount. Get my point? :- ) Quote Link to comment Share on other sites More sharing options...
Dathremar Posted July 29, 2009 Share Posted July 29, 2009 If You mean the going out of bottom border of the page, then You need to make something like a fixed number of post per page and put them in a fixed size boxes. In the box put if the text is longer then (lets say) 100 chars then make it a link to a new page. Quote Link to comment Share on other sites More sharing options...
.josh Posted July 29, 2009 Share Posted July 29, 2009 so...like I said, you are wanting to for instance take a 300 char count post and chop it off at say, 100 chars? Did you go to the substr link I provided? It sounds like that's what you want... Quote Link to comment Share on other sites More sharing options...
Peosan Posted July 29, 2009 Author Share Posted July 29, 2009 so...like I said, you are wanting to for instance take a 300 char count post and chop it off at say, 100 chars? Did you go to the substr link I provided? It sounds like that's what you want... Yea, I checked the link, but needless to say, I'm horrible making up a valible(?) function. :x Any idea how it would go? Quote Link to comment Share on other sites More sharing options...
Peosan Posted July 29, 2009 Author Share Posted July 29, 2009 If You mean the going out of bottom border of the page, then You need to make something like a fixed number of post per page and put them in a fixed size boxes. In the box put if the text is longer then (lets say) 100 chars then make it a link to a new page. That sounds reasonable, any idea how I would go? Quote Link to comment Share on other sites More sharing options...
.josh Posted July 29, 2009 Share Posted July 29, 2009 The link tells you what needs to go where. substr(string, start, length); string = the target string start = the starting position length = how long you want it to be $content = "really long text here"; $content = substr($content,0,100); not trying to be mean, but it's not exactly rocket science... Quote Link to comment Share on other sites More sharing options...
Peosan Posted July 29, 2009 Author Share Posted July 29, 2009 It's alright, I spent additional time on the script and I found out how to do it. Here's how I did it: $output .= '<div class="text">' . substr($post_text,0,50) . '</div>'; Thanks for the assistance! :- ) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.