高级搜索条件下拉sql查询

时间:2015-05-14 03:36:05

标签: php sql search

我想从php脚本(idoc脚本)修改标准搜索表单,添加3个下拉菜单供选择,并根据某些条件(如品牌,类别,文件类型和搜索词)显示结果

我试图添加条件" AND" /" OR"在查询上,但如果下拉列表的值为空(未选中),则不会显示或使用任何结果。

我想获取搜索结果从哪里可以选择3个条件之前如果需要减少id到sql数据库的数量,从这些条件,它将能够从名称,标签和描述选项卡中找到特定的关键字

如果指定了一个,两个,三个或没有下拉输入,您能否给我反馈如何修复搜索查询以减少/选择生成结果的id数量!

通常情况下,我可以在网上或本网站上找到解决方案,但我不知道该怎么做。

这是我的表单。

<form name="sform" id="sform" action="{$baseurl}/{$langpage0}/{$langpage54}.html" method="get">
    <div class="form">
        <table>
            <tr>
                <td class="td">{$lang820} :</td>
                <td>
                    <div class="styled-select">
                        <select id="mfg" name="mfg">
                            <option value="">{$lang824}</option>
                            {insert name=get_doc_mfg assign=listdocmfg}
                            {section name=o loop=$listdocmfg}
                            <option value="{$listdocmfg[o].MFGID}">{$listdocmfg[o].name}</option>
                            {/section}
                        </select>
                    </div ><br />
                </td>
            </tr>
            <tr>
                <td class="td">{$lang218} :</td>
                <td>
                    <div class="styled-select">
                        <select id="category" name="category">
                            <option value="">{$lang115}</option>
                            {insert name=get_doc_categories assign=listdoccategories}
                            {section name=o loop=$listdoccategories}
                            <option value="{$listdoccategories[o].CATID}">{$listdoccategories[o].$langpage1}</option>
                            {/section}
                        </select>
                    </div ><br />
                </td>
            </tr>
            <tr>
                <td class="td">{$lang819} :</td>
                <td>
                    <div class="styled-select">
                        <select id="type" name="type">
                            <option value="">{$lang823}</option>
                            {insert name=get_doc_type assign=listdoctype}
                            {section name=o loop=$listdoctype}
                            <option value="{$listdoctype[o].TYPEID}">{$listdoctype[o].$langpage1}</option>
                            {/section}
                        </select>
                    </div ><br />
                </td>
            </tr>
            <tr>
                <td class="td">{$lang412}:</td>
                <td>
                    <div class="row">
                        <div class="text-fieldset">
                            <input class="text focus" type="text" name="q" id="q" value="{$q}"> 
                        </div>
                    </div>
                </td>
            </tr>
            <tr>
                <td class="td">&nbsp;</td>
                <td>
                    <div class="submit-fieldset">
                        <input class="submit" type="submit" value="{$lang693}">
                    </div>
                </td>
            </tr>
        </table>
    </div>
</form>  

我的avanced-search.php代码(基于search.php):(

<?php
include("conf.php");
include("imp.php");
$thebaseurl = $config['baseurl'];
$sortby = cleanit($_REQUEST['s']);
$filter = intval(cleanit($_REQUEST['f']));
$page = intval(cleanit($_REQUEST['page']));
$searchterm = cleanit($_REQUEST['q']);
$mfg = intval(cleanit($_REQUEST['mfg']));
$category = intval(cleanit($_REQUEST['category']));
$type = intval(cleanit($_REQUEST['type']));

if($searchterm != "")
{
    STemplate::assign('q',$searchterm);
    if ($sortby=="a")
    {
        $sort = "asc";
    }
    else
    {
        $sortby = "z";
        $sort = "desc";
    }

    if($page=="")
    {
        $page = "1";
    }
    $currentpage = $page;

    if ($page >=2)
    {
        $pagingstart = ($page-1)*$config['items_per_page'];
    }
    else
    {
        $pagingstart = "0";
    }
    $queryselected = "SELECT count(*) as total from docs WHERE public='1' AND active='1' AND (title like '%".mysql_real_escape_string($searchterm)."%' OR description like '%".mysql_real_escape_string($searchterm)."%' OR tags like '%".mysql_real_escape_string($searchterm)."%') order by rating $sort limit $config[maximum_results]";
    $query2 = "SELECT * from docs WHERE public='1' AND active='1'  AND (title like '%".mysql_real_escape_string($searchterm)."%' OR description like '%".mysql_real_escape_string($searchterm)."%' OR tags like '%".mysql_real_escape_string($searchterm)."%') order by rating $sort limit $pagingstart, $config[items_per_page]";
    STemplate::assign('pagetitle',$lang['664']." ".$searchterm);
    $executequeryselected = $conn->Execute($queryselected);

    $totaldocs = $executequeryselected->fields['total'];
    if ($totaldocs > 0)
    {
        if($executequeryselected->fields['total']<=$config['maximum_results'])
        {
            $total = $executequeryselected->fields['total'];
        }
        else
        {
            $total = $config['maximum_results'];
        }
        $toppage = ceil($total/$config['items_per_page']);
        if($toppage==0)
        {
            $xpage=$toppage+1;
        }
        else
        {
            $xpage = $toppage;
        }       
        $executequery2 = $conn->Execute($query2);
        $docs = $executequery2->getrows();
        $beginning=$pagingstart+1;
        $ending=$pagingstart+$executequery2->recordcount();
        $pagelinks="";
        $k=1;
        $theprevpage=$currentpage-1;
        $thenextpage=$currentpage+1;
        $added = "q=$searchterm&page=";
        if ($currentpage > 0)
        {

            if($currentpage > 1) 
            {
            $pagelinks.="<a href='$thebaseurl/".$langpage['0']."/".$langpage['54'].".html?".$added."1' class='page'>$lang[611]</a>";
            $pagelinks.="<a href='$thebaseurl/".$langpage['0']."/".$langpage['54'].".html?".$added."$theprevpage' class='page'>$lang[612]</a>";
            };

            $counter=0;

            $lowercount = $currentpage-5;
            if ($lowercount <= 0) $lowercount = 1;

            while ($lowercount < $currentpage)
            {
                $pagelinks.="<a href='$thebaseurl/".$langpage['0']."/".$langpage['54'].".html?".$added."$lowercount' class='page'>$lowercount</a>";
                $lowercount++;
                $counter++;
            }

            $pagelinks.="<span class='current'>$currentpage</span>";

            $uppercounter = $currentpage+1;

            while (($uppercounter < $currentpage+10-$counter) && ($uppercounter<=$toppage))
            {
                $pagelinks.="<a href='$thebaseurl/".$langpage['0']."/".$langpage['54'].".html?".$added."$uppercounter' class='page'>$uppercounter</a>";
                $uppercounter++;
            }

            if($currentpage < $toppage) 
            {

            $pagelinks.="<a href='$thebaseurl/".$langpage['0']."/".$langpage['54'].".html?".$added."$thenextpage' class='page'>$lang[613]</a>";
            $pagelinks.="<a href='$thebaseurl/".$langpage['0']."/".$langpage['54'].".html?".$added."$toppage' title='last page' class='page'>$lang[614]</a>";
            }
        }
    }
    else
    {
        $error = $lang['665'];
    }
}
else
{
    STemplate::assign('pagetitle',$lang['660']);    
}

//TEMPLATES BEGIN
STemplate::assign('mainmenu',4);
STemplate::assign('error',$error);
STemplate::assign('pagelinks',$pagelinks);
STemplate::assign('docs',$docs);
STemplate::display('header.tpl');
STemplate::display('recherche_avancee.tpl');
STemplate::display('footer.tpl');
//TEMPLATES END
?>

我的sql表和一个例子。

CREATE TABLE IF NOT EXISTS `docs` (
  `DID` bigint(20) NOT NULL AUTO_INCREMENT,
  `USERID` bigint(20) NOT NULL DEFAULT '0',
  `title` varchar(120) NOT NULL DEFAULT '',
  `description` text NOT NULL,
  `tags` text NOT NULL,
  `categories` varchar(255) NOT NULL DEFAULT '0|',
  `type` varchar(255) NOT NULL DEFAULT '0|',
  `mfg` varchar(255) NOT NULL DEFAULT '0|',
  `original_doc` varchar(50) NOT NULL DEFAULT '',
  `doc_name` varchar(50) DEFAULT NULL,
  `filesize` bigint(20) NOT NULL DEFAULT '0',
  `public` char(1) NOT NULL DEFAULT '1',
  `time_added` varchar(20) DEFAULT NULL,
  `date_added` date NOT NULL DEFAULT '0000-00-00',
  `viewcount` bigint(10) NOT NULL DEFAULT '0',
  `last_viewed` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `commentcount` int(8) NOT NULL DEFAULT '0',
  `favoritecount` int(8) NOT NULL DEFAULT '0',
  `downloadcount` int(8) NOT NULL DEFAULT '0',
  `featured` char(1) NOT NULL DEFAULT '0',
  `ratingcount` bigint(10) NOT NULL DEFAULT '0',
  `rating` float NOT NULL DEFAULT '0',
  `filehome` varchar(120) NOT NULL DEFAULT '',
  `allowcomments` char(1) NOT NULL DEFAULT '1',
  `allowratings` char(1) NOT NULL DEFAULT '1',
  `allowembeds` char(1) NOT NULL DEFAULT '1',
  `allowdownloads` char(1) NOT NULL DEFAULT '1',
  `voter_id` varchar(200) NOT NULL DEFAULT '',
  `active` char(1) NOT NULL DEFAULT '',
  `mature` char(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`DID`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=95 ;

--
-- Contenu de la table `docs`
--

INSERT INTO `docs` (`DID`, `USERID`, `title`, `description`, `tags`, `categories`, `type`, `mfg`, `original_doc`, `doc_name`, `filesize`, `public`, `time_added`, `date_added`, `viewcount`, `last_viewed`, `commentcount`, `favoritecount`, `downloadcount`, `featured`, `ratingcount`, `rating`, `filehome`, `allowcomments`, `allowratings`, `allowembeds`, `allowdownloads`, `voter_id`, `active`, `mature`) VALUES
(1, 1, 'AirWave VisualRF', 'Aruba VisualRF and VisualRF Plan User Guide AWMS 7.2\r\n\r\nThe VisualRF module is an add-on to the AirWave Wireless Management Suite that provides a real-time picture of the actual radio environment of your wireless network and the ability to plan the wireless coverage of new sites. To understand what is happening on your wireless net work, you need to know where your users and devices are located – and you need to monitor the RF environment in those areas. The VisualRF module puts this information at your finger tips through integrated mapping and location data. VisualRF uses sophisticated RF fingerprinting to accurately display coverage patterns and calculate the location of every wireless device in rang\r\ne. Moreover, VisualRF does not require dedicated RF sensors or a costly additional location appliance – all the necessary information is gathered from your existing wireless access points and controllers. ', 'VisualRF Aruba AirWave AWMS 7.2', '42', '9', '312', '41.pdf', '41.pdf', 4, '1', '1417583011', '2014-12-03', 2370, '2015-05-13 14:45:50', 0, 0, 89, '0', 1, 6, '', '1', '1', '1', '1', '|1|', '1', '0'),

1 个答案:

答案 0 :(得分:0)

我想我自己找到了解决方案。 但是我认为这个请求不必要地重。

有人知道我怎么能用symplifier?

$sortby = cleanit($_REQUEST['s']);
    $filter = intval(cleanit($_REQUEST['f']));
    $page = intval(cleanit($_REQUEST['page']));
    $searchterm = cleanit($_REQUEST['q']);
    $mfg = intval(cleanit($_REQUEST['mfg']));
    $category = intval(cleanit($_REQUEST['category']));
    $type = intval(cleanit($_REQUEST['type']));


    if($mfg != "")
        {
            $addtosql .= "AND mfg like'%".mysql_real_escape_string($mfg)."%'";
            STemplate::assign('mfg',$mfg);
        }
        if($category != "")
        {
            $addtosql .= "AND categories like'%".mysql_real_escape_string($category)."%'";
            STemplate::assign('category',$category);
        }
        if($type != "")
        {
            $addtosql .= "AND type like'%".mysql_real_escape_string($type)."%'";
            STemplate::assign('type',$type);
        }
        if($searchterm != "")
        {       
        $addtosql .= "AND (title like '%".mysql_real_escape_string($searchterm)."%' OR description like '%".mysql_real_escape_string($searchterm)."%' OR tags like '%".mysql_real_escape_string($searchterm)."%')";
        STemplate::assign('q',$searchterm);
        if ($sortby=="a")
        {
            $sort = "asc";
        }
        else
        {
            $sortby = "z";
            $sort = "desc";
        }
        if($page=="")
        {
            $page = "1";
        }
        $currentpage = $page;

        if ($page >=2)
        {
            $pagingstart = ($page-1)*$config['items_per_page'];
        }
        else
        {
            $pagingstart = "0";
        }


        $queryselected = "SELECT count(*) as total from docs WHERE public='1' AND active='1' $addtosql order by rating $sort limit $config[maximum_results]";
        $query2 = "SELECT * from docs WHERE public='1' AND active='1' $addtosql order by rating $sort limit $pagingstart, $config[items_per_page]";


        STemplate::assign('pagetitle',$lang['664']." ".$searchterm);
        $executequeryselected = $conn->Execute($queryselected);

表格已被修改以显示所选择的场地:)

<form name="sform" id="sform" action="{$baseurl}/{$langpage0}/{$langpage54}.html" method="get">
                        <div class="form">
                        <table>
                            <tr>
                                    <td class="td">{$lang820} :</td>
                                    <td>
                                        <div class="styled-select">
                                            <select id="mfg" name="mfg">
                                                <option value="">{$lang824}</option>
                                                {insert name=get_doc_mfg assign=listdocmfg}
                                                {section name=o loop=$listdocmfg}                               
                                                <option value="{$listdocmfg[o].MFGID}" {if $mfg eq $listdocmfg[o].MFGID}selected="selected"{/if}>{$listdocmfg[o].name}</option>
                                                {/section}
                                            </select>
                                        </div ><br />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="td">{$lang218} :</td>
                                    <td>
                                        <div class="styled-select">
                                            <select id="category" name="category">
                                                <option value="">{$lang115}</option>
                                                {insert name=get_doc_categories assign=listdoccategories}
                                                {section name=o loop=$listdoccategories}
                                                <option value="{$listdoccategories[o].CATID}" {if $category eq $listdoccategories[o].CATID}selected="selected"{/if}>{$listdoccategories[o].$langpage1}</option>
                                                {/section}
                                            </select>
                                        </div ><br />
                                    </td>
                                </tr>
                                 <tr>
                                    <td class="td">{$lang819} :</td>
                                    <td>
                                        <div class="styled-select">
                                            <select id="type" name="type">
                                                <option value="">{$lang823}</option>
                                                {insert name=get_doc_type assign=listdoctype}
                                                {section name=o loop=$listdoctype}
                                                <option value="{$listdoctype[o].TYPEID}" {if $type eq $listdoctype[o].TYPEID}selected="selected"{/if}>{$listdoctype[o].$langpage1}</option>
                                                {/section}
                                            </select>
                                        </div ><br />
                                    </td>
                                </tr>
                            <tr>
                                <td class="td">{$lang412}:</td>
                                <td>
                                <div class="row">
                                        <div class="text-fieldset">
                                            <input class="text focus" type="text" name="q" id="q" value="{$q}"> 
                                        </div>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td class="td">&nbsp;</td>
                                <td>
                                <div class="submit-fieldset">
                                        <input class="submit" type="submit" value="{$lang693}">
                                    </div>
                                </td>
                            </tr>
                        </table>
                        </div>
                        </form>