Seite 1 von 5

"Similar Topics" für Content-Seite

Verfasst: 08.06.2010 17:30
von uwe.ha
Hallo zusammen,

wie kann ich den "Similar Topics" Mod (viewtopic.php?t=182408) so anpassen, dass ihn auch auf einer Content-Seite (außerhalb des phpBB-Ordners) nutzen kann?

Beispiel:
Ich habe ein Forum über Blumen.
In einem redaktionellen Bericht im Content-Bereich über "Rosen" (/rosen.php) möchte ich "similar Topics" (ähnliche Beiträge) aus dem Forum zum Thema "Rosen" anzeigen.

Wie gehe ich nun vor?

Ich habe mir folgendes überlegt:
Da ich von außerhalb des phpBB-Ordners - soweit ich weiß - nicht auf die Funktionen von phpBB zugreifen kann, muss ich also eine "similar-Datei" innerhakb des phpBB-Ordners erstellen. Dazu habe ich mit einen Ordner /phpBB/similar erstellt.
In diesem Ordner muss ich dann für jede (?) similar-Suche eine eigene Datei erstellen? ... also z.B. eine /phpBB/similar/similar-rosen.php, die ich dann per <include> in die rosen.php einfügen kann, oder?

Ich habe - mit diversen Codefragmenten aus der Instalationsanleitung des Similar-Mods - mal so eine /phpBB/similar/similar-rosen.php erstellt ... die natürlich nicht funktioniert; ich sehe zwar den html-Teil, dieser wird aber nicht mit Inhalten aus der Abfrage gefüllt.
Hier mal deren Inhalt, damit ihr es etwas nachverfolgen könnt:

Code: Alles auswählen

<?php

/**
* @ignore
*/
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/functions_display.' . $phpEx);


// Begin similar topics
$sql_array = array(
	'SELECT'	=> 'f.forum_id, f.forum_name, t.topic_id, t.topic_title, u.user_id, u.username, u.user_colour, t.topic_replies',

	'FROM'		=> array(
		TOPICS_TABLE	=> 't',
	),

	'LEFT_JOIN'	=> array(
		array(
			'FROM'	=>	array(USERS_TABLE	=> 'u'),
			'ON'	=> 'u.user_id = t.topic_poster'
	),
		array(
			'FROM'	=>	array(FORUMS_TABLE	=> 'f'),
			'ON'	=> 'f.forum_id = t.forum_id'
		),
	),

	'WHERE'		=> 'topic_title' <> '%rosen%',

	'GROUP_BY'	=> 't.topic_id',

	'ORDER_BY'	=> 't.topic_last_post_time DESC',
);
$sql = $db->sql_build_query('SELECT', $sql_array);
if ($result = $db->sql_query_limit($sql, 10))
{
	while($similar = $db->sql_fetchrow($result))
	{
		if ($auth->acl_get('f_read', $similar['forum_id']))
		{
			$similar_forum_url	= append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=" . $similar['forum_id']);
			$similar_topic_url	= append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=" . $similar['forum_id'] . '&t=' . $similar['topic_id']);
			$similar_user		= get_username_string('full', $similar['user_id'], $similar['username'], $similar['user_colour'], $similar['username']);

			$template->assign_block_vars('similar', array(
				'TOPIC_TITLE'			=> $similar['topic_title'],
				'U_TOPIC'				=> $similar_topic_url,
				'REPLIES'				=> $similar['topic_replies'],
				'USER'					=> $similar_user,
				'U_FORUM'				=> $similar_forum_url,
				'FORUM'					=> $similar['forum_name'])
			);
		}
	}
}
// End Similar Topics
?>

<!-- IF .similar -->
<br />
	<table class="tablebg" width="100%" cellspacing="1" cellpadding="0" style="margin-top: 5px;">
	<tr>
		<th colspan="3" align="left">{L_SIMILAR_TOPICS}</th>
	</tr>
<!-- BEGIN similar -->
	<tr><td class="row1">
				<a href="{similar.U_TOPIC}" class="topictitle">{similar.TOPIC_TITLE}</a></td>
				<td class="row2">{L_FORUM}: <a href="{similar.U_FORUM}">{similar.FORUM}</a></td>
				<td class="row1">{L_REPLIES}: {similar.REPLIES}</td>
			
	</tr><!-- END similar -->
	</table>

<br />
<!-- ENDIF -->
Wie müsste ich vorgehen, damit es funktioniert?

DANKE!
Uwe

Re: "Similar Topics" für Content-Seite

Verfasst: 08.06.2010 17:59
von AmigaLink

Re: "Similar Topics" für Content-Seite

Verfasst: 08.06.2010 19:07
von uwe.ha
AmigaLink hat geschrieben:Schau dir das mal an: http://www.phpbb.com/kb/article/add-a-n ... -to-phpbb/
Super, Danke! Da klappt soweit.
Jetzt ist nur noch die Abfrage das Problem.

Im Funktionsaufruf von Similar-Topic:

Code: Alles auswählen

// Begin similar topics
$sql_array = array(
	'SELECT'	=> 'f.forum_id, f.forum_name, t.topic_id, t.topic_title, u.user_id, u.username, u.user_colour, t.topic_replies',

	'FROM'		=> array(
		TOPICS_TABLE	=> 't',
	),

	'LEFT_JOIN'	=> array(
		array(
			'FROM'	=>	array(USERS_TABLE	=> 'u'),
			'ON'	=> 'u.user_id = t.topic_poster'
	),
		array(
			'FROM'	=>	array(FORUMS_TABLE	=> 'f'),
			'ON'	=> 'f.forum_id = t.forum_id'
		),
	),

	'WHERE'		=> "MATCH (t.topic_title) AGAINST ('" . $db->sql_escape($topic_data['topic_title']) . "' ) >= 0.5
		AND t.topic_status <> " . ITEM_MOVED . '
		AND t.topic_id <> ' . (int) $topic_data['topic_id'],

	'GROUP_BY'	=> 't.topic_id',

	'ORDER_BY'	=> 't.topic_last_post_time DESC',
);
$sql = $db->sql_build_query('SELECT', $sql_array);
if ($result = $db->sql_query_limit($sql, 5))
{
	while($similar = $db->sql_fetchrow($result))
	{
		if ($auth->acl_get('f_read', $similar['forum_id']))
		{
			$similar_forum_url	= append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=" . $similar['forum_id']);
			$similar_topic_url	= append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=" . $similar['forum_id'] . '&t=' . $similar['topic_id']);
			$similar_user		= get_username_string('full', $similar['user_id'], $similar['username'], $similar['user_colour'], $similar['username']);

			$template->assign_block_vars('similar', array(
				'TOPIC_TITLE'			=> $similar['topic_title'],
				'U_TOPIC'				=> $similar_topic_url,
				'REPLIES'				=> $similar['topic_replies'],
				'USER'					=> $similar_user,
				'U_FORUM'				=> $similar_forum_url,
				'FORUM'					=> $similar['forum_name'])
			);
		}
	}
}
// End Similar Topics
ist wohl dieses

Code: Alles auswählen

	'WHERE'		=> "MATCH (t.topic_title) AGAINST ('" . $db->sql_escape($topic_data['topic_title']) . "' ) >= 0.5
		AND t.topic_status <> " . ITEM_MOVED . '
		AND t.topic_id <> ' . (int) $topic_data['topic_id'],
für den Abgleich von topic_title und den anderen topic_title in der db zuständig.

Wenn ich nun nicht title gegen title "matchen" will, sondern einfach nur "keyword" in "title" suchen will, wie heißt das dann (ich habe zwar schon selbst "ausprobiert", bekomme aber die korrekte Schreibweise nicht hin)?
WHERE => 't.topic_title' enthält 'keyword' ... oder so?

DANKE!

Re: "Similar Topics" für Content-Seite

Verfasst: 09.06.2010 00:59
von AmigaLink
Definier einfach (vor der SQL-Abfrage) eine variable mit dem Inhalt nach dem gesucht werden soll

Code: Alles auswählen

$keyword = 'Dein Keyword';
und ersetze in der SQL-Abfrage noch $topic_data['topic_title'] gegen die von dir definierte Variable.

Code: Alles auswählen

'WHERE'      => "MATCH (t.topic_title) AGAINST ('" . $db->sql_escape($keyword) . "' ) >= 0.5
// Edit

Hoppala, das hätte ich fast übersehen.

Code: Alles auswählen

      AND t.topic_id <> ' . (int) $topic_data['topic_id']
kann bzw. sollte noch weg.

also:

Code: Alles auswählen

   'WHERE'      => "MATCH (t.topic_title) AGAINST ('" . $db->sql_escape($keyword) . "' ) >= 0.5
      AND t.topic_status <> " . ITEM_MOVED . ',

Re: "Similar Topics" für Content-Seite

Verfasst: 09.06.2010 11:54
von uwe.ha
Hallo AmigaLink,

mit dem "keyword" ist soweit klar; meine similar-rosen.php sieht nun so aus:

Code: Alles auswählen

<?php
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);


// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

$template->set_filenames(array(
    'body' => 'similar-content.html',
));

make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));
page_footer();

// Begin similar topics
$keyword = 'rosen';
$sql_array = array(
	'SELECT'	=> 'f.forum_id, f.forum_name, t.topic_id, t.topic_title, u.user_id, u.username, u.user_colour, t.topic_replies',

	'FROM'		=> array(
		TOPICS_TABLE	=> 't',
	),

	'LEFT_JOIN'	=> array(
		array(
			'FROM'	=>	array(USERS_TABLE	=> 'u'),
			'ON'	=> 'u.user_id = t.topic_poster'
	),
		array(
			'FROM'	=>	array(FORUMS_TABLE	=> 'f'),
			'ON'	=> 'f.forum_id = t.forum_id'
		),
	),

    'WHERE'      => "MATCH (t.topic_title) AGAINST ('" . $db->sql_escape($keyword) . "') >= 0.5
        AND t.topic_status <> " . ITEM_MOVED . ',
        AND t.topic_id <> ' . (int) $topic_data['topic_id'],

	'GROUP_BY'	=> 't.topic_id',

	'ORDER_BY'	=> 't.topic_last_post_time DESC',
);
$sql = $db->sql_build_query('SELECT', $sql_array);
if ($result = $db->sql_query_limit($sql, 5))
{
	while($similar = $db->sql_fetchrow($result))
	{
		if ($auth->acl_get('f_read', $similar['forum_id']))
		{
			$similar_forum_url	= append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=" . $similar['forum_id']);
			$similar_topic_url	= append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=" . $similar['forum_id'] . '&t=' . $similar['topic_id']);
			$similar_user		= get_username_string('full', $similar['user_id'], $similar['username'], $similar['user_colour'], $similar['username']);

			$template->assign_block_vars('similar', array(
				'TOPIC_TITLE'			=> $similar['topic_title'],
				'U_TOPIC'				=> $similar_topic_url,
				'REPLIES'				=> $similar['topic_replies'],
				'USER'					=> $similar_user,
				'U_FORUM'				=> $similar_forum_url,
				'FORUM'					=> $similar['forum_name'])
			);
		}
	}
}
// End Similar Topics
?>
AmigaLink hat geschrieben:
Hoppala, das hätte ich fast übersehen.

Code: Alles auswählen

      AND t.topic_id <> ' . (int) $topic_data['topic_id']
kann bzw. sollte noch weg.
Wenn ich das weg lasse, kommt folgende Fehlermeldung:
"Parse error: syntax error, unexpected T_STRING, expecting ')' in /www/htdocs/xxx/phpBB/similar-rosen.php on line 43"

Diese Zeile 43 ist dann:

Code: Alles auswählen

	'GROUP_BY'	=> 't.topic_id',
Wenn ich es drinnen lasse, erscheint zwar die Seite (overall_header + similar-content-template + overall_footer), nur werden keine Ergebnisse ausgegeben :cry:

Was habe ich falsch gemacht?

DANKE!

Re: "Similar Topics" für Content-Seite

Verfasst: 09.06.2010 13:00
von AmigaLink
Fehler meinerseits, so müsste es gehen:

Code: Alles auswählen

<?php
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);


// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

$template->set_filenames(array(
    'body' => 'similar-content.html',
));

make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));
page_footer();

// Begin similar topics
$keyword = 'rosen';
$sql_array = array(
   'SELECT'   => 'f.forum_id, f.forum_name, t.topic_id, t.topic_title, u.user_id, u.username, u.user_colour, t.topic_replies',

   'FROM'      => array(
      TOPICS_TABLE   => 't',
   ),

   'LEFT_JOIN'   => array(
      array(
         'FROM'   =>   array(USERS_TABLE   => 'u'),
         'ON'   => 'u.user_id = t.topic_poster'
   ),
      array(
         'FROM'   =>   array(FORUMS_TABLE   => 'f'),
         'ON'   => 'f.forum_id = t.forum_id'
      ),
   ),

    'WHERE'      => "MATCH (t.topic_title) AGAINST ('" . $db->sql_escape($keyword) . "') >= 0.5
        AND t.topic_status <> " . ITEM_MOVED,

   'GROUP_BY'   => 't.topic_id',

   'ORDER_BY'   => 't.topic_last_post_time DESC',
);
$sql = $db->sql_build_query('SELECT', $sql_array);
if ($result = $db->sql_query_limit($sql, 5))
{
   while($similar = $db->sql_fetchrow($result))
   {
      if ($auth->acl_get('f_read', $similar['forum_id']))
      {
         $similar_forum_url   = append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=" . $similar['forum_id']);
         $similar_topic_url   = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=" . $similar['forum_id'] . '&t=' . $similar['topic_id']);
         $similar_user      = get_username_string('full', $similar['user_id'], $similar['username'], $similar['user_colour'], $similar['username']);

         $template->assign_block_vars('similar', array(
            'TOPIC_TITLE'         => $similar['topic_title'],
            'U_TOPIC'            => $similar_topic_url,
            'REPLIES'            => $similar['topic_replies'],
            'USER'               => $similar_user,
            'U_FORUM'            => $similar_forum_url,
            'FORUM'               => $similar['forum_name'])
         );
      }
   }
}
// End Similar Topics
?>

Re: "Similar Topics" für Content-Seite

Verfasst: 09.06.2010 14:57
von uwe.ha
Hallo Markus,

vielen Dank ... es wird zwar - neben header und footer - die Tabelle des similar-templates ausgegeben, diese ist aber leer:

Code: Alles auswählen

	<table class="tablebg" width="100%" cellspacing="1" cellpadding="0" style="margin-top:5px">
	<tr>
		<th colspan="3" align="left">Ähnliche Themen</th>
	</tr>
	<tr><td class="row1"><a href="" class="topictitle"></a></td>

		<td class="row2">Forum: <a href=""></a></td>
		<td class="row1">Antworten: </td>
	</tr>
	</table>
Das template sieht so aus:

Code: Alles auswählen

<!-- INCLUDE overall_header.html -->
	<table class="tablebg" width="100%" cellspacing="1" cellpadding="0" style="margin-top:5px">
	<tr>
		<th colspan="3" align="left">{L_SIMILAR_TOPICS}</th>
	</tr>
	<tr><td class="row1"><a href="{similar.U_TOPIC}" class="topictitle">{similar.TOPIC_TITLE}</a></td>
		<td class="row2">{L_FORUM}: <a href="{similar.U_FORUM}">{similar.FORUM}</a></td>
		<td class="row1">{L_REPLIES}: {similar.REPLIES}</td>
	</tr>
	</table>
<!-- INCLUDE jumpbox.html -->
<!-- INCLUDE overall_footer.html -->
Hast du noch eine Idee?

DANKE!

Re: "Similar Topics" für Content-Seite

Verfasst: 09.06.2010 15:39
von AmigaLink
Jo, da fehlt der Switch der dem Template-System sagt welcher Bereich zum einfügen der Daten genutzt werden soll.

Code: Alles auswählen

<!-- INCLUDE overall_header.html -->
   <table class="tablebg" width="100%" cellspacing="1" cellpadding="0" style="margin-top:5px">
   <tr>
      <th colspan="3" align="left">{L_SIMILAR_TOPICS}</th>
   </tr>
   <!-- BEGIN similar -->
   <tr><td class="row1"><a href="{similar.U_TOPIC}" class="topictitle">{similar.TOPIC_TITLE}</a></td>
      <td class="row2">{L_FORUM}: <a href="{similar.U_FORUM}">{similar.FORUM}</a></td>
      <td class="row1">{L_REPLIES}: {similar.REPLIES}</td>
   </tr>
   <!-- END similar -->
   </table>
<!-- INCLUDE jumpbox.html -->
<!-- INCLUDE overall_footer.html -->

Re: "Similar Topics" für Content-Seite

Verfasst: 09.06.2010 15:53
von uwe.ha
Hi ;-)

wenn ich den mit rein mache, wird der ganze Bereich (die ganze tablerow) zwischen dem Switch gar nicht angezeigt.
Auszug aus Quelltext:

Code: Alles auswählen

	<table class="tablebg" width="100%" cellspacing="1" cellpadding="0" style="margin-top:5px">
	<tr>
		<th colspan="3" align="left">Ähnliche Themen</th>
	</tr>
  
	</table>
:-?

Re: "Similar Topics" für Content-Seite

Verfasst: 09.06.2010 16:15
von AmigaLink
Dann gibt die Abfrage keine Daten zurück. :(

Hast du mal ein anderes Keyword ausprobiert?
Gib mal einen Titel an von dem du genau weisst das ein entsprechender Thread existiert. Wenn der dann nicht gefunden wird, stimmt noch etwas mit der abfrage nicht.