Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 跨多个字段(包括联接)搜索查询_Php_Mysql - Fatal编程技术网

Php 跨多个字段(包括联接)搜索查询

Php 跨多个字段(包括联接)搜索查询,php,mysql,Php,Mysql,简单地说,我想在我的数据库上运行一个搜索查询,但我对如何接近它有点迷茫 首先,搜索不是一个开放的文本字段,用户可以输入任何他们喜欢的内容,而是由三个设置字段的选择菜单和四个复选框组成 选择菜单一是作业分类(“分类id”),它由许多分类和一个“所有作业”字段组成,该字段没有相等的数据库条目,但我认为,如果选中,将返回所有作业,而不考虑分类,但取决于下两个选择字段(显然)。 但是,每个作业的类别(这是必填字段,将始终完成)使用Toxi解决方案按照以下数据库结构进行存储,因为作业可以(并且鼓励)包括许

简单地说,我想在我的数据库上运行一个搜索查询,但我对如何接近它有点迷茫

首先,搜索不是一个开放的文本字段,用户可以输入任何他们喜欢的内容,而是由三个设置字段的选择菜单和四个复选框组成

选择菜单一是作业分类(“分类id”),它由许多分类和一个“所有作业”字段组成,该字段没有相等的数据库条目,但我认为,如果选中,将返回所有作业,而不考虑分类,但取决于下两个选择字段(显然)。 但是,每个作业的类别(这是必填字段,将始终完成)使用Toxi解决方案按照以下数据库结构进行存储,因为作业可以(并且鼓励)包括许多类别。 从第一个选择菜单中,我可以指定每个类别值等于类别id

CREATE TABLE `jobinfo` (
`job_id` INT NOT NULL AUTO_INCREMENT, 
`company_name` VARCHAR(50) NOT NULL, 
`job_title` TEXT NOT NULL, 
`url` VARCHAR(255) NOT NULL,
'country'  VARCHAR(50) NOT NULL,
'state'  VARCHAR(50) NOT NULL,
'city'  VARCHAR(50) NOT NULL,
'job_type'  VARCHAR(50) NOT NULL,
PRIMARY KEY (`job_id`)
)

CREATE TABLE `categories` (
`category_id` INT NOT NULL AUTO_INCREMENT, 
`category_name` VARCHAR(20) NOT NULL, 
PRIMARY KEY (`category_id`)
)

CREATE TABLE `tag_relational` (
`job_id` INT NOT NULL, 
`category_id` INT NOT NULL
)
第二个和第三个选择字段基于位置(国家和州/县)。它们稍微简单一些,只存储在主jobinfo表中。但是,我需要一个通用选项,因此如果用户选择美国作为国家,在州选择字段中,有一个选项可以查看美国的所有工作,或者按各个州进行深入调查,显然,将相同的逻辑应用于其他国家

(有关信息,第三个选择字段根据第二个选择菜单中的选择在客户端填充-因此,如果用户在第二个字段中选择英国,则第三个字段填充相应的县/地区。)

最后,我有四个与工作类型相关的复选框——永久工、合同工、自由职业者和远程工作。同样,这些是必需的选择,并作为字符串存储在主jobinfo表中

因此,我希望查询返回所有作业或基于所选类别,特别是基于选择菜单2和3的地理位置,或者如果访问者选择“所有州”或“所有县”等,则忽略选择列3。 另外,如果访问者没有选择四个复选框中的任何一个,我想返回列出的所有作业,这些作业与前三个select字段中的criterea匹配

我的表单如下所示:

    <select name= "jobtype" id = "jc" title = "Hold down Ctrl to select">
            <option>JOB CATAGORY</option>
            <option value = "All Jobs">All Jobs</option>
            <option value = "1">Job Cat One</option>
            <option value = "2">Job Cat Two</option>
            <option value = "3">Job Cat Three</option>
            <option value = "4">Job Cat Four</option>
            <option value = "5">Job Cat Five</option>
            <option value = "6">Job Cat Six</option>

        </select>

        <select name="countries"  name = "country" onChange="updatecities(this.selectedIndex)">
            <option selected>REGION</option>
            <option value="usa">USA</option>
            <option value="canada">Canada</option>
            <option value="uk">United Kingdom</option>
            <option value="eu">European Union</option>
            <option value="rotw">Rest of the World</option>
        </select>

        <select name="cities">  <!--onClick="alert(this.options[this.options.selectedIndex].value)"-->
        </select>

    <ul>
    <li><input type = "checkbox" name = "cat[]" value = "Permanent"/></li><label>Permanent</label>
    <li><input type = "checkbox" name = "cat[]" value = "Cpntract"/></li><label>Contract</label>
    <li><input type = "checkbox" name = "cat[]" value = "Freelance"/></li><label>Freelance</label>
    <li><input type = "checkbox" name = "cat[]" value = "Telework"/></li><label>Telework</label>
    </ul>

    <input type="submit" value = "GO" />
我很感激这是一个漫长的过程,我希望有人有时间来帮助我

谢谢你一如既往, 丹


编辑:我可以根据所选内容在if语句中放置几个查询吗?例如:如果jobtype不是空的,country不是空的,city是空的,请为country选择匹配的jobtype,否则…?

您的问题不完全清楚**,因此我做了以下假设:

假设:
  • 你知道你的Javascript。i、 e.
    $\u GET['cities']
    将始终包含有效/合法的城市名称。这个答案就是PHP和MySql(就像问题的标签一样)
  • 当你说复选框是强制性的时,我认为你需要单选按钮
  • 当你谈论州时,你指的是城市,因为州没有
  • 第一个
    用于类别,一组复选框单选按钮用于
    jobinfo.job\u type

  • 代码:
    $legal_values['categories']=array(0=>''1',2=>'2',3=>'3',4=>'4',5=>'5',6=>'6');
    $legal_value['countries']=数组('usa'=>'usa','canada'=>'canada','uk'=>'ukin',
    ‘欧盟’=>‘欧盟’、‘rotw’=>‘世界其他地区’;
    $legal_values['term']=array(0=>'永久',1=>'合同',2=>'自由职业者',3=>'远程工作');
    回声“;
    foreach($select\u name=>$legal\u值为$select\u值)
    {
    如果($select_name=='term')
    {
    foreach($legal_值为$index=>$val)
    {
    如果(isset($\u-GET[$select\u-name])&&&$\u-GET[$select\u-name]==$val)
    {
    $selected[$select_name]=$val;
    $isSelected=true;
    }
    其他的
    {
    $selected[$select_name]=“”;
    $isSelected=false;
    }
    回显“{$val}”
    区域')
    .'';
    foreach($legal_值为$index=>$val)
    {
    如果(isset($\u-GET[$select\u-name])&&&$\u-GET[$select\u-name]==$val)
    {
    $selected[$select_name]=$val;
    $isSelected=true;
    }
    其他的
    {
    $selected[$select_name]=“”;
    $isSelected=false;
    }
    echo.“($isCategories?'Job Cat':”)“{$val}”;
    }
    回声';
    回声'
    ';
    }
    }
    回声'
    ';
    $specific_cat=isset($selected['categories'])和&$selected['categories']==0?真:假;
    $specific_city=(isset($_GET['cities'])和&$_GET['cities']=/*无论第一个州选项对每个国家/地区是什么*/)?true:false;
    $SQL\u result=mysql\u查询('
    选择不同的作业信息。*
    来自jobinfo'
    .($specific_cat?',categories,tag_relational':“”)
    .“WHERE.”($specific\u cat?“jobinfo.job\u id=tag\u relational.job\u id
    和tag_relational.category_id={$selected['categories']}和“:”)
    .($specific_city?'jobinfo.city=“”.mysql_real_escape_string($\u GET['cities'])。”:“”)
    “jobinfo.country=\”{$selected['countries']}\”
    和jobinfo.job\u type=\“”.mysql\u real\u escape\u string($selected['term'])。“\”
    按jobinfo.job_id DESC;)排序;
    //打印作业表:
    $NUM_jobs=mysql_NUM_rows($SQL_result);//要在此搜索结果上显示的课程
    $counter=0;
    if($NUM_jobs>0)//如果有任何课程与搜索匹配
    {
    而($DB\u ROW=mysql\u fetch\u assoc($SQL\u result))//逐行检查结果
    {
    $counter++;
    如果($counter==1)
    {
    回声'
    #';
    foreach($DB_行为$label=>$val)
    {
    回显“{$label}”;
    }
    回声';
    }
    回声“
    {$counter}”;
    foreach($DB_行为$val)
    {
    回显“{$val}”;
    }
    回声';
    如果(
    
    $query = 
    "SELECT j.*
    FROM jobinfo j
    JOIN tag_relational r
    ON j.job_id=r.job_id
    JOIN tags t
    ON t.tag_id=r.tag_id
    WHERE t.tag_id = $cat ORDER BY job_id DESC";
    
    $legal_values['categories']=array(0=>'', 1=>'One', 2=>'Two', 3=>'Three', 4=>'Four', 5=>'Five', 6=>'Six');
    $legal_values['countries']=array('usa'=> 'USA','canada'=>'Canada', 'uk'=>'United Kingdom', 
        'eu'=>'European Union', 'rotw'=>'Rest of the World');
    $legal_values['term']=array(0 => 'Permanent', 1=> 'Contract', 2=>'Freelance', 3=>'Telework');
    
    echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"POST\">";
    foreach($legal_values as $select_name => $legal_values)
    {
        if($select_name=='term')
        {
            foreach($legal_values as $index => $val)
            {
                if(isset($_GET[$select_name]) && $_GET[$select_name]==$val)
                {
                    $selected[$select_name]=$val;
                    $isSelected=true;
                }
                else
                {
                    $selected[$select_name]="";
                    $isSelected=false;
                }
                echo "<label for=\"radio_{$index}\">{$val}</label>
                <input type=\"radio\" name=\"{$select_name}\" id=\"radio_{$index}\" value=\"{$val}\"" 
                .($isSelected?' selected="selected"':'').'>';
            }
        }
        else
        {
            $isCategories=$select_name=='categories'?true:false;
            echo '<select name="'.
                (($isCategories)?
                'jobtype" id = "jc" title = "Hold down Ctrl to select">
                    <option>JOB CATAGORY'
                : 'countries" onChange="updatecities(this.selectedIndex)">
                    <option>REGION')
                .'</option>';
    
            foreach($legal_values as $index => $val)
            {
                if(isset($_GET[$select_name]) && $_GET[$select_name]==$val)
                {
                    $selected[$select_name]=$val;
                    $isSelected=true;
                }
                else
                {
                    $selected[$select_name]="";
                    $isSelected=false;
                }
                echo "<option value = \"{$index}\" "
                .($isSelected?' selected="selected"':'')
                .">".($isCategories?'Job Cat':'')." {$val}</option>";
            }
            echo '</select>';
    
            echo '<select name="cities">  <!--onClick="alert(this.options[this.options.selectedIndex].value)"-->
            </select>';
        }
    }
    echo '<input type="submit" value = "GO" />
    </form>';
    
    $specific_cat=isset($selected['categories']) && $selected['categories']==0?true:false;
    $specific_city=(isset($_GET['cities']) && $_GET['cities']==/*Whatever that first state option is for each country*/)?true:false;
    
    $SQL_result = mysql_query('
    SELECT DISTINCT jobinfo.* 
    FROM jobinfo'
    .($specific_cat?', categories, tag_relational ':'')
    .'WHERE '.($specific_cat?"jobinfo.job_id=tag_relational.job_id
                AND tag_relational.category_id={$selected['categories']} AND ":'')
            .($specific_city?'jobinfo.city="'.mysql_real_escape_string($_GET['cities']).'" ':'')
    ."jobinfo.country=\"{$selected['countries']}\"
    AND jobinfo.job_type=\"".mysql_real_escape_string($selected['term'])."\"
    ORDER BY jobinfo.job_id DESC;");
    
    //print table of jobs:
    $NUM_jobs = mysql_num_rows($SQL_result);//courses to be displayed on this search results
    $counter=0;
    if($NUM_jobs>0)//if any courses match the search
    {
        while($DB_ROW=mysql_fetch_assoc($SQL_result))//go through results by row
        {
            $counter++;
            if($counter==1)
            {
                echo '<table>
                <tr>
                <th>#</th>';
                foreach ($DB_ROW as $label => $val)
                {
                    echo "<th>{$label}</th>";
                }
                echo '</tr>';
            }
    
            echo "<tr>
            <td>{$counter}</td>";
            foreach ($DB_ROW as $val)
            {
                echo "<td>{$val}</td>";
            }
            echo '</tr>';
    
            if($counter==$NUM_jobs)
            {
                echo '</table>';
            }
        }
        mysql_free_result($SQL_result);
    }
    //else: if nullset returned:
    else{ echo '<p>Sorry, your search returned no results.</p>'; }