Dino Excel
Dino Excel

Reputation: 384

Indexing alphabetically ordered table with A-Z as "links"

I have a rather complicated question but perhaps you already understand what I am trying to accomplish by reading my title, hopefully.

I have a big list stored in SQLite and I am outputting the table into my webpage using php looping, but the problem that still remains is how to add index per letter.

For instance you have a list of items, each of which range from A all the way to Z

I only want to display every letter at one time, so if A comes first then only A should by default be visible the rest should be accessed using a tab or link system with indexing (A - Z)

feedback please!

$stmt = $db->prepare('SELECT Title, Description, Alternative FROM TblOne;');
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo "<table>";
echo "<thead><tr><th>Title</th><th>Description</th><th>Alternative</th></tr></thead>";
 foreach($res AS $val) {
  echo "<tr>";
  foreach($val AS $val1) {
    echo "<td><a href='.html'>$val1</a></td>";
  }
  echo "</tr>";
}
echo "</table>";

Upvotes: 0

Views: 84

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270431

If you want to get the letters that start the field, use:

select distinct left(title, 1)
from tblOne

If you want to get the list that starts with a given letter:

SELECT Title, Description, Alternative
FROM TblOne
where left(title, 1) = LETTERYOUWANT

Upvotes: 1

Related Questions