Create Index by Alpha Letter

I’d like to know if someone can point me to a tutorial on how I can create a web page that PHP will create an A-Z index (much like you see in a book), a user clicks on that letter and is sent to a page with a list based upon that clicked letter.

I appreciate your time! Thank you!

If you’re referring to my gratitude for the assistance, of course I’m thankful!

Thanks for the link.

I believe I got it working, thanks to your guys’ help! I’ve posted it, just in case anyone sees an error or if any other users need to use something similar:

Query:
mysql_select_db($database_rcofcuser, $rcofcuser);
$query_getCat = "SELECT DISTINCT UPPER(LEFT(member_directory.name,1)) as letters FROM member_directory ORDER BY letters";
$getCat = mysql_query($query_getCat, $rcofcuser) or die(mysql_error());
$row_getCat = mysql_fetch_assoc($getCat);
$totalRows_getCat = mysql_num_rows($getCat);
PHP:
<?php echo '<a style="text-decoration: none;" href="member_directory_alpha.php?ltr='.$row_getCat['letters'].'">'.$row_getCat['letters'].'</a>'; ?>

members_directory_alpha.php

Query:
$var1_getCat = "-1";
if (isset($_GET['ltr'])) {
  $var1_getCat = $_GET['ltr'];
}
mysql_select_db($database_rcofcuser, $rcofcuser);
$query_getCat = sprintf("SELECT member_directory.name, member_directory.contact, member_directory.`position`, member_directory.address, member_directory.city, member_directory.`state`, member_directory.zipcode, member_directory.voice, member_directory.fax, member_directory.email, member_directory.url_address, member_directory.heading_id FROM member_directory WHERE UPPER(LEFT(member_directory.name,1)) = %s", GetSQLValueString($var1_getCat, "text"));
$getCat = mysql_query($query_getCat, $rcofcuser) or die(mysql_error());
$row_getCat = mysql_fetch_assoc($getCat);
$totalRows_getCat = mysql_num_rows($getCat);
PHP:
&lt;?php if (!empty($row_getCat['name'])) { echo '&lt;strong&gt;' . $row_getCat['name'] . '&lt;/strong&gt;&lt;br /&gt;' ; } ?&gt;
&lt;?php if (!empty($row_getCat['contact']) && (!empty($row_getCat['position']))) { echo $row_getCat['contact'] . ',&nbsp;' . $row_getCat['position'] . '&lt;br /&gt;' ; } ?&gt;
&lt;?php if (!empty($row_getCat['address'])) { echo $row_getCat['address'] . '&lt;br /&gt;' ; } ?&gt;
&lt;?php if (!empty($row_getCat['city'])) { echo $row_getCat['city']; } ?&gt;
&lt;?php if (!empty($row_getCat['state'])) { echo ',&nbsp;' . $row_getCat['state']; } ?&gt;
&lt;?php if (!empty($row_getCat['zipcode'])) { echo '&nbsp;' . $row_getCat['zipcode'] . '&lt;br /&gt;' ; } ?&gt;
&lt;?php if (!empty($row_getCat['voice'])) { echo '&lt;strong&gt;Phone:&nbsp;&lt;/strong&gt;' . $row_getCat['voice'] . '&lt;br /&gt;' ; } ?&gt;
&lt;?php if (!empty($row_getCat['fax'])) { echo '&lt;strong&gt;Fax:&nbsp;&lt;/strong&gt;' . $row_getCat['fax'] . '&lt;br /&gt;' ; } ?&gt;
&lt;?php if (!empty($row_getCat['email'])) { echo '&lt;strong&gt;Email:&nbsp;&lt;/strong&gt;' . urls2linkscomplex($row_getCat['email']) . '&lt;br /&gt;' ; } ?&gt;
&lt;?php if (!empty($row_getCat['url_address'])) { echo '&lt;strong&gt;Web:&nbsp;&lt;/strong&gt;' . '&lt;a href="http://' . $row_getCat['url_address'] . '" target="_blank"&gt;'; echo $row_getCat['url_address']; } if (!empty($row_getCat['url_address'])) { echo '&lt;/a&gt;'; } ?&gt;

The results seems to be coming out as expected!

Thanks again Sitepoint!

Okay. I’ve been able to pull the Alpha Index by letters off:

SELECT DISTINCT UPPER(LEFT(member_directory.name,1)) as letters
FROM member_directory
ORDER BY letters

with this:
<td><?php echo ‘<a style=“text-decoration: none;” href="member_directory.php?ltr=’.$row_getCat[‘letters’].‘">’.$row_getCat[‘letters’].‘</a>’; ?></td>

So far so good. Now, I’m trying to figure out how to retrieve the information from the dbase based upon the letter they’ve chosen. I’ve gotten this far:

SELECT member_directory.name, member_directory.contact, member_directory.position, member_directory.address, member_directory.city, member_directory.state, member_directory.zipcode, member_directory.voice, member_directory.fax, member_directory.email, member_directory.url_address, member_directory.heading_id FROM member_directory WHERE member_directory.name LIKE ‘$ltr%’

But have hit a brick wall because it displays all of the records instead of the records based on the chosen letter. Any insight?

By the way, if this is more for the MySQL forum, just let me know and I’ll go over there.

Thanks!

Looks good!

As StarLion states, you need to apply the choosen critiria (filter) to the query which returns the records.

Here’s a few pointers.


<?php
#collected letters from db
$letters_from_db = array(
  'A',
  'E',
  'G',
  'J',
  'T',
  'V'
);

#display links for letters which map to db
foreach(range('A', 'Z') as $chr){
  echo '<span style="margin: 5px;">';
  echo in_array($chr, $letters_from_db) ? sprintf('<a href="view.php?chr=&#37;1$s">%1$s</a>', $chr) : $chr ;
  echo '</span>';
}
?>


<?php
#set a default letter
$letter = 'A';

#if letter is passed and acceptable
if(false === empty($_GET['chr']) && true === in_array($_GET['chr'], range('A', 'Z'))){
  $letter = $_GET['chr'];
}

#find records
$res = mysql_query(
  sprintf(
    "SELECT id, foo FROM table WHERE UPPER(LEFT(foo, 1)) = '%s'",
    mysql_real_escape_string($letter)
  )
);

#display records
echo '<ul>';
while($row = mysql_fetch_assoc($res)){
  printf(
    '
    <li>
      <a href="view-member.php?id=%d">
        %s
      </a>
    </li>
    ',
    $row['id'],
    $row['foo']
  );
}
echo '</ul>';
?>

You’ve already used the UPPER and LEFT commands to identify the captialized first letters of the entries… why dont you put that into a WHERE clause with = $_GET[‘ltr’] ?

PS: $ltr will not be automatically filled in anymore by default installations of PHP 5. Have you filled $ltr with the value of $_GET[‘ltr’] ?


<?php foreach(range('a', 'z') as $chr): ?>
  <a href="view-list.php?letter=<?php echo $chr; ?>">
    <?php echo $chr; ?>
  </a>
<?php endforeach; ?>

:slight_smile:

And Index of… what? Database entries? Where are you getting your data for your index?

Well yeah, but cant really point him in the right direction if it’s a database assignment and we point him towards the fread man page :stuck_out_tongue:

I suspect that it’s an assignment that’s designed for him to learn how to do this.

You’ll need to loop through your index and use something like chr to provide the characters.

You’ll want to set the querystring of each link to pass the appropriate info to the next page


L. <a href="book.php?chapter=L">Explaining Pastafarianism</a>

Then, when the page reloads you’ll need to check the querystring and show the appropriate page, using something like:


$chapter = filter_input(INPUT_GET, 'page');
if (empty($chapter)) {
    // show TOC
} else {
    // show $chapter section of the book
}

the information is coming from a DNase I built. It’s nothing more than a Rolodex of addresses and phone numbers of businesses.

I’m trying figure if I should select all of the business names and use the LENGTH(1) function and group them by letter.

The reason why I asked for a direction to a tutorial is I’d rather go through a tutorial than waste anyone’s time, which doesn’t seem to be working.