function phpAds_sortArray(&$array, $column = 0, $ascending = TRUE)
{
    for ($i = 0; $i < sizeof($array); $i++) {
        if (isset($array[$i]['children']) && is_array($array[$i]['children'])) {
            phpAds_sortArray($array[$i]['children'], $column, $ascending);
        }
    }
    phpAds_qsort($array, $column, $ascending);
}
        phpAds_sortArray($array, 'id', $orderdirection == 'up' ? TRUE : FALSE);
        break;
    case 'views':
        phpAds_sortArray($array, 'views', $orderdirection == 'up' ? TRUE : FALSE);
        break;
    case 'clicks':
        phpAds_sortArray($array, 'clicks', $orderdirection == 'up' ? TRUE : FALSE);
        break;
    case 'CTR':
        phpAds_sortArray($array, 'CTR', $orderdirection == 'up' ? TRUE : FALSE);
        break;
    case 'conversions':
        phpAds_sortArray($array, 'conversions', $orderdirection == 'up' ? TRUE : FALSE);
        break;
    case 'CNVR':
        phpAds_sortArray($array, 'CNVR', $orderdirection == 'up' ? TRUE : FALSE);
        break;
    default:
        break;
}
//-----------------------------------------------------------------------------------------------------------------------
// Sort array according to selected column and direction END
//-----------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------
// Start output
//-----------------------------------------------------------------------------------------------------------------------
// Form to select Overall or By Zone
echo "<form action='" . $HTTP_SERVER_VARS['PHP_SELF'] . "' method='post'>";
echo "<select name='screen' onChange='this.form.submit();' accesskey='" . $keyList . "' tabindex='" . $tabindex++ . "'>";
echo "<option value='overall'" . ($screen == 'overall' ? ' selected' : '') . ">" . $strOverall . "</option>";
echo "<option value='zones'" . ($screen == 'zones' ? ' selected' : '') . ">" . $strByZone . "</option>";
        phpAds_sortArray($stats, 'clicks', $orderdirection == 'up' ? TRUE : FALSE);
        break;
    case $GLOBALS['strCPCShort']:
        phpAds_sortArray($stats, 'cpc', $orderdirection == 'up' ? TRUE : FALSE);
        break;
    case $GLOBALS['strTotalCost']:
        phpAds_sortArray($stats, 'total_cost', $orderdirection == 'up' ? TRUE : FALSE);
        break;
    case $GLOBALS['strConversions']:
        phpAds_sortArray($stats, 'conversions', $orderdirection == 'up' ? TRUE : FALSE);
        break;
    case $GLOBALS['strCNVRShort']:
        phpAds_sortArray($stats, 'sr', $orderdirection == 'up' ? TRUE : FALSE);
        break;
    case $GLOBALS['strCPCoShort']:
        phpAds_sortArray($stats, 'cpco', $orderdirection == 'up' ? TRUE : FALSE);
        break;
    default:
        break;
}
//-----------------------------------------------------------------------------------------------------------------------
// Sort array according to selected column and direction END
//-----------------------------------------------------------------------------------------------------------------------
echo "<table border='0' width='100%' cellpadding='0' cellspacing='0'>";
echo "<tr><td>";
echo "<form name='choose_view' action='stats-campaign-banners.php'>";
echo "<input type='hidden' name ='clientid' value='" . $clientid . "'>";
echo "<input type='hidden' name ='campaignid' value='" . $campaignid . "'>";
echo "<select name='period' onChange='this.form.submit();' accesskey='" . $keyList . "' tabindex='" . $tabindex++ . "'>";
echo "<option value='banner'" . ($HTTP_SERVER_VARS['PHP_SELF'] == '/admin/stats-campaign-banners.php' ? ' selected' : '') . ">" . $strBannerOverview . "</option>";
echo "<option value='keyword'" . ($HTTP_SERVER_VARS['PHP_SELF'] == '/admin/stats-campaign-keywords.php' ? ' selected' : '') . ">" . $strKeywordStatistics . "</option>";
// $Revision: 1.1 $
/************************************************************************/
/* phpAdsNew 2                                                          */
/* ===========                                                          */
/*                                                                      */
/* Copyright (c) 2000-2002 by the phpAdsNew developers                  */
/* For more information visit: http://www.phpadsnew.com                 */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
// get active campaigns which are set to be optimized
$result_campaigns = phpAds_dbQuery("\n\t\n\t\t\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\t\t\tcampaignname as name,\n\t\t\t\t\t\t\t\t\tcampaignid as id,\n\t\t\t\t\t\t\t\t\tactive,\n\t\t\t\t\t\t\t\t\toptimise\n\t\t\t\t\t\t\t\tFROM \n\t\t\t\t\t\t\t\t\t" . $phpAds_config['tbl_campaigns'] . "\n\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\tactive\t\t= 't' AND\n\t\t\t\t\t\t\t\t\toptimise \t= 't'\n\t\t\t\t\t\t\t\tLIMIT\n\t\t\t\t\t\t\t\t\t15\n\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t");
while ($campaigns = phpAds_dbFetchArray($result_campaigns)) {
    // get banners in this campaign
    $result_banners = phpAds_dbQuery("\n\t\t\n\t\t\t\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\t\t\t\tb.bannerid\t \t\tas bannerid,\n\t\t\t\t\t\t\t\t\t\tb.campaignid\t\tas campaignid,\n\t\t\t\t\t\t\t\t\t\tb.weight \t\t\tas weight,\n\t\t\t\t\t\t\t\t\t\tsum(s.views)\t\tas views,\n\t\t\t\t\t\t\t\t\t\tsum(s.clicks) \t\tas clicks,\n\t\t\t\t\t\t\t\t\t\tsum(s.conversions) \tas conversions\n\t\t\t\t\t\t\t\t\tFROM \n\t\t\t\t\t\t\t\t\t\t" . $phpAds_config['tbl_banners'] . " as b,\n\t\t\t\t\t\t\t\t\t\t" . $phpAds_config['tbl_adstats'] . " as s\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\tb.bannerid = s.bannerid AND\n\t\t\t\t\t\t\t\t\t\tb.campaignid = " . $campaigns['id'] . "\n\t\t\t\t\t\t\t\t\tGROUP BY \n\t\t\t\t\t\t\t\t\t\tb.bannerid\n\t\t\t\t\t\t\t");
    if (mysql_num_rows($result_banners) > 1) {
        $banners_opt = array();
        // create array with banners for this campaign
        while ($banners = phpAds_dbFetchArray($result_banners)) {
            $banners_opt[] = $banners;
        }
        // sort array of banners by CTR descending
        phpAds_sortArray($banners_opt, 'conversions', false);
        // reset all banners in this campaign to weight=1
        $update = phpAds_dbQuery("\n\t\t\n\t\t\t\t\t\t\t\t\tUPDATE\n\t\t\t\t\t\t\t\t\t\t" . $phpAds_config['tbl_banners'] . "\n\t\t\t\t\t\t\t\t\tSET\n\t\t\t\t\t\t\t\t\t\tweight = 1\n\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\tcampaignid = " . $campaigns['id'] . "\n\t\t\t\t\t\t\t");
        // change the banner with most conversions to weight=10
        $update = phpAds_dbQuery("\n\t\t\n\t\t\t\t\t\t\t\t\tUPDATE\n\t\t\t\t\t\t\t\t\t\t" . $phpAds_config['tbl_banners'] . "\n\t\t\t\t\t\t\t\t\tSET\n\t\t\t\t\t\t\t\t\t\tweight = 10\n\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\tbannerid = " . $banners_opt[0]['bannerid'] . "\n\t\t\t\t\t\t\t");
    }
}