Welcome Guest, Not a member yet? Register   Sign In
I have a problem with pagenition pls help
#1

[eluser]Fahad Alrahbi[/eluser]
Hello ,all


i am using codeigniter with Smarty template engine and when i try to use pagenition it's working but if Url is

http://localhost/cic/showq/viewv/8
and i add (') or (-) i got error
after add
http://localhost/cic/showq/viewv/-8


error msg is

Quote: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 '-8, 4' at line 3

SELECT * FROM (`topic`) LIMIT -8, 4

Filename: C:\xampp\htdocs\cic\system\database\DB_driver.php

Line Number: 330


My Code
controllers :


Code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
All Copyrights reserved.
Programmer information
Name : Fahad Salim Alrahbi
Location: Sultanate of Oman - Muscat
Email : [email protected]
Tel : +968 98985262
*/

class showq extends CI_Controller{
    
    
    function viewv(){
        $this->load->library('pagination');
        
        $config['base_url'] = 'http://localhost/cic/showq/viewv';
        $config['total_rows'] =$this->db->get('topic')->num_rows();
      
        $config['per_page'] = 4;
        $config['num_links']=5;
        $config['first_link'] = 'Start';
        $config['last_link'] = 'End';
        $config['prev_link'] = 'Back';
        
    
        $this->pagination->initialize($config);
      
        $s=$this->db->get('topic',$config['per_page'],$this->uri->segment(3));
         foreach($s->result() as $datar){
            
            $row[]=$datar;
         }      
        $this->parser->assign('pages',$this->pagination->create_links());
        $this->parser->assign('row',$row);
        $this->parser->parse('showq.tpl');
    }
}
?>


Template showq.tpl

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html xml:lang="en" lang="en"&gt;

&lt;head&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=UTF-8" /&gt;
&lt;meta name="author" content="PHPDes7" /&gt;

&lt;title&gt;View data &lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

{section name='idx' loop=$row}

{$row[idx]->id}| {$row[idx]->topic} <br />

{/section}

<br />

{$pages}







&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]skunkbad[/eluser]
The pagination class needs to use a number, not a number with extra chars you feel like adding. Why do you need those extra chars? Maybe re-read the user guide section on pagination; it's really easy to do when you follow the guide.
#3

[eluser]Fahad Alrahbi[/eluser]
Hello ,

Thanks for reply


i want if visitor change the page url he will go to first page not showing db error

is my code correct ? im new in codeigniter
#4

[eluser]johanriyan[/eluser]
hello,

i have same error.

how to fix.
#5

[eluser]Ckirk[/eluser]
You could make use of the form validation library to determine whether $this->uri->segment(3) is actually a number. I've changed your code so try the code below. It's untested but should work.
I haven't accounted for anyone entering a valid page number, which exceeds the number of records in the database. In this scenario your query would return zero results but I'm sure you know how to handle that - at least there's no DB/SQL errors now Smile


Code:
&lt;?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
All Copyrights reserved.
Programmer information
Name : Fahad Salim Alrahbi
Location: Sultanate of Oman - Muscat
Email : [email protected]
Tel : +968 98985262
*/

class showq extends CI_Controller{
    
    
    function viewv(){
        $this->load->library(array('pagination','form_validation'));   // don't forget to load form_validation lib
        
        $config['base_url'] = 'http://localhost/cic/showq/viewv';
        $config['total_rows'] =$this->db->get('topic')->num_rows();
      
        $config['per_page'] = 4;
        $config['num_links']=5;
        $config['first_link'] = 'Start';
        $config['last_link'] = 'End';
        $config['prev_link'] = 'Back';
        
        // Set the $page to be 0 if the number in the URI is crap
        $page = !$this->form_validation->is_natural($this->uri->segment(3)) ? 0 : $this->uri->segment(3);
    
        
    
        $this->pagination->initialize($config);
      
        $s=$this->db->get('topic',$config['per_page'],$page);
         foreach($s->result() as $datar){
            
            $row[]=$datar;
         }      
        $this->parser->assign('pages',$this->pagination->create_links());
        $this->parser->assign('row',$row);
        $this->parser->parse('showq.tpl');
    }
}
?&gt;
#6

[eluser]johanriyan[/eluser]
thks guys,

Code:
$page = !$this->form_validation->is_natural($this->uri->segment(3)) ? 0 : $this->uri->segment(3);

how to show show_404();
if page not valid.


#7

[eluser]Pert[/eluser]
You can force 404 error, there are some examples on this page

http://stackoverflow.com/questions/12367...cing-a-404

You could also help visitor out a little and normalise the values

Code:
if ($page < 1)
   $page = 1;
else if ($page > $total_pages)
   $page = $total_pages;

This way they will still see meaningful information rather than are forced to 404 page. Lets say there is only one item on the last page that they are trying to access and you delete said item, they will end up on 404 page when in fact it would be more helpful if it just lands on last page available.
#8

[eluser]johanriyan[/eluser]
when i am add (-) in URL,
i want show such as image.

how to create.
#9

[eluser]Ckirk[/eluser]
[quote author="johanriyan" date="1372131698"]when i am add (-) in URL,
i want show such as image.

how to create.[/quote]

Code:
if(!$this->form_validation->is_natural($this->uri->segment(3))) show_error('You are not authorised to perform this action');




Theme © iAndrew 2016 - Forum software by © MyBB