Jump to content

MySQL query results sorted alphabeltically


webguync

Recommended Posts

I have a MySQL query that displays data from a table in a long list of about 100 entries. it requires the user to scroll quite a bit down the page. I know I can limit the number of results per page using the LIMIT clause, but how can I display the results alphabetically (in this case by last name), so that initially all of the A's will display, and I can provide links for the rest of the alpha's, so when you click on B's, all of the B's will display, C's, D's etc.

 

Let me know if I need to explain further, but I believe this should be understandible.

Link to comment
Share on other sites

I see, thanks.

 

is there an easier way to create results for each letter of the alpha instead of a new query to correspond to each letter? Right now I am setting the variable $letter=A, but then I will want to do that for B through Z as well.

 

thanks for the help!

Link to comment
Share on other sites

i assume A will be on a different "page" than B

 

such as .... "search.php?letter=A" compared to "search.php?letter=B"

 

then its really easy to change the letter...

 

$letter = sql_quote['letter'];

 

however that won't work alone, you need a script to call the url. I'm not on my home computer, so i don't have that script with me....

Link to comment
Share on other sites

<?php
$let = 'A';
for ($i=0; $i<26; $i++) 
{
   echo "<a href='?letter=$let'>$let</a> ";
   $let++;
}

if (isset($_GET['letter']))
{
    $letter = $_GET['letter'];
    echo "<h3>$letter</h3>";
    
    $sql = "SELECT firstname, lastname FROM tablename              
            WHERE lastname LIKE '$letter%'
            ORDER BY lastname";                                                          // adjust column and table names
    $res = mysql_query($sql) or die(mysql_error());
    while (list($f, $l) = mysql_fetch_row($res))
    {
        echo "$l, $f<br/>";
    }
}
?>

Link to comment
Share on other sites

thanks the code supplied above is pretty much what I want, but only the first name is displayed for some reason. In my SQL query I have

$sql ="SELECT id, f_name, l_name, company FROM $table_name WHERE l_name LIKE '$letter%' ORDER BY l_name ";
$result = @mysql_query($sql, $connection) or die(mysql_error());

 

so I should be getting the result of first name, last name and company

Link to comment
Share on other sites

try this with your query

<?php
$let = 'A';
for ($i=0; $i<26; $i++) 
{
   echo "<a href='?letter=$let'>$let</a> ";
   $let++;
}

if (isset($_GET['letter']))
{
    $letter = $_GET['letter'];
    echo "<h3>$letter</h3>";
    
    $sql = "SELECT id, f_name, l_name, company 
            FROM $table_name 
            WHERE l_name LIKE '$letter%' 
            ORDER BY l_name ";                                                          // adjust column and table names
    $res = mysql_query($sql) or die(mysql_error());
    while (list($id, $fn, $ln, $co) = mysql_fetch_row($res))
    {
        echo "$id $fn $ln, $co<br/>";
    }
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.