Links to sort fetched mysql data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cantiquo
Forum Newbie
Posts: 2
Joined: Tue Sep 08, 2009 12:16 am

Links to sort fetched mysql data

Post by cantiquo »

Hello Everyone,

I am making a long list of artists.
I generated the Alphabet, with links (A, B, C, D etc). so the people can easily find them by their last name. When I click on the letters I am getting the results ok but also the first person even when I click Z, or even when there are no artists in that letter. Could you help to do this better? also, how can I add a "No artists found" when I have no info in that letter?

I would appreciate any help. Thanks in advance!!!

Leon


Here's my code:

Code: Select all

 
<?php
// Here is where I generate the Alphabet, with links that ends in ?letter=
for ($i=65;$i<=90; ) {
$x = chr($i);
echo '<a href="'.$_SERVER["PHP_SELF"].'?letter='.$x.'">'.$x.'</a> '."\n";
}
 
// This is where i get the letter from the URL
$letter = $_GET['letter'];
 
// This should load the page showing only the artists with the letters clicked
$rsArtistsmaster = mysql_query("SELECT * FROM artists WHERE lname LIKE '$letter%'") 
or die(mysql_error());
?>
 

Code: Select all

 
<table border="0" align="center" cellpadding="5"><tr>
<th>Artists</th>
</tr>
<?php do { ?>
<tr>
<td><a href="singersinthenews.php?recordID=<?php echo $row_rsArtistsmaster['id']; ?>"> <?php echo $row_rsArtistsmaster['lname']; ?>, <?php echo $row_rsArtistsmaster['fname']; ?>&nbsp; 
<em><?php echo $row_rsArtistsmaster['voice']; ?></em>&nbsp; </a></td>
</tr>
<?php } while ($row_rsArtistsmaster = mysql_fetch_assoc($rsArtistsmaster)); 
?>
</table>
Last edited by cantiquo on Thu Sep 10, 2009 5:15 am, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Links to sort fetched mysql data

Post by VladSun »

I couldn't understand clearly your question, but you need a "while-do", not a "do-while" loop for iterating the DB result.


Please, use [ php ] [ /php ] (without spaces) BB code tags to hilight your PHP code.
There are 10 types of people in this world, those who understand binary and those who don't
cantiquo
Forum Newbie
Posts: 2
Joined: Tue Sep 08, 2009 12:16 am

Re: Links to sort fetched mysql data

Post by cantiquo »

Hello VladSun,

Thanks for your reply. So far this is my code! Here is the page that I am trying to fix: http://raincolor.com/singersmaster.php

What I am trying to get is to display all the names since the beginning and then display by letter (A, B, C etc, any name that is under that letter) I don't know how to add NO RESULT FOUND when I have no info in that letter.

Thanks!!

Code: Select all

 
// SELECT BY LETTER CODE MYSQL
// Here is where I generate the Alphabet, with links that ends in ?letter=
for ($i=65;$i<=90;$i++) {
    $x = chr($i);
    echo '<a href="'.$_SERVER["PHP_SELF"].'?letter='.$x.'">'.$x.'</a> '."\n";
}
 
// This is where i get the letter from the URL
$letter = $_GET['letter'];
 
 
// This should load a page showing only the artists with the letters clicked
$rsArtistsmaster = mysql_query("SELECT id, lname, fname, voice
FROM artists 
WHERE left(lname,1) = '$letter'
ORDER BY lname ASC , fname ASC;");
 
 
 
$rsArtistMasterCount = mysql_query("SELECT count
FROM artists 
WHERE left(lname,1) = '$letter'
ORDER BY lname ASC , fname ASC;");
 
 
      
?>

Code: Select all

<p><?php echo $totalRows_rsArtistsmaster ?> Artists Total</p>
    <p><?php echo $rsArtistMasterCount = mysql_num_rows($rsArtistsmaster); ?> Artists Selected</p>


Code: Select all

 
<br />
<a href="/singersmaster.php"><em>Show all Artists</em></a><br />
<table border="0" align="center" cellpadding="5">
      <tr>
        <th>Artists</th>
      
      </tr>
      <?php while ($row_rsArtistsmaster = mysql_fetch_assoc($rsArtistsmaster)) 
{ ; 
?>
 
<tr>
   <td><a href="singersinthenews.php?recordID=<?php echo $row_rsArtistsmaster['id']; ?>">
                                              <?php echo $row_rsArtistsmaster['lname']; ?>, 
                                              <?php echo $row_rsArtistsmaster['fname']; ?>&nbsp;<em>
                                              <?php echo $row_rsArtistsmaster['voice']; ?></em>&nbsp; </a></td>
</tr>
<?php } ?>
  
 
 
    </table>
Post Reply