Scheduled PSN downtime tomorrow in Back Page News

Need some help with a script.


72 replies to this topic - - - - -

#61 -Alex-

    Noob Hunter

  • 2,551 posts
  • Joined: 08-August 06
  • Location: Liverpool / Amsterdam

Posted 22 February 2012 - 05:42

View PostFunRoomSite, on 22 February 2012 - 01:50, said:

Ok so here is the problem. So I use bluehost for my hosting, and I installed wordpress on my bluehost account. However, to edit my wordpress I don't even have to log into my bluehost. Is this going to be a problem? Can I still put this script like it is on wordpress? Because I don't just want a blank page with these numbers, I want to keep it looking like the rest of my site.
No problem... the code just needs wrapping in a Widget. Copy this into a file called leaderboard.php

<?php

   /*
   Plugin Name: Leaderboard
   Description: Dominion leaderboard
   Author: Alex Jones
   Version: 1.0.0
   Author URI: mailto:alexjones@myneowin.net
   */

   function widget_leaderboard()
   {
      echo '<table border="1" cellspacing="0"><th>#</th><th>Username</th><th><a href="' . $_SERVER['PHP_SELF'] . '?sort=elo">ELO</a></th><th><a href="' . $_SERVER['PHP_SELF'] . '?sort=wins">Wins</a></th><th><a href="' . $_SERVER['PHP_SELF'] . '?sort=losses">Losses</a></th><th><a href="' . $_SERVER['PHP_SELF'] . '?sort=ratio">Ratio</a></th>';

      $i = 0;
      $fh = fopen('results.txt', 'r');
      while($line = fgets($fh))
      {
         // Remove any lines with no Dominion results
         if(strpos($line, 'Dominion: ') !== FALSE)
         {
            preg_match('/Dominion: (.*)L]/', $line, $scores);
            // For the username, sometimes the line has '(Now:Bronze, S1:Silver)', sometimes not
            $score[$i]['username'] = (substr($line, 0, strpos($line, ' (Now:')) ?: substr($line, 0, strpos($line, ', ')));
            $score[$i]['elo'] = substr($scores[1], 0, strpos($scores[1], ' ['));
            $score[$i]['wins'] = substr($scores[1], strlen($score[$i]['elo'])+2, strpos($scores[1], 'W/')-strlen($score[$i]['elo'])-2);
            $score[$i]['losses'] = substr($scores[1], strlen($score[$i]['elo'])+strlen($score[$i]['wins'])+4);
            $score[$i]['ratio'] = round($score[$i]['wins']/$score[$i]['losses'], 2);
            $i++;
         }
      }
      fclose($fh);

      function mysort($a, $b)
      {
         // Define the default sort here (currently 'elo')
         $sort = ($_GET['sort'] != 'elo' && $_GET['sort'] != 'wins' && $_GET['sort'] != 'losses' && $_GET['sort'] != 'ratio') ? 'elo' : $_GET['sort'];
         return strnatcmp($b[$sort], $a[$sort]);
      }

      usort($score, 'mysort');

      $i = 1;
      foreach($score as $score)
      {
         echo '<tr><td>' . $i . '</td><td>' . $score['username'] . '</td><td>' . $score['elo'] . '</td><td>' . $score['wins'] . '</td><td>' . $score['losses'] . '</td><td>' . $score['ratio'] . '</td></tr>';
         $i++;
      }

      echo '</table>';
   }

   function leaderboard_init()
   {
      register_sidebar_widget(__('Leaderboard'), 'widget_leaderboard');
   }

   add_action("plugins_loaded", "leaderboard_init");

?>

Then upload it to /public_html/wp-content/plugins

Next go to your WP admin dashboard, click on Plugins, and click Activate on the new 'Leaderboard' plugin.

Now go to Appearance > Widgets, and drag the new widget into (e.g.) your sidebar.

And now you'll have something like this:

Attached Image: Dominate Dominion leaderboard v3.png

Font sizes can be fiddled with afterwards. If you get a load of errors like 'fopen failed to open results.txt/fgets expects 2 parameters/blah blah blah', then you just need to change the path to results.txt in: $fh = fopen('results.txt', 'r');

Finally, I haven't just put my 'plugin author info' there to be anal - it's required by WP, otherwise the plugin won't show up.

As usual, any issues and we're here to help! :)


#62 Xoligy

    Resident Fanatic

  • 702 posts
  • Joined: 09-May 10

Posted 22 February 2012 - 16:38

Nice work Alex, though your border isnt showing so makes it hard to read =P
<---eyes like a hawk! lol

#63 FunRoomSite

    Resident Elite

  • 1,314 posts
  • Joined: 07-May 06

Posted 23 February 2012 - 00:06

You're awesome Alex. But when I clicked activate I got this error "Parse error: syntax error, unexpected ':' in /home2/dominau4/public_html/wordpress/wp-content/plugins/leaderboard.php on line 24".

#64 -Alex-

    Noob Hunter

  • 2,551 posts
  • Joined: 08-August 06
  • Location: Liverpool / Amsterdam

Posted 23 February 2012 - 03:55

View PostFunRoomSite, on 23 February 2012 - 00:06, said:

You're awesome Alex. But when I clicked activate I got this error "Parse error: syntax error, unexpected ':' in /home2/dominau4/public_html/wordpress/wp-content/plugins/leaderboard.php on line 24".
Hey,

Nice easy error to fix. Quite simply, Bluehost must be running an old version of PHP on your server. Change this line:

$score[$i]['username'] = (substr($line, 0, strpos($line, ' (Now:')) ?: substr($line, 0, strpos($line, ', ')));

To this:

$score[$i]['username'] = (substr($line, 0, strpos($line, ' (Now:')) ? substr($line, 0, strpos($line, ' (Now:')) : substr($line, 0, strpos($line, ', ')));

This ?: That is short-hand for, if This is true, show me This, otherwise show me That... but support for the short-hand ternary was only added in PHP5. So instead, you have to duplicate This so it becomes This ? This : That... if any of that made sense :p

Anyways, make that change and you should be good to go! :D

One more thing: if you get a load of fopen/fgets errors (like I mentioned in my last post), change 'results.txt' to '../results.txt'

Alex

#65 FunRoomSite

    Resident Elite

  • 1,314 posts
  • Joined: 07-May 06

Posted 24 February 2012 - 23:01

Sorry for the late reply! I have been busy with test. It does work now that I changed that. But do you know if it is possible to have it in the middle instead of on the side?

#66 FunRoomSite

    Resident Elite

  • 1,314 posts
  • Joined: 07-May 06

Posted 25 February 2012 - 00:55

Oh! Or is there a way I could add a field on the page that lets them type their username in to get added to the list? Can it be sent directly to players.txt somehow?

#67 -Alex-

    Noob Hunter

  • 2,551 posts
  • Joined: 08-August 06
  • Location: Liverpool / Amsterdam

Posted 25 February 2012 - 07:26

View PostFunRoomSite, on 24 February 2012 - 23:01, said:

Sorry for the late reply! I have been busy with test. It does work now that I changed that. But do you know if it is possible to have it in the middle instead of on the side?

Honestly, I don't know how to make a widget appear in the main content area. You could rewrite it so that it's a plugin instead of a widget, but that's a bit more work. Maybe one of the other WP experts could help you here... glad it works though! :)

View PostFunRoomSite, on 25 February 2012 - 00:55, said:

Oh! Or is there a way I could add a field on the page that lets them type their username in to get added to the list? Can it be sent directly to players.txt somehow?

The best way to do this is to have a cron job setup on your Bluehost account to periodically dump all the usernames in the custom profile field of your forums. I can post the code you need to use, and how to set it up; but first I need these 2 bits of information.

-- 1)

Go to your phpBB admin panel

Click the Users and Groups tab

Then click Custom Profile Fields on the left side

Can you tell me what it says under 'Field identification'?

-- 2)

Still in your ACP, click the Maintenance tab

On the left side, under Database; click Backup

On the right you'll see a list of tables (Table select)... they'll all begin with a 'table prefix' (by default phpbb_)... can you tell me what it says before the underscore?


Alex :)

#68 FunRoomSite

    Resident Elite

  • 1,314 posts
  • Joined: 07-May 06

Posted 26 February 2012 - 02:58

View Post-Alex-, on 25 February 2012 - 07:26, said:

Honestly, I don't know how to make a widget appear in the main content area. You could rewrite it so that it's a plugin instead of a widget, but that's a bit more work. Maybe one of the other WP experts could help you here... glad it works though! :) The best way to do this is to have a cron job setup on your Bluehost account to periodically dump all the usernames in the custom profile field of your forums. I can post the code you need to use, and how to set it up; but first I need these 2 bits of information. -- 1) Go to your phpBB admin panel Click the Users and Groups tab Then click Custom Profile Fields on the left side Can you tell me what it says under 'Field identification'? -- 2) Still in your ACP, click the Maintenance tab On the left side, under Database; click Backup On the right you'll see a list of tables (Table select)... they'll all begin with a 'table prefix' (by default phpbb_)... can you tell me what it says before the underscore? Alex :)

It says "summoner_name" "usual_play_time" and "preferred_champs" and before the _ is says "phpbb"

#69 -Alex-

    Noob Hunter

  • 2,551 posts
  • Joined: 08-August 06
  • Location: Liverpool / Amsterdam

Posted 28 February 2012 - 16:53

Hey,

Sorry, my PC's been offline a couple of days. Which out of them 3 fields contains LoL usernames?

Alex

#70 FunRoomSite

    Resident Elite

  • 1,314 posts
  • Joined: 07-May 06

Posted 28 February 2012 - 19:28

View Post-Alex-, on 28 February 2012 - 16:53, said:

Hey,

Sorry, my PC's been offline a couple of days. Which out of them 3 fields contains LoL usernames?

Alex
No problem. But summoner name is their LoL username.

#71 -Alex-

    Noob Hunter

  • 2,551 posts
  • Joined: 08-August 06
  • Location: Liverpool / Amsterdam

Posted 01 March 2012 - 04:50

Just wanted to write a quick post to let you know I'm gonna post the full info needed to get this working as soon as I can... my main dev PC crashed and having to restore everything slowly to another PC.

In the meantime if anyone else wants to write a PHP script to write the results of a MySQL query to a text file, then here's the start of it:

<?php

require_once('./forum/config.php');

$dh = mysqli_connect($dbhost, $dbuser, $dbpasswd, $dbname);
$res = mysqli_query($dh, "SELECT `pf_summoner_name` from `phpbb_profile_fields_data` WHERE `pf_summoner_name` != '' AND `pf_summoner_name` IS NOT NULL");

?>

This will then be called via a cron job as 'php -q /home2/dominau4/public_html/somefile.php'

#72 Site Lab

    Professional Graphics Design

  • 197 posts
  • Joined: 23-August 11
  • Location: Manchester, England
  • OS: Windows 8 Consumer Preview

Posted 01 March 2012 - 17:56

<?
require_once('./forum/config.php');


$fh = fopen('data.txt', 'w');

mysqli_connect($dbhost, $dbuser, $dbpasswd, $dbname);

$result = mysql_query("SELECT `pf_summoner_name` from `phpbb_profile_fields_data` WHERE `pf_summoner_name` != '' AND `pf_summoner_name` IS NOT NULL;");

while ($row = mysql_fetch_array($result)) {

    $last = end($row);

    foreach ($row as $item) {

        fwrite($fh, $item);

        if ($item != $last)
            fwrite($fh, "\t");

    }

    fwrite($fh, "\n");
}

fclose($fh);
?>

Would that work?

#73 FunRoomSite

    Resident Elite

  • 1,314 posts
  • Joined: 07-May 06

Posted 05 March 2012 - 16:43

View PostSite Lab, on 01 March 2012 - 17:56, said:

<?
require_once('./forum/config.php');


$fh = fopen('data.txt', 'w');

mysqli_connect($dbhost, $dbuser, $dbpasswd, $dbname);

$result = mysql_query("SELECT `pf_summoner_name` from `phpbb_profile_fields_data` WHERE `pf_summoner_name` != '' AND `pf_summoner_name` IS NOT NULL;");

while ($row = mysql_fetch_array($result)) {

	$last = end($row);

	foreach ($row as $item) {

		fwrite($fh, $item);

		if ($item != $last)
			fwrite($fh, "\t");

	}

	fwrite($fh, "\n");
}

fclose($fh);
?>

Would that work?

I have no idea. :\
Alex left us! :(