• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Town statistics [supporting multiworld]

Zonet

Web Developer
Joined
Sep 1, 2008
Messages
4,393
Reaction score
52
Location
Tibia VS RL-life, guess whos back?
Very easy script. I'm improving my skills in PHP, so any suggestions would be really appreciated :).
This counts how many players are citizen in X town.
Configuration in config, you have to set the town ids and town names.

PHP:
<?php
$main_content .= '<table border="0px" cellspacing="1" cellpadding="4" width="100%">
<tr><th>Town</th><th>Citizens</th></tr>';
$row = 1;
$worlds = $config['site']['worlds'];
$main_content .= '<b>Towns:</b> ';
    foreach($worlds as $wid => $wname) {
        switch($_GET['world']) {
            case $wid:
                $towns = $towns_list[$wid];
            break;
        }
        $main_content .= '<a href="?subtopic=country&world='.$wid.'">'.$wname.'</a>, ';           
		foreach($towns as $idd => $name) {
			$color = ($row % 2 ? $config['site']['darkborder'] : $config['site']['lightborder']);
			$row++;
			$main_content .= '<tr bgcolor="'.$color.'"><td>'.$name.'</td>';
			$c = $SQL->query('SELECT id, COUNT(town_id) FROM players WHERE town_id = '.$idd.' AND world_id = '.$wid)->fetch();    
			$main_content .= '<td>'.$c[COUNT(town_id)].'</td></tr>';
		}
	}
$main_content .= '</table>';
?>

:$
 
Last edited:
Thanks, little bugfix with multiworld (in query)
PHP:
<?php
$main_content .= '<table border="0px" cellspacing="1" cellpadding="4" width="100%">
<tr><th>Town</th><th>Citizens</th></tr>';
$row = 1;
$worlds = $config['site']['worlds'];
$main_content .= '<b>Towns:</b> ';
    foreach($worlds as $wid => $wname) {
        switch($_GET['world']) {
            case $wid:
                $towns = $towns_list[$wid];
            break;
        }
        $main_content .= '<a href="?subtopic=country&world='.$wid.'">'.$wname.'</a>, ';           
		foreach($towns as $idd => $name) {
			$color = ($row % 2 ? $config['site']['darkborder'] : $config['site']['lightborder']);
			$row++;
			$main_content .= '<tr bgcolor="'.$color.'"><td>'.$name.'</td>';
			$c = $SQL->query('SELECT id, COUNT(town_id) FROM players WHERE town_id = '.$idd.' AND world_id = '.$wid)->fetch();    
			$main_content .= '<td>'.$c[COUNT(town_id)].'</td></tr>';
		}
	}
$main_content .= '</table>';
?>
 
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\houses.php on line 14

PHP:
foreach($towns as $idd => $name) {
 
PHP:
foreach($towns as $idd => $name) {

Works properly for me.
Change the name of the variables, because there may be $towns in houses.php :)

#edit try this, changed variable names:
PHP:
   <?php
$main_content .= '<table border="0px" cellspacing="1" cellpadding="4" width="100%">
<tr><th>Town</th><th>Citizens</th></tr>';
$row = 1;
$_worlds = $config['site']['worlds'];
$main_content .= '<b>Towns:</b> ';
    foreach($_worlds as $wid => $wname) {
        switch($_GET['world']) {
            case $wid:
                $_towns = $towns_list[$wid];
            break;
        }
        $main_content .= '<a href="?subtopic=country&world='.$wid.'">'.$wname.'</a>, ';           
        foreach($_towns as $_idd => $_name) {
            $color = ($row % 2 ? $config['site']['darkborder'] : $config['site']['lightborder']);
            $row++;
            $main_content .= '<tr bgcolor="'.$color.'"><td>'.$_name.'</td>';
            $c = $SQL->query('SELECT id, COUNT(town_id) FROM players WHERE town_id = '.$_idd.' AND world_id = '.$wid)->fetch();    
            $main_content .= '<td>'.$c[COUNT(town_id)].'</td></tr>';
        }
    }
$main_content .= '</table>';
?>
 
I didn't really understand what you mean quering each town, but yes I'm bad at PHP & MySQL, just learning this :)

He means this:

PHP:
SELECT `town_id`, COUNT('town_id') FROM `players` WHERE `world_id` = '.$wid.' GROUP BY `town_id`
 
I dont find any problem with it :eek:?

Ok... More clear for you:

SELECT `town_id`, COUNT('town_id') FROM `players` WHERE `world_id` = '.$wid.' GROUP BY `town_id`

With this you don't need to query every city.
 
What elf means is that every time the foreach is done it will send another request to get the sql data, costs more resources (memory/cpu)
 
Back
Top