Php 从预先存在的数据库列表中获取信息,并以样式文本“动态”显示它

Php 从预先存在的数据库列表中获取信息,并以样式文本“动态”显示它,php,jquery,html,Php,Jquery,Html,我有一个问题,我很难表达,所以如果我的标题需要更改,请让我知道; 我在一个分类广告网站上工作。用户被要求选择一个城市,他们希望看到该城市内的广告。这些城市被列在它们的母国之下,就像这样 加拿大-155邮政多伦多哈利法克斯等。。。 美国-底特律芝加哥等78个邮政。。。 这太好了,列表填充正确,一切都正常。我遇到的问题是,我想将这些国家从列表中隔离出来,并在网站的其他地方动态使用它们的帖子计数。我不知道如何将这些信息与列表本身分开。我明白这个代码:是什么把这些信息拉到了我想要的列表中,但我只想在我的

我有一个问题,我很难表达,所以如果我的标题需要更改,请让我知道; 我在一个分类广告网站上工作。用户被要求选择一个城市,他们希望看到该城市内的广告。这些城市被列在它们的母国之下,就像这样 加拿大-155邮政多伦多哈利法克斯等。。。 美国-底特律芝加哥等78个邮政。。。 这太好了,列表填充正确,一切都正常。我遇到的问题是,我想将这些国家从列表中隔离出来,并在网站的其他地方动态使用它们的帖子计数。我不知道如何将这些信息与列表本身分开。我明白这个代码:是什么把这些信息拉到了我想要的列表中,但我只想在我的网站上有一个页面有一些基本的文字,大意是加拿大有155个帖子,美国有78个 我想我要问的是,我如何在不把整个名单都列出来的情况下,把一个国家的名字和它的帖子逐一统计出来?我知道这应该是简单的,但我是自学的一切,所以请原谅我,如果这似乎很愚蠢。我已经包括了下面的所有代码,因为我知道这是有帮助的

<?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>

我真的很感谢你们的帮助,今天是我的生日,所以也许有人可以扔这只老狗一根骨头!提前感谢。

希望此查询对您有所帮助

SELECT c.name, c.countryid, COUNT(a.id) 
    FROM country AS c, city AS ct, ads AS a
    WHERE c.enabled = '1' AND ct.countryid = c.countryid
    AND ct.enabled = '1' AND a.cityid = ct.cityid
GROUP BY c.countryid;

在加载dom之后使用jquery

 $("a.citylist_country").each(function () {
         //use this country name
         alert( $(this).html());
 });

我的猜测是——使用高效或编写良好的SQL查询,您可以直接获得所需的数据,而无需遍历所有数据。花时间浏览代码并理解结构。我会看看我能做什么。