Jump to content

Confused.. I am a starter!


rockinaway

Recommended Posts

So you have links similar to <a href="yourpage.php?sortal=a">A</a> etc?

Well then this is how i would do it:

<?php
$letter = mysql_real_escape_string($_GET['sortal']);//using mysql_real_escape_string to prevent any malicous use of the sort
$sql = "SELECT * FROM `yourtable` WHERE `userfield` LIKE '$letter%'";
mysql_query($sql) or die (mysql_error());
?>

You use LIKE to allow you to place wildcard matches in your search term. A % siginfies any number of any character, and an underscore is used for one character of any type. Here, you want the letter that has been selected followed by any number of other characters. If no letter has been selected, then you would have a searchterm of just % in which case all results would be found.

Link to comment
Share on other sites

You need to wrap the code within an if statement, $_GET['sortal'] will only exist if the link has been clicked. So...

[code]
<?php
  if (isset($_GET['sortal'])) {
    $letter = mysql_real_escape_string($_GET['sortal']);//using mysql_real_escape_string to prevent any malicous use of the sort
    $sql = "SELECT * FROM `yourtable` WHERE `userfield` LIKE '$letter%'";
    if ($result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {
        while ($row = mysql_fetach_assoc($result))
          echo $row['username']."<br />";
        }
      }
    }
  }
?>
[/code]

Ive also extended the query a little to actually display the data.
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.