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.

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.
Same problem.. it needs the second . else it doesn't seem to work without error.

Also another thing, once the person has clicked and gone to the new page I want the click to be reset so the changes that occur from clicking do not occur again until they click..

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.