Problem with pagination

#1
[eluser]Docmo[/eluser]
Hi,

I'm sorry if my english is not good, it's my 4th language..

I have a problem with the pagination, after generating the results with a search engine.

Here is my code (in the controller)

Code:
if(isset($_POST['categorie']) && isset($_POST['contenu']))
        {
            $tmp=3;
            if($this->uri->segment(4)){
                $query = $this->Model_job->getResult($_POST['categorie'], $_POST['contenu'], $this->uri->segment(4) ,$tmp);
                $query2 = $this->Model_job->getResult($_POST['categorie'], $_POST['contenu'], 0 ,0);
            }
            else
            {
                $query = $this->Model_job->getResult($_POST['categorie'], $_POST['contenu'], '0' , $tmp);
                $query2 = $this->Model_job->getResult($_POST['categorie'], $_POST['contenu'], 0 , 0);
            }
            $search = explode(' ', $_POST['contenu']);
            $req = 'http://www.job24.ca/job/resultats/';
            $req = $req.$search[0];
            for($i=1;$i<count($search);$i++)
            {
                $req=$req."+".$search[$i];
            }
            $config['base_url'] = $req;
            $config['total_rows'] = $this->countRes($query2);
            $config['per_page'] = $tmp;
            
            $this->pagination->initialize($config);
            $data['links']=$this->pagination->create_links();
            $data['res'] = $query;
            $this->load->view('resultats',$data);
        }
        else
        {
            $tmp=3;            
                    
            
            if($this->uri->segment(3)){
                $query = $this->Model_job->getResult('', '', $this->uri->segment(3) , $tmp);
                $query2 = $this->Model_job->getResult('', '', 0 , 0);
            }
            else{
                $query = $this->Model_job->getResult('', '', '0' , $tmp);
                $query = $this->Model_job->getResult('', '', 0 , 0);
            }
        
            
            $config['base_url'] = 'http://www.job24.ca/job/resultats';
            $config['total_rows'] = $this->countRes($query2);
            $config['per_page'] = $tmp;

            $this->pagination->initialize($config);
            $data['links']=$this->pagination->create_links();
            $data['res'] = $query;
            $this->load->view('resultats',$data);
            
        }
    }

The code in the "else" works perfectly, and so the pagination. (it's when the user doesn't enter any key words in the search engine)
But in the first if, the code inside is not working. When the user enters 2 key words ("word1 word2" for example) or even 1 key word, the code generates the results with the pagination, but when i click on "2" or "3", i obtain an error, and i don't understain why when i click on "2" the is executed and not the first if.

I hope that you understood my problem, and thank you for helping me!


...

#2
[eluser]Dam1an[/eluser]
Hi, welcome to the forums

What error message do you get? (my telepathy isn't up to it's normal standard today Wink)

#3
[eluser]Docmo[/eluser]
Ok so i have to show you my function in the model! But as i said, the problem is when clicking on "2" the code in the else is executed.. well ok here is my model function : this is the code executed when typing a search..
Code:
else if(strlen($categorie)==0 && strlen($contenu)!=0){
            $search = explode(' ', $contenu);
            $req = "'%".$search[0]."%'";
            for($i=1;$i<count($search);$i++)
            {
                $req=$req." OR '%".$search[$i]."%'";
            }
            if($limit1==0 && $limit2==0)
            $data = $this->db->query("SELECT * FROM offres WHERE titre LIKE ".$req." OR description LIKE ".$req);
            else
            $data = $this->db->query("SELECT * FROM offres WHERE titre LIKE ".$req." OR description LIKE ".$req." LIMIT ".$limit1." , ".$limit2);
            if(count($data->result_array())==0)
            {
                return'0';
            }
        }

But when clinking on "2", i don't now why it executes again the model, and this time this part is executed

Code:
else if(strlen($categorie)==0 && strlen($contenu)==0){
            if($limit1==0 && $limit2==0)
            $data = $this->db->query("SELECT * FROM offres ORDER BY id DESC ");
            else
            $data = $this->db->query("SELECT * FROM offres ORDER BY id DESC LIMIT ".$limit1." , ".$limit2);
            if(count($data->result_array())==0)
                return'0';
        }

The error is :
A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'test , 3' at line 1

SELECT * FROM offres ORDER BY id DESC LIMIT test , 3


You can visit the website to understand the problem : www.job24.ca (i'm working on it..) type "test" in the search engine and then click on "2"

Thanks for the help


...

#4
[eluser]Dam1an[/eluser]
If you manipulate the URL to reove the word 'test' it seems to work fine (and all clicks after that also work)
Think you might need to either tweak the base URL or some URI segments (I would help more but about to go offline now)

#5
[eluser]Docmo[/eluser]
[quote author="Dam1an" date="1245986173"]If you manipulate the URL to reove the word 'test' it seems to work fine (and all clicks after that also work)
Think you might need to either tweak the base URL or some URI segments (I would help more but about to go offline now)[/quote]

Not really, true the pagination works, but if you continue by clicking on "4" you'll see results that don't contain the word "test" Sad

I have to make it work with the url http://www.job24.ca/job/resultats/test/3 !!

Thanks for the help Smile


...

#6
[eluser]Dam1an[/eluser]
So the problem is it seems to be taking the search term as the first part of the limit clause (and I don't have the part where you set that, so I'm not sure whats going on there)

$limit1 should be segment(4) and
$limit2 should be 3

Is that the case?

#7
[eluser]Docmo[/eluser]
Yes!

Ok i'm gonna start from the beginning, the view, here is the code for the seach engine in the view :

Code:
&lt;?php echo form_open('job/resultats');?&gt;
   &lt;input type="text" name="contenu" value="Rechercher une offre dans" size="30" &gt;
   <select  name="categorie">
      <option value="" selected>Toutes les offres</option>
      <option value="Babysitting">Babysitting</option>
      <option value="Éducation">Éducation</option>
      <option value="Agriculture">Agriculture</option>
    </select>
    &lt;input type="submit" value="Recherche" /&gt;
    &lt;/form&gt;

So, if we type the word "test", we obtain the link http://www.job24.ca/job/resultats with the 3 firts results containing the word "test".

The code in the controller :

Code:
$tmp=3;
            if($this->uri->segment(4)){ //executed if the user click on "2" or more
                $query = $this->Model_job->getResult($_POST['categorie'], $_POST['contenu'], $this->uri->segment(4) ,$tmp);
                $query2 = $this->Model_job->getResult($_POST['categorie'], $_POST['contenu'], 0 ,0);
            }
            else//executed when clicking on "1"
            {
                $query = $this->Model_job->getResult($_POST['categorie'], $_POST['contenu'], '0' , $tmp);//only 3 results
                $query2 = $this->Model_job->getResult($_POST['categorie'], $_POST['contenu'], 0 , 0);//to obtain the total number of results
            }


            $search = explode(' ', $_POST['contenu']);//generating a link containing the key words
            $req = 'http://www.job24.ca/job/resultats/';
            $req = $req.$search[0];
            for($i=1;$i<count($search);$i++)
            {
                $req=$req."-".$search[$i];
            }
                        //configuring the pagination
            $config['base_url'] = $req;
            $config['total_rows'] = $this->countRes($query2);
            $config['per_page'] = $tmp;
            
            $this->pagination->initialize($config);
            $data['links']=$this->pagination->create_links();
            $data['res'] = $query;
            $this->load->view('resultats',$data);

So the problem is, when we click on 2, the browser reload the page, but when it happens, the browser forgets our query in the search engine, and executes another part in the code but he hasn't anymore the $_POST['categorie'] and $_POST['contenu'], so we obtain a SQL error because we lost a variable!

Hope you better understand the problem!

Thx!


...


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.