I have a question that I am having a hard time articulating so if my title needs to be changed please let me know. ;)
I am working on a classifieds website. Users are asked to select the city they want to see ads that are within that city. Those cites are listed under their parent countries like so  

CANADA - 155 posts  
-Toronto  
-Halifax  
-ect...  

USA - 78 posts  
-Detroit  
-Chicago  
-ect...  

So thats great, the list populates correctly and everything works as far as that is concerned.  

The problem I am having is that I would like to isolate the countries from the list with their post counts on the fly to use elsewhere in the site. I cant figure out how to separate that information from the list itself. I understand that this code:   

 `<div><a href="<?php echo $country_url; ?>" class="citylist_country"><?php echo $country['countryname']; ?> <?php if($show_region_adcount) echo "(".(0+$country_adcounts[$country['countryid']]).")"; ?></a></div>`  

```
<?php

require_once("initvars.inc.php");
require_once("config.inc.php");

?>
<table width="100%"><tr><td valign="top">
<?php

// Show city list

if($location_sort) 
{
    $sort1 = "ORDER BY countryname";
    $sort2 = "ORDER BY cityname";
}
else
{
    $sort1 = "ORDER BY c.pos";
    $sort2 = "ORDER BY ct.pos";
}

if ($show_region_adcount || $show_city_adcount)
{
    // First get ads per city and country
    $country_adcounts = array();
    $city_adcounts = array();
    $sql = "SELECT ct.cityid, c.countryid, COUNT(*) as adcnt
            FROM $t_ads a
                INNER JOIN $t_cities ct ON ct.cityid = a.cityid AND ($visibility_condn)
                INNER JOIN $t_countries c ON ct.countryid = c.countryid
            WHERE ct.enabled = '1' AND c.enabled = '1'
            GROUP BY ct.cityid";

    $res = mysql_query($sql) or die(mysql_error().$sql);

    while($row=mysql_fetch_array($res))
    {
        $country_adcounts[$row['countryid']] += $row['adcnt'];
        $city_adcounts[$row['cityid']] += $row['adcnt'];
    }
}

$sql = "SELECT * FROM $t_countries c INNER JOIN $t_cities ct ON c.countryid = ct.countryid AND ct.enabled = '1' WHERE c.enabled = '1' GROUP BY c.countryid $sort1";
$resc = mysql_query($sql);

$country_count = mysql_num_rows($resc);
//$split_at = ($country_count%3?((int)($country_count/3))+2:($country_count/3)+1);
$percol = floor($country_count/$location_cols);
$percolA = array();
for($i=1;$i<=$location_cols;$i++) $percolA[$i]=$percol+($i<=$country_count%$location_cols?1:0);

$i = 0; $j = 0;
$col = 1;
while($country = mysql_fetch_array($resc))
{
    /* Begin Version 5.0 */
    $country_url = buildURL("main", array((0-$country['countryid']), $country['countryname']));
    /* End Version 5.0 */
?>

    <div><a href="<?php echo $country_url; ?>" class="citylist_country"><?php echo $country['countryname']; ?> <?php if($show_region_adcount) echo "(".(0+$country_adcounts[$country['countryid']]).")"; ?></a></div>

    <?php

    if($country['countryid'] == $xcountryid || !$expand_current_region_only)
    {

        $sql = "SELECT * FROM $t_cities ct WHERE countryid = $country[countryid] AND enabled = '1' $sort2";
        $resct = mysql_query($sql);

        /* Begin Version 5.0 */
        $citycount = mysql_num_rows($resct);
        /* End Version 5.0 */

        while($city=mysql_fetch_array($resct))
        {        
            /* Begin Version 5.0 */
            if ($shortcut_regions && $citycount == 1 
                    && $city['cityname'] == $country['countryname']) {
                continue;
            }

            $city_url = buildURL("main", array($city['cityid'], $city['cityname']));
            /* End Version 5.0 */

    ?>

            <a href="<?php echo $city_url; ?>" class="citylist_city"><?php echo $city['cityname']; ?> <?php if($show_city_adcount) echo "(".(0+$city_adcounts[$city['cityid']]).")"; ?></a><br>

    <?php

        }
    }

    ?>

    <?php

    $i++; $j++;
    //if($i%$split_at == 0) echo "</td><td valign=\"top\">";
    if ($j%$percolA[$col]==0 && $i<$country_count) { echo "</td><td valign=\"top\">"; $col++; $j=0; } 

}

?>

</td></tr></table>

```

I really appreciate the help you guys give and its my birthday today, so maybe someone can throw this old dog a bone!   

Thanks in advance.