Jump to content

Only display records begining with...


hoponhiggo

Recommended Posts

Hi All

 

How would i go about amending my code to only display records beginning with a selected letter?

 

Example...

 

A,B,C,D,E,F,G,....would be displayed at the top of the page (defaults to A)

 

These are hyperlinks to all records beginning with the selected character...

 

my current code (which displays all records) is

 

else {
//gets all the members from the database

$getusers = '';

if(isset($_GET['filter'])){
	$getusers = mysql_query("SELECT * FROM `users` ORDER BY username ASC WHERE left(username, 1) == \"a\"") or die(mysql_error());	
} else {
	$getusers = mysql_query("SELECT * FROM `users` ORDER BY username ASC") or die(mysql_error());	
}

//loops there name out
while($user = mysql_fetch_array($getusers)) {
	$username = $user['username'];
	//to display image from source
	$dir = "prof_pics";

	$sql = "SELECT prof_pic FROM users WHERE username = '$username'";
	$res = mysql_query($sql) or die(mysql_error());
	if(mysql_num_rows($res) == 0) 
		die("Username not found in database.");

	$row = mysql_fetch_array($res);
	$pic="$dir/".$row['prof_pic'];
	$img="<img src=\"$pic\" width=\"88\" height=\"88\" align=\"center\"><br>";


	echo "<div id='memcontainer'>
	<div id='mempic'>$img</div>
	<div id='memname'><a href='members.php?user=$user[username]'>$user[username]</a></div><br>
	<div class='addfriend'><a style='text-decoration:none' href='friendrequest.php?user=$user[username]'><font color='red'>Add as Friend</a></div>
	</div><br>
	";
}
}
echo "<center>";
?>

Link to comment
Share on other sites

Try also NOT using SELECT * and make a deffinate effort NOT to run queries within loops!

 

something like

else {
//gets all the members from the database

$getusers = '';

if(isset($_GET['filter'])){
	$getusers = mysql_query("SELECT username, prof_pic FROM `users` ORDER BY username ASC WHERE left(username, 1) == \"a\"") or die(mysql_error());	
} else {
	$getusers = mysql_query("SELECT username, prof_pic FROM `users` ORDER BY username ASC") or die(mysql_error());	
}

//loops there name and profile image out
while($user = mysql_fetch_array($getusers)) {
	$username = $user['username'];
	$dir = "prof_pics";
	$pic="$dir/".$user['prof_pic'];
	$img="<img src=\"$pic\" width=\"88\" height=\"88\" align=\"center\"><br>";
	echo "<div id='memcontainer'>
	<div id='mempic'>$img</div>
	<div id='memname'><a href='members.php?user=$user[username]'>$user[username]</a></div><br>
	<div class='addfriend'><a style='text-decoration:none' href='friendrequest.php?user=$user[username]'><font color='red'>Add as Friend</a></div>
	</div><br>
	";
}
}
echo "<center>";
?>

 

Another thing, your using div ID's when you should be using div classes.

Link to comment
Share on other sites

Hi

 

Tried changing the code as suggested, but when i try to run the query (...members.php?filter=a) i get an SQL error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE username LIKE 'a%' ORDER BY username' at line 1

 

Any suggestions?

 

Updated code

 

else {
//gets all the members from the database

$getusers = '';

if(isset($_GET['filter'])){
	$getusers = mysql_query("SELECT * FROM `users` ORDER BY username WHERE username LIKE 'a%' ORDER BY username") or die(mysql_error());	
} else {
	$getusers = mysql_query("SELECT * FROM `users` ORDER BY username ASC") or die(mysql_error());	
}

//loops there name out
while($user = mysql_fetch_array($getusers)) {
	$username = $user['username'];
	//to display image from source
	$dir = "prof_pics";
	$pic="$dir/".$user['prof_pic'];
	$img="<img src=\"$pic\" width=\"88\" height=\"88\" align=\"center\"><br>";


	echo "<div id='memcontainer'>
	<div id='mempic'>$img</div>
	<div id='memname'><a href='members.php?user=$user[username]'>$user[username]</a></div><br>
	<div class='addfriend'><a style='text-decoration:none' href='friendrequest.php?user=$user[username]'><font color='red'>Add as Friend</a></div>
	</div><br>
	";
}
}
echo "<center>";
?>

Link to comment
Share on other sites

Why do you have 2 ORDER BY statements in the same query?

Try this:

 

1. grab the filter:

if(isset($_GET['filter']) && $_GET['filter'] != ''){
$filter = $_GET['filter']."%";
}else{
// set A as default
$filter = "a%";
}

(Notice I took the opportunity to add the wildcard % after the letter when grabbing the filter variable from the url string, and also defined a default value for when $_GET['filter'] does not exist or is empty - you can change this if it's not what you want)

2. use filter in your query:

$getusers = mysql_query("SELECT * FROM `users` WHERE `username` LIKE '$filter' ORDER BY `username`") or die(mysql_error());

 

 

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.