0

A while back I found a neat little php alphabetical pagination script. It returns entries in a database that start with a particular letter when click on a hyperlink related to the letter. It was using the mysql API so I modified it to use the mysqli API.

What I want to do now is to modify the php so that a css class will surround the selected letter, and I can use that class as an active state. Any ideas on how I could modify the code below to do this? Thank you.

$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>" ;
}
stevenmw
  • 689
  • 2
  • 8
  • 15

1 Answers1

0

Here you go, look for $class to see what I changed:

$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>" ;
}
Alexandre Danault
  • 8,602
  • 3
  • 30
  • 33