Search Form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bkm_ho
Forum Newbie
Posts: 19
Joined: Mon Feb 11, 2008 12:25 am

Search Form

Post by bkm_ho »

hi guys, creating a search form and im having difficulties getting the selected city or subcategory to submit so that the search form displays which city and/or subcategory the user selects. the code in question is as follows, and parts of it are incorrect I know, but its the only way I can get the form to load (even though the form itself doesnt actually work!)

Code: Select all

 
<form action="?" method="get">
<input type="hidden" name="cityid" value="<?php echo $xcityid; ?>">
<input type="hidden" name="lang" value="<?php echo $xlang; ?>">
<input name="search" type="text" size="<?php echo $searchbox_on_top?35:23; ?>" value="<?php echo $_GET['search']; ?>">
 
<?php
 
if ($xsubcatid > 0)
{
 
?>
                                        <?php echo $field_sep; ?>
 
                                        <select name="cities">
                                        
                                        <?php
                                        
                                        $sql = "SELECT countryid, cityname AS cityname
                                                FROM $t_cities
                                                WHERE enabled = '1'
                                                ORDER BY countryid";
 
                                        $cityres = mysql_query($sql);
 
                                        while ($row=mysql_fetch_array($cityres))
                                                {
                                                    echo "<option value=\"$row[countryid]\">$row[cityname]</option>\n";
                                                    if ($_GET['cities'] == $row['cityname']) echo " selected";
                                                }
 
                                        ?>                                      
                                        </select>
                                        <input type="hidden" name="view" value="ads">
                                        <input type="hidden" name="cities" value="<?php echo $_GET['selected']; ?>">
                                        
                                        <?php echo $field_sep; ?>
 
                                        <select name="subcatname">
                                        
                                        <?php
                                        
                                        $sql = "SELECT subcatid, subcatname AS subcatname
                                                FROM $t_subcats scat
                                                WHERE enabled = '1'
                                                ORDER BY subcatid";
 
                                        $scatres = mysql_query($sql);
 
                                        while ($row=mysql_fetch_array($scatres))
                                                {
                                                    echo "<option value=\"$row[subcatid]\">$row[subcatname]</option>\n";
                                                }
 
                                        ?>                                      
                                        </select>
                                        <input type="hidden" name="view" value="ads">
                                        <input type="hidden" name="subcatid" value="<?php echo $row[subcatname]; ?>">
 
can someone point out to me where am I going wrong? do i need to put "selected" somewhere?
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Search Form

Post by andym01480 »

Curly Brackets around the $_row variables in the echo...

Code: Select all

echo "<option value=\"{$row[countryid]}\">$row[cityname]</option>\n";
and for the other one too.
bkm_ho
Forum Newbie
Posts: 19
Joined: Mon Feb 11, 2008 12:25 am

Re: Search Form

Post by bkm_ho »

no difference im afraid. the answer must lie in

Code: Select all

<input type="hidden" name="cities" value="<?php echo $_GET['selected']; ?>">
because i havent specified a variable for value. but such a variable must also be specified in the sql query surely? at the moment, i can only get the search form to appear if the url contains ?xsubcatid, because of certain 'if' statements within the search code itself.

i just need to figure out how a user can switch to another subcategory and/or city purely by using the search form.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Search Form

Post by andym01480 »

You've lost me now!
But...

i)There is no submit button to make the form GET the info back to itself
ii) Select and option tags don't have selected attribute http://www.w3schools.com/tags/tag_select.asp select="selected" is fo radio button defaults
iii) You need the {} round $row['arrayid'] within echo statements.

Edit:
iv) Are you connecting to the database?
bkm_ho
Forum Newbie
Posts: 19
Joined: Mon Feb 11, 2008 12:25 am

Re: Search Form

Post by bkm_ho »

i've only supplied part of the code in the search form. there is a submit button at the end. the db connection details are in a separate inc file.

so can anyone suggest how i can rewrite the code so that i can select the location?

there's two search forms within the site...one is search.inc.php here:

Code: Select all

 
<?php
 
require_once("config.inc.php");
 
 
if ($xview == "main" || $show_sidebar_always) 
{
    $searchbox_on_top = 0;
    $field_sep = "<br><img src=\"images/spacer.gif\" height=\"2\" width=\"2\"><br>";
}
else
{
    $searchbox_on_top = 1;
    $field_sep = " &nbsp; ";
}
 
 
 
if($dir_sort) 
{
    $sortcatsql = "ORDER BY catname";
    $sortsubcatsql = "ORDER BY subcatname";
}
else
{
    $sortcatsql = "ORDER BY pos";
    $sortsubcatsql = "ORDER BY scat.pos";
}
 
?>
<form action="?" method="get">
<input type="hidden" name="cityid" value="<?php echo $xcityid; ?>">
<input type="hidden" name="lang" value="<?php echo $xlang; ?>">
<input name="search" type="text" size="<?php echo $searchbox_on_top?35:23; ?>" value="<?php echo $_GET['search']; ?>">
 
<?php
 
if ($xsubcatid > 0)
{
 
?>
                                        <?php echo $field_sep; ?>
 
                                        <select name="cities">
                                        
                                        <?php
                                        
                                        $sql = "SELECT countryid, cityname AS cityname
                                                FROM $t_cities
                                                WHERE enabled = '1'
                                                ORDER BY countryid";
 
                                        $cityres = mysql_query($sql);
 
                                        while ($row=mysql_fetch_array($cityres))
                                                {
                                                    echo "{<option value=\"$row[countryid]\">$row[cityname]}</option>\n";
                                                    
                                                }
 
                                        ?>                                      
                                        </select>
                                        <input type="hidden" name="view" value="ads">
                                        <input type="hidden" name="cities" value="<?php echo $city_url; ?>">
                                        
                                        <?php echo $field_sep; ?>
 
                                        <select name="subcatname">
                                        
                                        <?php
                                        
                                        $sql = "SELECT subcatid, subcatname AS subcatname
                                                FROM $t_subcats scat
                                                WHERE enabled = '1'
                                                ORDER BY subcatid";
 
                                        $scatres = mysql_query($sql);
 
                                        while ($row=mysql_fetch_array($scatres))
                                                {
                                                    echo "{<option value=\"$row[subcatid]}\">$row[subcatname]</option>\n";
                                                }
 
                                        ?>                                      
                                        </select>
                                        <input type="hidden" name="view" value="ads">
                                        <input type="hidden" name="subcatid" value="<?php echo $row[subcatname]; ?>">
    <?php
    
    if ($xsubcathasprice)
    {
 
    ?>
 
        <?php echo $field_sep; ?>
 
        <?php echo $xsubcatpricelabel; ?>:
        <input type="text" name="pricemin" size="6"><?php echo $lang['SEARCH_TO']; ?> 
        <input type="text" name="pricemax" size="6">
 
    <?php
 
    }
 
    ?>
 
    <?php
 
    foreach ($xsubcatfields as $fldnum=>$fld)
    {
        if($fld['SEARCHABLE'])
        {
 
    ?>
 
        <?php echo $field_sep; ?>
 
        <?php echo $fld['NAME']; ?>: 
 
        <?php if ($fld['TYPE'] == 'N') { ?>
 
            <input type="text" name="x[<?php echo $fldnum; ?>][min]" size="6"><?php echo $lang['SEARCH_TO']; ?>  
            <input type="text" name="x[<?php echo $fldnum; ?>][max]" size="6">
 
        <?php } else if ($fld['TYPE'] == "D") { ?>
 
            <select name="x[<?php echo $fldnum; ?>]">
            <option value="">- <?php echo $lang['ALL']; ?> -</option>
            <?php
            foreach ($fld['VALUES_A'] as $v)
            {
                echo "<option value=\"$v\">$v</option>";
            }
            ?>
            </select>
 
        <?php } else { ?>
 
            <input type="text" name="x[<?php echo $fldnum; ?>]" size="23">
 
        <?php } ?>
 
 
    <?php
 
        }
    }
 
    ?>
 
    <input type="hidden" name="view" value="ads">
    <input type="hidden" name="subcatid" value="<?php echo $xsubcatid; ?>">
    
<?php
 
}
 
elseif ($xcatid > 0)
{
 
?>
 
    <?php echo $field_sep; ?>
 
    <select name="subcatid">
    <option value="0">- <?php echo $xcatname; ?> -</option>
    <?php
    
    $sql = "SELECT subcatid, subcatname AS subcatname
            FROM $t_subcats scat
            WHERE catid = $xcatid
                AND enabled = '1'
            $sortsubcatsql";
 
    $scatres = mysql_query($sql);
 
    while ($row=mysql_fetch_array($scatres))
    {
        echo "<option value=\"$row[subcatid]\">$row[subcatname]</option>\n";
    }
 
    ?>
    </select>
    <input type="hidden" name="view" value="ads">
    <input type="hidden" name="catid" value="<?php echo $xcatid; ?>">
 
<?php
 
}
 
elseif ($xview == "events" || $xview == "showevent")
{
 
?>
 
    <select><option value="0">- <?php echo $xcatname; ?> -</option></select>
    <input type="hidden" name="view" value="events">
 
 
<?php
 
}
 
else
{
 
?>
    
    <?php echo $field_sep; ?>
 
    <select name="catid">
    <?php
    
    $sql = "SELECT catid, catname AS catname
            FROM $t_cats
            WHERE enabled = '1'
            $sortcatsql";
 
    $catres = mysql_query($sql);
 
    while ($row=mysql_fetch_array($catres))
    {
        echo "<option value=\"$row[catid]\">$row[catname]</option>\n";
    }
 
    ?>
    <?php if($enable_calendar) { ?><option value="-1"><?php echo $lang['EVENTS']; ?></option><?php } ?>
    </select>
    <input type="hidden" name="view" value="ads">
 
<?php
 
}
 
?>
 
<?php 
if($xcityid>0)
{
?>
 
    <?php
    $sql = "SELECT areaname FROM $t_areas WHERE cityid = $xcityid";
    $area_res = mysql_query($sql);
    if (mysql_num_rows($area_res))
    {
    ?>
 
    <?php echo $field_sep; ?>
    <?php echo $lang['POST_LOCATION']; ?>:
    <select name="area">
    <option value="">- <?php echo $lang['ALL']; ?> -</option>
 
    <?php
 
        while($area_row = mysql_fetch_array($area_res))
        {
            echo "<option value=\"$area_row[areaname]\"";
            if ($_GET['area'] == $area_row['areaname']) echo " selected";
            echo ">$area_row[areaname]</option>";
        }
 
    ?>
 
    </select>
 
    <?php 
    }
    ?>
 
<?php 
}
?>
 
<button type="submit"><?php echo $lang['BUTTON_SEARCH']; ?></button>
</form>
 
you can see this search in action on http://www.asiapropertyshop.co.uk which only displays certain search fields depending on which page you are viewing.

if you look at http://www.asiapropertyshop.co.uk/?xsubcatid=1, you can see all the search fields from search.inc.

i want to create a separate detailed search page that allows users to choose a location, and the property type aswell as the search fields from search.inc but i am not sure how or why the detail search page isnt working.

i've also re written part of the code for location search here, which im told should work...except it still doesnt

Code: Select all

 
 
<?php echo $field_sep; ?>
 
                                        <select name="cityid">
                                        
                                        <?php
                                        
                                        $sql = "SELECT countryid, cityname AS cityname
                                           FROM $t_cities
                                           WHERE enabled = '1'
                                           ORDER BY countryid";
 
                                    $cityres = mysql_query($sql);
 
                                    $userSelectedCity = $_REQUEST['cityid'];
 
                                    while ($row=mysql_fetch_array($cityres))   {
                                       
                                    if($row[cityname]==$userSelectedCity) {
                                    $selected="selected=\"selected\"";                                           
                                    } else {
                                    $selected="";
                                                }
    
                                    echo "{<option value=\"$row[countryid]\" $selected>$row[cityname]}</option>\"";
                                        }
                                    ?>                  
                                        </select>
                                        <input type="hidden" name="view" value="ads">
                                        <input type="hidden" name="cityid" value="<?php echo "$xcityid"; ?>">
 
 
hope someone can help point me in the right direction. cheers
Post Reply