0

不久前,我发现了一个简洁的 php 字母分页脚本。当单击与该字母相关的超链接时,它会返回数据库中以特定字母开头的条目。它使用的是 mysql API,所以我修改它以使用 mysqli API。

我现在要做的是修改 php,以便 css 类将围绕选定的字母,并且我可以将该类用作活动状态。关于如何修改下面的代码来做到这一点的任何想法?谢谢你。

$mysqli = new mysqli("x", "x", "x", "x");

$sort = $_REQUEST['letter'];


if($sort == ""){
$qry= "SELECT * FROM table ORDER BY title ASC " ;
}else{

$qry = "SELECT * FROM table WHERE title LIKE '$sort%' ORDER BY title ASC" ;
}

$execute = $mysqli->query($qry) or die(mysqli_error());
$row_cnt = mysqli_num_rows($execute);

echo "<p>" ;
for ($i = 65; $i < 91; $i++) {
    printf('<a href="%s?letter=%s">%s</a> | ',
    $PHP_SELF, chr($i), chr($i));
}
echo "</p>" ;

if ($row_cnt  > 0) {
do{
while ($row = $execute->fetch_assoc()) {
        printf ("%s %s <br />", $row["id"], $row["title"]);
    }
}
while ($row = $execute->fetch_assoc());
}else{
echo "<p>No customer found.</p>" ;
}
4

1 回答 1

0

在这里,查找$class以查看我所做的更改:

$mysqli = new mysqli("x", "x", "x", "x");

$sort = $_REQUEST['letter'];


if($sort == ""){
$qry= "SELECT * FROM table ORDER BY title ASC " ;
}else{

$qry = "SELECT * FROM table WHERE title LIKE '$sort%' ORDER BY title ASC" ;
}

$execute = $mysqli->query($qry) or die(mysqli_error());
$row_cnt = mysqli_num_rows($execute);

echo "<p>" ;
for ($i = 65; $i < 91; $i++) {
    if(ord($sort) == $i) {
      $class = 'active';
    } else {
      $class = '';
    }

    printf('<a href="%s?letter=%s" class="%s">%s</a> | ', $PHP_SELF, chr($i), $class, chr($i));
}
echo "</p>" ;

if ($row_cnt  > 0) {
do{
while ($row = $execute->fetch_assoc()) {
        printf ("%s %s <br />", $row["id"], $row["title"]);
    }
}
while ($row = $execute->fetch_assoc());
}else{
echo "<p>No customer found.</p>" ;
}
于 2013-06-08T00:57:01.817 回答