我为我的网站做了一个php/ajax/mysql/json脚本,显示所有标签。MySQL计数包括到jSon对象
如果我获取页面ID,它会显示该页面的所有标签。 但是!在我的主页中,我想显示所有标签,从所有页面上都有一个计数器。
例如,如果我标记4页用
"**ilovestackoverflow**"
我想在我的主页,看看
"**ilovestackoverflow (4)**"
我知道我可以指望与MySQL COUNT线(),但unfortunatlety我不能在这种情况下使用它,但是我尝试了很多方法:(
(函数sqlnez($ value)是我的stripslashes脚本)
php的:
$server = mysql_connect($host, $user, $password);
$connection = mysql_select_db($database, $server);
//if got pageID
if(isset($_GET['id'])) {
$query = mysql_query("SELECT * FROM tags WHERE id=".sqlnez($_GET['id'])."");
$json = "({ tags:[";
for ($x = 0, $numrows = mysql_num_rows($query); $x < $numrows; $x++) {
$row = mysql_fetch_assoc($query);
$json .= "{tag:'" . $row["tag"] . "'}";
if ($x < mysql_num_rows($query) -1)
$json .= ",";
else
$json .= "]})";
}
}
//this is the part what needs help
//if got nothing
else {
$query = mysql_query("SELECT * FROM tags GROUP BY tag");
$json = "({ tags:[";
for ($x = 0, $numrows = mysql_num_rows($query); $x < $numrows; $x++) {
$row = mysql_fetch_assoc($query);
$json .= "{tag:'" . $row["tag"] . "',counter:'" . I WANT THE COUNTER HERE . "'}";
if ($x < mysql_num_rows($query) -1)
$json .= ",";
else
$json .= "]})";
}
}
$response = $_GET["callback"] . $json;
echo utf8_encode($response);
mysql_close($server);
感谢您的帮助和时间:)
将来,您应该尝试缩进代码。这对每个人都好。 – 2010-02-24 12:00:31