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

pythonian29033
  • 5,148
  • 5
  • 31
  • 56
ThorDozer
  • 104
  • 2
  • 12

2 Answers2

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?

Ragnar123
  • 5,174
  • 4
  • 24
  • 34
  • because it's a research function so naturally the input should be in the url, no? – ThorDozer Apr 07 '11 at 01:12
  • Alright, thats probably just preferences :-) Try the suggested solution. – Ragnar123 Apr 07 '11 at 01:15
  • how can i set my $search variable this way? – ThorDozer Apr 07 '11 at 01:15
  • 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 – ThorDozer Apr 07 '11 at 01:16
  • 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.) – Guttsy Apr 07 '11 at 01:16
  • You have not adjusted the form properly. To see if it works, try typing into the address bar: `yourwepage.org/recherche2/SOMEKEYWORD` – Ragnar123 Apr 07 '11 at 01:18
  • @Gutsy: this is the Reg Ex use: 'a-z 0-9~%.:_\-' – ThorDozer Apr 07 '11 at 01:18
  • @Ragnar: it works but when i make an alert.. it only show the first character : yourwepage.org/recherche2/LAPTOP (it shows only 'L') AND i want to pass to an input type text to generate the results and not directly by the url (if it's possible) – ThorDozer Apr 07 '11 at 01:21
  • Read some discussions on the topic: http://stackoverflow.com/questions/334708/how-to-allow-codeigniter-get-parameters – Ragnar123 Apr 07 '11 at 01:35
  • @Ragnar: I read it but there's subtility in it that can modify my code (via config file) – ThorDozer Apr 07 '11 at 01:44
  • @Ragnar: maybe there's a way to take an input and put it in the url? (i don't know why because i really need to use a GET) – ThorDozer Apr 07 '11 at 01:46
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>';
}
icchanobot
  • 3,323
  • 27
  • 37
  • 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 – ThorDozer Apr 07 '11 at 01:27
  • 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. – icchanobot Apr 07 '11 at 02:14
  • i put the querystrings as True – ThorDozer Apr 07 '11 at 02:23
  • but it won't work either.. i try the $this->input->get('search', true) but its not working – ThorDozer Apr 07 '11 at 02:35
  • um.. what does your form look like? i just tried this and it works: public function test() { echo '['.$this->input->get('search').']'; echo '
    '; echo ''; echo '
    '; }
    – icchanobot Apr 07 '11 at 03:12
  • its should go into your controller, it will display a form which posts a single text field to itself. then you can see the posted value inside the square brackets (if everything is working). – icchanobot Apr 08 '11 at 04:18