Author Topic: [MOD] Last topics from phpBB3 forum to index page (php)  (Read 13346 times)

0 Members and 1 Guest are viewing this topic.

Offline Joss

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • All about digital photography
[MOD] Last topics from phpBB3 forum to index page (php)
« on: October 26, 2009, 11:11:05 PM »
There is a JavaScript version, but it is not cached by 4images and parsed by search engines.
So here is php version
Demo (in middle of index page): http://gallery.imagemaster.ru/

1. Insert into index.html

Code: [Select]
<?php 
include($_SERVER['DOCUMENT_ROOT']."/rphpbb.php"); 
?>


2. Create rphpbb.php file in root (2 variants with last reply time and without it)

1. with time
Code: [Select]
<?php
$connection 
mysql_connect(localhost,"your_login","your password") or die("Service temporairly unavailable");
$db mysql_select_db("your_db_name",$connection) or die("Service temporairly unavailable");
$sql "select * from phpbb_topics order by topic_last_post_time desc limit 0,12";
$result mysql_query($sql) or die("Service temporairly unavailable");
for(
$x=1;$x<=12;$x++){

    
$row mysql_fetch_array($result);
$time_last_reply date("d.m"$row[topic_last_post_time]);
$time_from_last_reply $time_last_reply;

$minutes_from_last_reply = (int) ((time() - $row[topic_last_post_time])/60);

if ($minutes_from_last_reply 60) {
$time_from_last_reply $minutes_from_last_reply." m.";
}

if ($minutes_from_last_reply >= 60 && $minutes_from_last_reply <= 480) {
$hours_from_last_reply = (int) ($minutes_from_last_reply/60);
$time_from_last_reply $hours_from_last_reply." h.";



    echo 
"[$row[topic_replies_real]]&nbsp;<a href = \"http://www.yourforumdomain.com/viewtopic.php?f=$row[forum_id]&t=$row[topic_id]&view=unread#unread\" target = \"_blank\">$row[topic_title]</a>&nbsp;(".$time_from_last_reply.")<br>";
}
?>


2. w/o time
Code: [Select]
<?php
$connection 
mysql_connect(localhost,"your_login","your password") or die("Service temporairly unavailable");
$db mysql_select_db("your_db_name",$connection) or die("Service temporairly unavailable");
$sql "select * from phpbb_topics order by topic_last_post_time desc limit 0,12";
$result mysql_query($sql) or die("Service temporairly unavailable");
for(
$x=1;$x<=12;$x++){
    
$row mysql_fetch_array($result);
    echo 
"[$row[topic_replies_real]]&nbsp;<a href = \"http://www.yourforumdomain.com/viewtopic.php?f=$row[forum_id]&t=$row[topic_id]&view=unread#unread\" target = \"_blank\">$row[topic_title]</a><br>";
}
?>


3. Change in file the strings:

"your_login"  - to your db forum login
"your password"  - to your forum db password
"your_db_name"   - to your forum db name
www.yourforumdomain.com   - to your forum address

4. In include/constants.php must set 1 in this define:

define('EXEC_PHP_CODE', 1);

5. You can tune the code: 12 last topics displayed now, you can change this constant (change 12 in two places to another values)
Digital Photo Gallery: http://gallery.imagemaster.ru

Offline alekseyn1

  • Full Member
  • ***
  • Posts: 102
    • View Profile
    • My Project
Re: [MOD] Last topics from phpBB3 forum to index page (php)
« Reply #1 on: December 02, 2009, 09:58:41 AM »
Thanks very much for the MOD! Very useful.

I have a problem though...

the return from the script shows as:

[8] ?????, ???????????. (1 h.)
[3] ??????????? ?????? (????????) (1 h.)
[2] ???????? ????? (1 h.)
[2] ???????? ? ????????? ??? ???? (1 h.)
  • ???????? =) (1 h.)


I guess this is wrong database encoding return... any idea how to recode the database return into cp1251?

EDIT:
I have found the solution if anybody is interested:

My problem was that the database information was stored in UTF 8 and my site was displayed in Windows 1251...

here is what you need to do to correct those question marks:

in the code above find:

Code: [Select]
$db = mysql_select_db("your_db_name",$connection) or die("Service temporairly unavailable");
and insert after:

Code: [Select]
@mysql_query('set character set utf8');
then

replace

Code: [Select]
echo "[$row[topic_replies_real]]&nbsp;<a href = \"http://www.yourforumdomain.com/viewtopic.php?f=$row[forum_id]&t=$row[topic_id]&view=unread#unread\" target = \"_blank\">$row[topic_title]</a>&nbsp;(".$time_from_last_reply.")<br>";

with

Code: [Select]
    $s = "[$row[topic_replies_real]]&nbsp;<a href = \"http://www.yourforumdomain.com/viewtopic.php?f=$row[forum_id]&t=$row[topic_id]&view=unread#unread\" target = \"_blank\">$row[topic_title]</a>&nbsp;(".$time_from_last_reply.")<br>";
    echo iconv('utf-8', 'windows-1251', $s);

that did the trick for me... (sorry I presumed that you have iconv installed with your hosting provider)

in case you do not have it installed, you could use recoding functions available online... here are some samples (i did not check them but they were reported as functional)

Code: [Select]
function str_encode ($string,$to="iso-8859-9",$from="utf8") {
    if($to=="iso-8859-9" && $from=="utf8"){
        $str_array = array(
       chr(196).chr(177) => chr(253),
       chr(196).chr(176) => chr(221),
       chr(195).chr(182) => chr(246),
       chr(195).chr(150) => chr(214),
       chr(195).chr(167) => chr(231),
       chr(195).chr(135) => chr(199),
       chr(197).chr(159) => chr(254),
       chr(197).chr(158) => chr(222),
       chr(196).chr(159) => chr(240),
       chr(196).chr(158) => chr(208),
       chr(195).chr(188) => chr(252),
       chr(195).chr(156) => chr(220)
       );
       return str_replace(array_keys($str_array), array_values($str_array), $string);
   
    }   
    return $string;
}

function FixEncoding($x){
  if(mb_detect_encoding($x)=='UTF-8'){
    return $x;
  }else{
    return utf8_encode($x);
  }
}

function cp1251_to_utf8($s)
{
           $c209 = chr(209); $c208 = chr(208); $c129 = chr(129);
           for($i=0; $i<strlen($s); $i++)    {
               $c=ord($s[$i]);
               if ($c>=192 and $c<=239) $t.=$c208.chr($c-48);
               elseif ($c>239) $t.=$c209.chr($c-112);
               elseif ($c==184) $t.=$c209.$c209;
               elseif ($c==168)    $t.=$c208.$c129;
               else $t.=$s[$i];
           }
           return $t;
       }

function utf8_to_cp1251($s)
        {
            for ($c=0;$c<strlen($s);$c++)
            {
               $i=ord($s[$c]);
               if ($i<=127) $out.=$s[$c];
                   if ($byte2){
                       $new_c2=($c1&3)*64+($i&63);
                       $new_c1=($c1>>2)&5;
                       $new_i=$new_c1*256+$new_c2;
                   if ($new_i==1025){
                       $out_i=168;
                   } else {
                       if ($new_i==1105){
                           $out_i=184;
                       } else {
                           $out_i=$new_i-848;
                       }
                   }
                   $out.=chr($out_i);
                   $byte2=false;
                   }
               if (($i>>5)==6) {
                   $c1=$i;
                   $byte2=true;
               }
            }
            return $out;
}


so if you will use functions then your last line will look like this:

Code: [Select]
echo utf8_to_cp1251($s);
I hope this will save lots of research for people )))
« Last Edit: December 15, 2009, 11:00:54 AM by alekseyn1 »