MyBB Community Forums

Full Version: Php optimizing.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
http://tm0.info/showthread.php?tid=12

A friend is asking for some help.


<?php
    $user->IsLogged2();

    if ($functions->IsGetVarEmpty('world')) {
        $World = $u_data['World'];
    } else {
        if (!is_numeric($_GET['world']) || $_GET['world'] < 0 || $_GET['world'] > 10) {
            $functions->Redirect('Map');
        }
        $World = $_GET['world'];
    }
    if ($functions->IsGetVarEmpty('continent')) {
        $Continent = $u_data['Continent'];
    } else {
        if (!is_numeric($_GET['continent']) || $_GET['continent'] <= 0 || $_GET['continent'] > 25) {
            $functions->Redirect('Map');
        }
        $Continent = $_GET['continent'];
    }

    if (!$functions->IsGetVarEmpty('show') && $_GET['show'] === 'world'){
        $Action = 'World';
    } else {
        $Action = 'Continent';
    }

    $World = $functions->SanitizeVar($World);
    $Continent = $functions->SanitizeVar($Continent);

    if ($Action === 'Continent') {
        $coords = array();
        $data = array();
        $i = 0;
        $shc1 = 1;
        $shc2 = 1;
        $q = mysql_query("SELECT Passable,Coords FROM map WHERE World = {$World} AND Continent = {$Continent}");
        while ($row = mysql_fetch_array($q)) {
            $coords[$i] = $row['Coords'];
            $data[$coords[$i]] = $row;
            $i++;
        }
        $new = $coords;

        $Map = '';

        for($i = 0 ; $i < 225 ; $i++) {
            if(($i) % 15 == 0) {
                $Map .= '<tr>' . "\n";
            }

            $query = mysql_query("SELECT Username, Alliance, Banned FROM u_data WHERE World = {$World} AND Continent = {$Continent} AND Coords = '{$new[$i]}'");

            //Query2 Faster?
            $query2 = mysql_query("SELECT TroopID FROM map WHERE World = {$World} AND Continent = {$Continent} AND Coords = '{$new[$i]}' LIMIT 0 , 1");

            if ($data[$new[$i]]['Passable'] == 'l') {
                $bg = 'green';
            } 
            if ($data[$new[$i]]['Passable'] == 's') {
                $bg = 'blue';
            } 
            if ((mysql_num_rows($query2)) == 1) {
                $row = mysql_result($query2, 0);
                if ($row == $u_data['Username']) {
                    $bg = '#80FF00';
                }
            }
            if ((mysql_num_rows($query)) == 1) {
                $query = mysql_fetch_array($query);
                if ($query['Alliance'] !== 'None' && $query['Alliance'] == $u_data['Alliance']) {
                    $bg = 'yellow';
                } else {
                    $bg = 'red';
                }
                if ($query['Banned'] === 'Yes') {
                    $bg = 'grey';
                }
            } 

            if ($World == $u_data['World'] && $Continent == $u_data['Continent'] && $new[$i] == $u_data['Coords']) {
                $bg = 'lightgreen';
            } 

            $Map .= '<td style="background: '. $bg . ';"><a href="?page=MapAction&world='. $World .'&continent='. $Continent .'&coords='. $new[$i] .'"></a></td>' . "\n";
            if(($i+1) % 15 == 0) {
                $Map .= '</tr>' . "\n";
            }
        }

        $Minus1Continent = (($Continent - 1) == 0) ?  $Continent : $Continent - 1;
        $Plus1Continent = (($Continent + 1) == 26) ?  $Continent : $Continent + 1;
        $SmartyArray = array (
        'Minus1Continent' => $Minus1Continent,
        'Plus1Continent' => $Plus1Continent,
        );
        $smarty->assign($SmartyArray);
        
    } elseif ($Action === 'World') {
        $Map = '';
        $rownum = 0;
        $Continent = 1;
        for($i = 0 ; $i < 121 ; $i++) {
            if(($i) % 11 == 0) {
                $Map .= '<tr>' . "\n";
            }
            $bg = 'green';

            if ($functions->IsEven(($i) % 11 ) || $functions->IsEven($rownum)) {
                $bg = 'blue';
            }

            if (!$functions->IsEven(($i) % 11 ) && !$functions->IsEven($rownum) && $u_data['World'] == $World && $u_data['Continent'] == $Continent) {
                $bg = 'lightgreen';
            }

            if ($bg == 'green' || $bg == 'lightgreen') {
                $Map .= '<td style="background: '.$bg.';"><a href="?page=Map&world='.$World.'&continent='.$Continent.'"></a></td>' . "\n";
            } else {
                $Map .=  '<td style="background: '.$bg.';"></td>' . "\n";
            }


            if(($i+1) % 11 == 0) {
                $Map .=  '</tr>' . "\n";
                $rownum++;   
            }

            if (!$functions->IsEven(($i) % 11 ) && !$functions->IsEven($rownum)) {
                $Continent++;
            }
        }

    }

    $Minus1World = (($World - 1) == 0) ?  $World : $World - 1;
    $Plus1World = (($World + 1) == 11) ?  $World : $World + 1;

    $SmartyArray = array (
    'Action' => $Action,
    'MapTable' => $Map,
    'World' => $World,
    'Continent' => $Continent,
    'Minus1World' => $Minus1World,
    'Plus1World' => $Plus1World,
    );

    $smarty->assign($SmartyArray);
?>
without seeing what is in all of his classes, we'l never be able to help. for all we know, the slowdowns can be from those
(2010-06-27, 05:50 PM)pavemen Wrote: [ -> ]without seeing what is in all of his classes, we'l never be able to help. for all we know, the slowdowns can be from those

I'll tell him that, see if he can post them.