【发布时间】:2011-01-20 11:24:58
【问题描述】:
我为我的网站制作了一个 php/ajax/mysql/json 脚本,以显示所有标签。
如果我获取页面 ID,它将显示该页面的所有标签。 但!在我的主页中,我想显示所有带有计数器的页面中的所有标签。
例如,如果我用
标记了 4 个页面"**ilovestackoverflow**"
我想在我的主页上看到
"**ilovestackoverflow (4)**"
我知道我可以用 mysql COUNT() 计算行数,但不幸的是我不能在这种情况下使用它,但是我尝试了很多方法:(
(function 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);
感谢您的帮助和时间:)
【问题讨论】:
-
以后,你应该尝试缩进你的代码。这对每个人都更好。
标签: php jquery mysql ajax json