开发者

PHP CodeIgniter Parameter Query

开发者 https://www.devze.com 2023-02-22 09:57 出处:网络
i want to know how can i pass a parameter (Get) to my model to search in my database and return the results?

i want to know how can i pass a parameter (Get) to my model to search in my database and return the results?

I make a Research function for all the products in my database

Here's my code :

Controller :

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;
 }

Model:

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

    return $query;
}

View:

 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&开发者_JAVA百科gt;  
 <?php 
     } 
 }
 else 
     echo 'Aucun résultat obtenu';      

Thanks to all


You need to enable querystrings in your application/config/config.php

$config['enable_query_strings'] = TRUE;

BUT you dont need it at this point. you should just pass it as a URL segment.

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

ALSO I think your problem is that you pass the value to your model like this:

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

which gets the "nom" value out of the array as a single value.. (assuming you have only one search box)

then in your alert you do this:

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

Which is reading a value out of an associative array, but at this point $data is not an array, its a local variable which contains a string. You could view it if you did

 echo $data;

// EDIT sorry I cant really post formatted code in a comment. try this:

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


In codeigniter parameters are often passed in url segments, making the urls look a lot nicer.

The url would look like this example.org/recherche2/KEYWORD. This technique has some disadvantages, because not all characters are allowed in the url.

You may set the allowed characters in the config files.

In case you will use the url segment based way, the controller will look like this:

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;
}

By the way, for what reason are you not using post?

0

精彩评论

暂无评论...
验证码 换一张
取 消