格式化从db中拉出的下拉框中的数据?

时间:2014-04-24 14:34:53

标签: php mysql

我有以下代码片段:

<li>
    <label>Engine Size:</label>
    <select name="engine">
        <?php
        $sql = mysql_query("SELECT DISTINCT BikeEngine FROM bikes "
                . "ORDER BY BikeEngine ASC");
        while ($row = mysql_fetch_array($sql)) {
            echo "<option>" . $row['BikeEngine'] . "cc" . "</option>";
        }
        ?>
    </select>
</li>

我的主要问题是引擎大小在数据库字段中存储为int,但我想在下拉框中将cc添加为后缀,但是当查询运行时,它会搜索说1500cc并且不返回任何结果,因为它在数据库字段中存储为1500。

我想知道我是如何解决这个问题的?

型号:

public function searchBikes($brand, $engine)
{
    if ($engine=='Any' || $brandString=='Any')
    {
        $this->db->select('BikeEngine');
        $this->db->select('BikeMake');
        $query = $this->db->get('bikes');
    }

    else
    {
        $this->db->like('BikeEngine', $engine);
        $this->db->like('BikeMake', $brand);
    }

    $query = $this->db->get('bikes');
    return $query->result();
}

控制器:

function search() {
$this->load->helper(array('form', 'url')); //without this form will not display

if ($this->input->post())
{
    $brand = $this->input->post('brand');
    $engine = $this->input->post('engine');
    $this->load->model("mbikes");
    $data['results'] = $this->mbikes->searchBikes($brand, $engine);
    $this->load->view('searchresults.php', $data);
} 
else 
{
    $this->load->view('search.php'); //display success view
}
}

1 个答案:

答案 0 :(得分:3)

选项值可能与其显示不同:

echo "<option value=". $row['BikeEngine'] .">" . $row['BikeEngine'] . "cc" . "</option>";