Últimos mensajes en el índice del foro y en páginas externas Tema Solucionado
Publicado: 14 Oct 2014, 19:32
Últimos mensajes en el índice del foro y en páginas externas
La finalidad de esta guía, es compartir algunos códigos con los cuales poder tener una tabla con los últimos posts del foro.Además colocaré una serie de códigos para que puedan hacer lo mismo en sus páginas externas (portales, wordpress, joomla, etc...).
En realidad es muy sencillo y útil. Espero les sirva.
1) Tabla con los últimos 5 temas en el índice del foro.
Esto se vería así en prosilver:
Y así en subsilver2:
Para lograr esto, debes hacer las siguientes modificaciones:
- En index.php, Buscar:
Código: Seleccionar todo
// Start session management $user->session_begin(); $auth->acl($user->data); $user->setup('viewforum'); display_forums('', $config['load_moderators']);
- Agregar después:
** nota: En la siguiente línea pueden modificar el 5, por otro número, para que les muestre 8 temas, por ejemplo.
Código: Seleccionar todo
// bloque últimos posts - mitch - phpBB-Es include($phpbb_root_path . 'includes/bbcode.' . $phpEx); 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'); } $out_where = ''; if ($size_gen_id > 0) { $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)) { $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; } // Número de últimos posts a mostrar (editar el 5 si así lo deseas): $search_limit = 5; $posts_ary = array( 'SELECT' => 'p.*, t.*, u.username, u.user_colour', 'FROM' => array( POSTS_TABLE => 'p', ), 'LEFT_JOIN' => array( array( 'FROM' => array(USERS_TABLE => 'u'), 'ON' => 'u.user_id = p.poster_id' ), array( 'FROM' => array(TOPICS_TABLE => 't'), 'ON' => 'p.topic_id = t.topic_id' ), ), 'WHERE' => $db->sql_in_set('t.forum_id', array_keys($auth->acl_getf('f_read', true))) . ' AND t.topic_status <> ' . ITEM_MOVED . ' AND t.topic_approved = 1', 'ORDER_BY' => 'p.post_id DESC', ); $posts = $db->sql_build_query('SELECT', $posts_ary); $posts_result = $db->sql_query_limit($posts, $search_limit); while ($posts_row = $db->sql_fetchrow($posts_result)) { $topic_title = $posts_row['topic_title']; $topic_title = censor_text($topic_title); $post_author = get_username_string('full', $posts_row['poster_id'], $posts_row['username'], $posts_row['user_colour']); $post_date = $user->format_date($posts_row['post_time']); $post_link = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "p=" . $posts_row['post_id'] . "#p" . $posts_row['post_id']); $post_text = nl2br($posts_row['post_text']); $bbcode = new bbcode(base64_encode($bbcode_bitfield)); $bbcode->bbcode_second_pass($post_text, $posts_row['bbcode_uid'], $posts_row['bbcode_bitfield']); $post_text = smiley_text($post_text); $template->assign_block_vars('last_topics', array( 'TOPIC_TITLE' => censor_text($topic_title), 'POST_AUTHOR' => $post_author, 'POST_DATE' => $post_date, 'POST_LINK' => $post_link, 'POST_TEXT' => censor_text($post_text), )); } // Fin bloque últimos posts - mitch - phpBB-Es
Código: Seleccionar todo
// Número de últimos posts a mostrar (editar el 5 si así lo deseas): $search_limit = 5;
- Si tu estilo es prosilver (o está basado en él), abrir /styles/prosilver/template/index_body.html y Buscar:
Agregar ANTES:Código: Seleccionar todo
<!-- INCLUDE forumlist_body.html -->
Código: Seleccionar todo
<div class="forabg"> <div class="inner"><span class="corners-top"><span></span></span> <ul class="topiclist"> <li class="header"> <dl class="icon"> <dt>Últimos Posts</dt> </dl> </li> </ul> <ul class="topiclist forums"> <li class="row"> <dl> <dt> <!-- BEGIN last_topics --> <a href="{last_topics.POST_LINK}">{LAST_POST_IMG}</a> <a href="{last_topics.POST_LINK}" class="forumtitle">{last_topics.TOPIC_TITLE}</a> por {last_topics.POST_AUTHOR} - {last_topics.POST_DATE}<br /> <!-- END last_topics --> </dt> </dl> </li> </ul> <span class="corners-bottom"><span></span></span></div> </div>
- Si tu estilo es subsilver2 (o está basado en él), abrir /styles/subsilver2/template/index_body.html y Buscar:
Agregar ANTES:Código: Seleccionar todo
<!-- INCLUDE forumlist_body.html -->
nota: Si en la palabra Últimos, aparece un carácter extraño en la "U", es por el acento. Deben ir en su editor de texto a Guardar como... y guardar el archivo en formato UTF8Código: Seleccionar todo
<table class="tablebg" width="100%" cellspacing="1"> <tr> <td class="cat"><h4>Últimos Posts</h4></td> </tr> <tr> <td class="row1"><p class="genmed"> <!-- BEGIN last_topics --> <a href="{last_topics.POST_LINK}">{LAST_POST_IMG}</a> <a href="{last_topics.POST_LINK}" ><b>{last_topics.TOPIC_TITLE}</b></a> por {last_topics.POST_AUTHOR} - {last_topics.POST_DATE}<br> <!-- END last_topics --> </p></td> </tr> </table> <br>
- Limpiar caché.
2) Tabla de últimos posts en páginas externas al foro.
Con esto, me refiero a lo siguiente; supongamos que el foro se encuentra en la carpeta "/foro/" (http://www.tupagina.com/foro/). Pero aparte del foro, tienes un portal, el que se encuentra en la raíz, en home.php (siguiendo con el ejemplo, estaría en http://www.tupagina.com/home.php). Para lograr mostrar en este portal externo, los posts del foro, ya no nos sirven los códigos antes mencionados y tendremos que usar los que muestro a continuación.
Y dejaré 3 alternativas distintas... la primera, para mostrar los últimos mensajes de todo el foro. La segunda, para mostrar los ultimos mensajes de un foro en particular (que tú vas a especificar... por ejemplo el foro de noticias de tu web). Y por último, un código que te servirá para mostrar las respuestas de un tema específico, con su contenido.
- Ejemplo 1: Últimos 5 posts de todo el foro
en tu archivo .php externo, deberás agregar estas líneas:De este código, debes editar en esta línea:Código: Seleccionar todo
<?php define('IN_PHPBB', true); $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './phpBB3/'; $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('viewforum'); /* 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; } // Número de últimos posts a mostrar: $search_limit = 5; $posts_ary = array( 'SELECT' => 'p.*, t.*, u.username, u.user_colour', 'FROM' => array( POSTS_TABLE => 'p', ), 'LEFT_JOIN' => array( array( 'FROM' => array(USERS_TABLE => 'u'), 'ON' => 'u.user_id = p.poster_id' ), array( 'FROM' => array(TOPICS_TABLE => 't'), 'ON' => 'p.topic_id = t.topic_id' ), ), 'WHERE' => $db->sql_in_set('t.forum_id', array_keys($auth->acl_getf('f_read', true))) . ' AND t.topic_status <> ' . ITEM_MOVED . ' AND t.topic_approved = 1', 'ORDER_BY' => 'p.post_id DESC', ); $posts = $db->sql_build_query('SELECT', $posts_ary); $posts_result = $db->sql_query_limit($posts, $search_limit); while ($posts_row = $db->sql_fetchrow($posts_result)) { $topic_title = $posts_row['topic_title']; $topic_title = censor_text($topic_title); $post_author = get_username_string('full', $posts_row['poster_id'], $posts_row['username'], $posts_row['user_colour']); $post_date = $user->format_date($posts_row['post_time']); $post_link = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "p=" . $posts_row['post_id'] . "#p" . $posts_row['post_id']); $post_text = nl2br($posts_row['post_text']); $bbcode = new bbcode(base64_encode($bbcode_bitfield)); $bbcode->bbcode_second_pass($post_text, $posts_row['bbcode_uid'], $posts_row['bbcode_bitfield']); $post_text = smiley_text($post_text); echo "<b>!!</b> <a href=\"$post_link\">$topic_title</a> por $post_author - $post_date<br>"; } ?>
editas el ./phpBB3/ por la carpeta en donde se encuentre tu foro... puede ser ./foro/. por ejemplo.Código: Seleccionar todo
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './phpBB3/';
Si quieres que en vez de 5 temas, muestre 8, u otro valor, lo editas el "5" en esta otra línea:Y casi al final del código, esta línea:Código: Seleccionar todo
// Número de últimos posts a mostrar: $search_limit = 5;
es la que muestra finalmente el resultado... la puedes modificar como quieras, cuidando de no modificar los $valores y las comillas iniciales y finales.Código: Seleccionar todo
echo "<b>!!</b> <a href=\"$post_link\">$topic_title</a> por $post_author - $post_date<br>";
- Ejemplo 2: Últimos posts de un foro en específico
El código a agregar en tu .php sería este:Debes editar la ruta de tu phpBB tal como lo indiqué en el código anterior. Lo mismo, si quieres editar el número de temas a mostrar, en la misma línea.Código: Seleccionar todo
<?php define('IN_PHPBB', true); $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './phpBB3/'; $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('viewforum'); /* 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; } // Número de últimos posts a mostrar: $search_limit = 5; // ID del foro específico. Si deseas más de un foro, separar por "comas". $forum_id = array(3); $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_limit); while ($topics_row = $db->sql_fetchrow($topics_result)) { $topic_title = $topics_row['topic_title']; $topic_title = censor_text($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", "p=" . $topics_row['topic_last_post_id'] . "#" . $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", "t=" . $topics_row['topic_id']); echo "<b>!!</b> <a href=\"$topic_link\">$topic_title</a> por $topic_author - $topic_date<br>"; } ?>
Para editar el foro específico, lo haces en esta línea (cambiar el número 3):Ese 3, es la número ID del foro. Si te fijas, al entrar en un foro, la URL mostrada en el navegador es de este tipo: http://ejemplo.com/phpBB3/viewforum.php?f=3, ese número, es la ID de tu foro.Código: Seleccionar todo
// ID del foro específico. Si deseas más de un foro, separar por "comas". $forum_id = array(3);
Y tal como dice allí, puedes colocar más de un foro en específico, separado por comas. - Ejemplo 3: Últimos posts (respuestas) de un tema en específico.
Con este código, mostraremos las respuestas (con su contenido) que se han dejado en un tema que nosotros especificamos. Se verán los bbcodes y todo lo demás
El código es el siguiente:
Y se aplica lo mismo que los anteriores; debes editar la ruta del foro, y puedes editar el número de temas a mostrar.Código: Seleccionar todo
<?php define('IN_PHPBB', true); $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './phpBB3/'; $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('viewforum'); /* 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; } // Número de últimos posts a mostrar: $search_limit = 5; // ID del post específico. Si deseas más de un tema, separar por "comas". $topic_id = array(1); $topic_id_where = create_where_clauses($topic_id, 'topic'); $posts_ary = array( 'SELECT' => 'p.*, t.*, u.username, u.user_colour', 'FROM' => array( POSTS_TABLE => 'p', ), 'LEFT_JOIN' => array( array( 'FROM' => array(USERS_TABLE => 'u'), 'ON' => 'u.user_id = p.poster_id' ), array( 'FROM' => array(TOPICS_TABLE => 't'), 'ON' => 'p.topic_id = t.topic_id' ), ), 'WHERE' => str_replace( array('WHERE ', 'topic_id'), array('', 't.topic_id'), $topic_id_where) . ' AND t.topic_status <> ' . ITEM_MOVED . ' AND t.topic_approved = 1', 'ORDER_BY' => 'p.post_id DESC', ); $posts = $db->sql_build_query('SELECT', $posts_ary); $posts_result = $db->sql_query_limit($posts, $search_limit); while ($posts_row = $db->sql_fetchrow($posts_result)) { $topic_title = $posts_row['topic_title']; $topic_title = censor_text($topic_title); $post_author = get_username_string('full', $posts_row['poster_id'], $posts_row['username'], $posts_row['user_colour']); $post_date = $user->format_date($posts_row['post_time']); $post_link = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "p=" . $posts_row['post_id'] . "#p" . $posts_row['post_id']); $post_text = nl2br($posts_row['post_text']); $bbcode = new bbcode(base64_encode($bbcode_bitfield)); $bbcode->bbcode_second_pass($post_text, $posts_row['bbcode_uid'], $posts_row['bbcode_bitfield']); $post_text = smiley_text($post_text); echo "<b>!!</b> <a href=\"$post_link\">$topic_title</a> por $post_author - $post_date<br>$post_text<hr>"; } ?>
La ID de tema (o los temas), se colocan en la siguiente línea (cambiar el número 1):y la ID del tema, lo obtienes fijandote en este número de la URL del tema: http://ejemplo.com/phpBB3/viewtopic.php?f=3&t=4.Código: Seleccionar todo
// ID del post específico. Si deseas más de un tema, separar por "comas". $topic_id = array(1);
Espero les sea de utilidad. Las dudas en el foro oportuno por favor, acá en phpBB-Es.
Fuente: http://blog.phpbb.com/2009/11/09/how-to ... nal-pages/
Últimos posts en el índice, y modificación del original por mitch