0

长时间的搜索者在这里第一次提问。

我正在使用 codeigniter 创建一个系统,用户可以在其中使用表单中的一系列字段来搜索结果。这些字段都不是必需的,但可用于压缩结果。我让它在下面的直接 php 中工作,但经过一些认真的搜索后,我无法理解如何让该功能在 CodeIgniter 中工作。我也是这个软件的新手。任何建议将不胜感激。

索引.php

<form action="site/searchcharacter.php" method="post" class="form" id="searchForm">
        <legend>Search AnimeDB</legend>
        <select name="gender"><option>None</option><option>Male</option><option>Female</option></select>
        <select name="approx_age"><option>None</option><option>Baby</option><option>Todler</option><option>Child</option><option>Teen</option><option>Adult</option><option>Senior</option></select>
        <input type="text" name="hair_colour" class="input-small" placeholder="Hair Colour">
        <select name="hair_length"><option>None</option><option>Hair Above Ears</option><option>Hair Below Ears</option><option>Hair Below Shoulders</option><option>Hair Below Waist</option></select>
        <input type="text" name="eye_colour" class="ut-small" placeholder="Eye Colour">
        <select name="ear_type"><option>None</option><option>Human Ears</option><option>Cat Ears</option><option>Dog Ears</option><option>Horns</option><option>Other</option></select>
        <input type="text" name="weapons" class="input-small" placeholder="Weapons">
        <div class="form-actions">
            <button class="btn btn-primary" id="submit_button" type="submit">Search</button>
        </div>
      </form>

搜索字符.php

$query = "SELECT * FROM characters WHERE 1=1";

    if ($gender != "None") {
        if (!empty($gender)) {
            $query = $query . " AND gender='$gender'";
        }
    }

    if ($age != "None") {
        if(!empty($age)) {
            $query = $query . " AND approx_age='$age'";
        }
    }


    if(!empty($hairColour)) {
        $query = $query . " AND hair_colour='$hairColour'";
    }

    if ($hairLength != "None") {
        if(!empty($hairLength)) {
            $query = $query . " AND hair_length='$hairLength'";
        }
    }

    if(!empty($eyeColour)) {
        $query = $query . " AND eye_colour='$eyeColour'";
    }

    if ($earType != "None") {
        if(!empty($earType)) {
            $query = $query . " AND ear_type='$earType'";
        }
    }

    if(!empty($weapons)) {
        $query = $query . " AND weapons='$weapons'";
    }

    //Prepare and Exec
    $STH = $DBH->query($query);
4

1 回答 1

0

我将从 Active Records 开始 -

if ($gender != "None" && !empty($gender))
{
  $this->db->where('gender', $gender);
}

然后,您可以对其他检查重复此操作,因为多个 where 被添加为链式 AND。

然后你打电话$query_result = $this->db->get('characters');

于 2013-04-14T19:59:03.300 回答