I've been able to put together a basic search form that allows users to search my database by entering their last name. My question is how do I offer them an option, using a radio button, to allow them to search by, for example, a city or state or age, etc. Here is the code I have in place for a search of a user's last name:

<?php 

	$SQL_FROM = 'adr';
	$SQL_WHERE = 'last';

?>
<?php
	$searchq		=	strip_tags($_GET['q']);
	$getRecord_sql	=	'SELECT * FROM '.$SQL_FROM.' WHERE '.$SQL_WHERE.' LIKE "'.$searchq.'%"';
	$getRecord		=	mysql_query($getRecord_sql);
	if(strlen($searchq)>0){

	echo '<ul>';
	while ($list = mysql_fetch_array($getRecord)) {?>
		<li><a href="runners2.php?name=<?php echo urlencode($list['first'].' '.$list['last']); ?>"><?php echo $list['last']; ?> <small><?php echo $list['first']; ?></small><small><?php echo $list['city']; ?></small><small><?php echo $list['age']; ?></small></a></li>
	<?php } 
	echo '</ul>';
	?>
<?php } ?>

If I used a radio button, this code would represent my "last name" search, but how would I also allow the user to search via other options? I'm assuming I would use IF...Else, but don't know how to construct the code around this search code.

Recommended Answers

All 2 Replies

only a litle change i m using switch u can use 'if '

<?php 

	$SQL_FROM = 'adr';
switdh($_request['radiobuttomname']){
case 'lastname':
	$SQL_WHERE = 'last';
case 'city:
        $SQL_WHERE = 'city';
and 
so on ....

?>
<?php
	$searchq		=	strip_tags($_GET['q']);
	$getRecord_sql	=	'SELECT * FROM '.$SQL_FROM.' WHERE '.$SQL_WHERE.' LIKE "'.$searchq.'%"';
	$getRecord		=	mysql_query($getRecord_sql);
	if(strlen($searchq)>0){

	echo '<ul>';
	while ($list = mysql_fetch_array($getRecord)) {?>
		<li><a href="runners2.php?name=<?php echo urlencode($list['first'].' '.$list['last']); ?>"><?php echo $list['last']; ?> <small><?php echo $list['first']; ?></small><small><?php echo $list['city']; ?></small><small><?php echo $list['age']; ?></small></a></li>
	<?php } 
	echo '</ul>';
	?>
<?php } ?>

I'm not sure how the if/else or switch statement would work if I'm attempting to use it with a radio button. The idea is to give the user an option between searching via a 'last name' or searching using 'city'. The name of the radio buttons are 'last' and 'city'. Would it be better to use switch or if, and what is the difference? Thanks

<?php 

	
	include('config.php'); 
	
	
	

	$SQL_FROM = 'adr';
	$SQL_WHERE = 'last';

?>
<?php
	$searchq		=	strip_tags($_GET['q']);
	$getRecord_sql	=	'SELECT * FROM '.$SQL_FROM.' WHERE '.$SQL_WHERE.' LIKE "'.$searchq.'%"';
	$getRecord		=	mysql_query($getRecord_sql);
	if(strlen($searchq)>0){
	// AJAX Response													
	echo '<ul>';
	while ($list = mysql_fetch_array($getRecord)) {?>
		<li><a href="runners2.php?name=<?php echo urlencode($list['first'].' '.$list['last']); ?>"><?php echo $list['last']; ?> <small><?php echo $list['first']; ?></small><small><?php echo $list['city']; ?></small><small><?php echo $list['age']; ?></small></a></li>
	<?php } 
	echo '</ul>';
	?>
<?php } ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.