1

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>  
 <?php 
     } 
 }
 else 
     echo 'Aucun résultat obtenu';      

Thanks to all

1
  • The name of my input text bar is search Commented Apr 7, 2011 at 1:04

2 Answers 2

0

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>';
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the suggestion --> it works now (for the alert) Also, like i said i want to pass by my input type text in the view and not by the URL directly
I'm confused. If your set your form method to GET then it will just post to http://yoursite/controller/method?search={value}. but you have to enable querystrings for this to be readable in your controller. If you dont want to use the URL.. then you have to do a POST.
i put the querystrings as True
but it won't work either.. i try the $this->input->get('search', true) but its not working
um.. what does your form look like? i just tried this and it works: public function test() { echo '['.$this->input->get('search').']'; echo '<form method="GET">'; echo '<input type="input" name="search" /><input type="submit" />'; echo '</form>'; }
|
0

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?

11 Comments

because it's a research function so naturally the input should be in the url, no?
Alright, thats probably just preferences :-) Try the suggested solution.
how can i set my $search variable this way?
it generate me an error Severity: Warning Message: Missing argument 1 for ordiDepot::recherche2() Filename: controllers/ordiDepot.php Line Number: 281 A PHP Error was encountered Severity: Notice Message: Undefined variable: search Filename: controllers/ordiDepot.php Line Number: 282
I am not an expert but... I think HTTP intends that GET is used for things like searching (data is not changed server-side) and POST is meant for things that do cause changes (and should not be repeated, hence all the browser warnings when you do a refresh.) Maybe this idea is a bit dated. I know when I look at other search engines, it's based on queries in the URL. Not sure if this is obvious, but if you wanted to use CI and GET for this, can you make sure the URL gets encoded to allow all characters? (I'm learning CI at the moment.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.