DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   creating a search form with multiple options, like the search form used on this site? (http://www.daniweb.com/forums/thread139538.html)

justinmyoung Aug 10th, 2008 6:47 pm
creating a search form with multiple options, like the search form used on this site?
 
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.

nikesh.yadav Aug 11th, 2008 2:45 am
Re: creating a search form with multiple options, like the search form used on this site?
 
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 } ?>

justinmyoung Aug 13th, 2008 8:40 pm
Re: creating a search form with multiple options, like the search form used on this site?
 
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 } ?>


All times are GMT -4. The time now is 6:10 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC