PHP CodeIgniter参数查询


PHP CodeIgniter Parameter Query

我想知道如何将参数(Get)传递给我的模型以在数据库中搜索并返回结果?

我为数据库中的所有products创建了一个Research函数

下面是我的代码:

控制器:

function recherche2($search){
    $this->load->model('ordiDepotModele');
    $resultat = $this->ordiDepotModele->rechercher($search);
    $i=0;
    foreach ($resultat->result() as $row){
        $item = array();
        $item['num'] = $row->idProduit;
        $item['nomProduit'] =  $row->nomProduit;
        $item['prix'] =  $row->prixVente;   
        $listeitems[$i] = $item;
        $i++;
    }
    $data['item'] = $listeitems;
 }

模型:

function rechercher($search){
    $query = $this->db->query("SELECT * FROM produit WHERE nomProduit LIKE '%".$search."%'");
    return $query;
}

视图:

 if(isset($item)){
    for($i = 0; $i<count($item);$i++){?>    
        <div class = "res_item">
            <div class = "res_img">
                <img src = "<?php echo $image; ?>/computer2.png"/>
            </div>
            <div class = "res_info">
                <div class "res_num">
                    <label class = "titre_prod">
                        Numéro : 
                    </label>
                    <span class = "info_prod"> <?php echo $item[$i]['num']?> </span>
                </div>
                <div class "res_name">
                    <label class = "titre_prod">
                        Nom : 
                    </label>
                    <span class = "info_prod"> <?php echo $item[$i]['nomProduit']?> </span>
                </div>
                <div class "res_prix">
                    <label class = "titre_prod">
                        Prix : 
                    </label>
                    <span class = "info_prod"> <?php echo $item[$i]['prix']?> $ </span>
                </div>
                <div class "res_cat">
                    <label class = "titre_prod">
                        Catégorie : 
                    </label>
                    <span class = "info_prod"> Test </span>
                </div>  
            </div>
        </div>  
 <?php 
     } 
 }
 else 
     echo 'Aucun résultat obtenu';      

Thanks to all

您需要在application/config/config.php中启用查询字符串

$config['enable_query_strings'] = TRUE;

但是你现在不需要它。你应该把它作为URL段传递。

function recherche2($keyword) {
    $this->load->model('ordiDepotModele');
    $resultat = $this->ordiDepotModele->rechercher($keyword);
    $data['nom'] = $keyword;
...

我也认为你的问题是你传递值给你的模型,像这样:

 $resultat = $this->ordiDepotModele->rechercher($data['nom']);

将"nom"值作为单个值从数组中取出。(假设你只有一个搜索框)

然后在你的提醒中这样做:

function rechercher($data){
     echo "<script>alert('".$data['nom']."');</script>";

从关联数组中读取值,但此时$data不是一个数组,它是一个包含字符串的局部变量。你可以查看

 echo $data;

//编辑对不起,我不能在评论中发布格式化的代码。试试这个:

public function test() {
    echo '['.$this->input->get('search').']';
    echo '<form method="GET">';
    echo '<input type="input" name="search" /><input type="submit" />';
    echo '</form>';
}

在codeigniter中,参数通常在url段中传递,使url看起来更好。

url看起来像这样example.org/recherche2/KEYWORD。这种技术有一些缺点,因为url中不允许使用所有字符。

您可以设置配置文件中允许的字符。

如果你将使用基于url段的方式,控制器将看起来像这样:

function recherche2($search) {
    $data['nom'] = $search;
    $this->load->model('ordiDepotModele');
    $resultat = $this->ordiDepotModele->rechercher($data['nom']);
    $i=0;
    foreach ($resultat->result() as $row){
       $item = array();
       $item['num'] = $row->idProduit;
       $item['nomProduit'] =  $row->nomProduit;
       $item['prix'] =  $row->prixVente;   
       $listeitems[$i] = $item;
       $i++;
    }
    $data['item'] = $listeitems;
}
顺便问一下,为什么使用post?