Jump to content


Photo

Pagination but with letters


  • Please log in to reply
3 replies to this topic

#1 JayNo

JayNo
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationToronto

Posted 09 January 2006 - 03:25 AM

I'm page that has a mysql database. the database contains company names with some extra information. I need to build a page that has an alphabet at the top of the page, and if you click on one of the letters then you get the company names that start with that letter. Right now i have that, and it's working. But i've run into a problem. I want it so that when the alphabet at the top is generated, i only want letters that actually have stuff in the database to have links (so that a user knows where data is).
Example, if the database contained "Adidas" "Nike" and "K-Swiss" then only A, N, and K would have links on them, the rest would still be there, but not have a link.

here's the code that i have so far

// query
mysql_select_db($mysqldatabase, $mysqlconnection);
$testquery = "    SELECT        col_clientName
        FROM        tbl_TEST
        ORDER BY        clientName asc
";
$dotestquery = mysql_query($testquery, $mysqlconnection) or die(mysql_error());
$page = "";
if(isset($_GET['page'])) {
   $page = $_GET['page'];
}
// output alphabet
for ($i=65; $i<=90; $i++){
   $letter = chr($i);
   echo " <a href='letterTEST.php?page=$letter'>";
   echo $letter;
   echo "</a>";
}
echo "<br /><br />";    

// array output    
while (list($col_clientName) = mysql_fetch_row($dotestquery)) {
   $wordLetter = substr($col_clientName,0,1);
   if ( $wordLetter==$page ){
     echo "$clientName";
     echo "<br />";
   }
}

Has anyone done anything like this? Thank you in advance

- JayNo

#2 LazyJones

LazyJones
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 09 January 2006 - 03:55 AM

You could build a function that would check if database has match for that letter, like this:
function inDb($letter) {
//search database on first letter, LIMIT is because one is enough
    $query = "SELECT col_clientName FROM tbl_TEST WHERE clientName LIKE '$letter%' LIMIT 1";
    $result = mysql_query($query) or die (mysql_error());
    if (mysql_num_rows($result) > 0) {
        return true;    
    } else {
        return false;    
    }
}

and where you display the links:

...
if(inDb($letter)) {
    echo " <a href='letterTEST.php?page=$letter'>";
    echo $letter;
    echo "</a>";
} else {
    echo $letter;
}
...

Haven't tested the code, but maybe you get the basic idea.

#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 09 January 2006 - 04:40 AM

That's not ideal either -- it would require 26 calls to the database! The better way would simply be to return all of the leading letters in a single string, from one DB call:

SELECT UPPER( GROUP_CONCAT( DISTINCT LEFT(clientName,1) SEPARATOR '' ) ) AS clientAlphabet FROM tbl_TEST

And then simply check whether the letter appears in the stored alphabet string -- retrieved via a list()...mysql_fetch_row() call into a varible I'll call $clientAlphabet -- as you loop through the alphabet:

// output alphabet
for ($i=65; $i<=90; $i++){
   $letter = chr($i);
   echo ( strpos($clientAlphabet, $letter) === false ) ? $letter : " <a href='letterTEST.php?page=$letter'>$letter</a>";
}

Hope that makes sense.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#4 LazyJones

LazyJones
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 09 January 2006 - 04:46 AM

Valuable stuff, Fenway, thank you for that.

26 database calls isn't the optimized way. But at least I had the LIMIT :D





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users