LemonSquid Posted March 21, 2012 Share Posted March 21, 2012 Hi guys, I have a question about pagination. It's not really about the coding side so I'm really sorry if this is the wrong section! Ok my question is, does pagination actually create multiple pages? I'm really new to PHP and I'm just reading through different things and tutorials to try and figure it out if you will. So does it create new pages? From my understanding it simply changes a variable to a page number and uses that page number (and simple matH) to figure out what rows to show. If this is the case, how do people use pagination to create links such as http://example.com/imnooby/page-1 http://example.com/imnooby/page-2 http://example.com/imnooby/page-3 Surely if this was all just one page, it would just be http://example.com/imnooby/page and the variable would change depending on the button, but the same page would be reloaded? This is the part that's really bugging me and any explanation would really help. Quote Link to comment Share on other sites More sharing options...
scootstah Posted March 21, 2012 Share Posted March 21, 2012 By using mod_rewrite rules or with URI routing. Quote Link to comment Share on other sites More sharing options...
LemonSquid Posted March 22, 2012 Author Share Posted March 22, 2012 Ahh, ok. So there will be a little script where if the page does exist then it will ignore mod_rewrite but if it doesn't then it will use it and create the page? Thanks so much, will look into this! Quote Link to comment Share on other sites More sharing options...
LemonSquid Posted March 22, 2012 Author Share Posted March 22, 2012 Hi again, could you help me with the rewrite rule? I've been googling but can't get it working on my test page. I can get the pagination working in the sense of limits and goes to &page2 etc but I can't do rewrite to make the $page um Id actually change, it stays at 1. I know this is wrong section but I already had the thread so thought it was worth a try Quote Link to comment Share on other sites More sharing options...
scootstah Posted March 22, 2012 Share Posted March 22, 2012 Usually it's something like <IfModule mod_rewrite.c> RewriteEngine on Options +FollowSymLinks -Indexes RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] </IfModule> All requests will go through index.php and then you throw some logic in there to direct traffic. Your links would look like example.com/segment1/segment2/segment3 ... etc You can access them by exploding $_SERVER['REQUEST_URI'] Quote Link to comment Share on other sites More sharing options...
LemonSquid Posted March 22, 2012 Author Share Posted March 22, 2012 Thx, I added the code you said to my htaccess but I don't really know what it's doing :\ I also echo'd (explode(" ",$_SERVER['REQUEST_URI'])) and it just prints 'Array' - am I using explode wrong? (this will only be 2nd time I've used it) Quote Link to comment Share on other sites More sharing options...
scootstah Posted March 22, 2012 Share Posted March 22, 2012 Like I said, that rewrite rule filters all traffic through the index.php file. So example.com/something becomes example.com/index.php/something. You can't echo arrays, you need to use something like print_r or var_dump. Also, you need to explode it by a slash, /. print_r(explode('/', $_SERVER['REQUEST_URI'])); Quote Link to comment Share on other sites More sharing options...
LemonSquid Posted March 22, 2012 Author Share Posted March 22, 2012 I should probably look into mod_rewrite before asking these questions but I literally copy and pasted (sorry!) what you gave me and it does nothing to my url. Was I supposed to change something? My current url is /index.php?pagenum=X Thanks for the explode help, it returned Array ( [0] => [1] => index.php?pagenum=2 ) Quote Link to comment Share on other sites More sharing options...
LemonSquid Posted March 22, 2012 Author Share Posted March 22, 2012 If I had the array returning as: Array ( [0] => [1] => index.php => 2 ) would I then be able to do $getnum = (explode('/', $_SERVER['REQUEST_URI'])); $pagenum = $getnum[3] ? Quote Link to comment Share on other sites More sharing options...
scootstah Posted March 22, 2012 Share Posted March 22, 2012 It doesn't change your URL, you have to do that. It changes what the URL does, where the request goes. You need to have some kind of logic in your index.php file to further route traffic. For example, you can now do this: example.com/view/some-article/17. $_SERVER['REQUEST_URI'] will now return "index.php/view/some-article/17". If you explode it by a forward-slash you can split up the segments and use them accordingly. $segments = explode('/', $_SERVER['REQUEST_URI']); // get rid of the "index.php" segment array_shift($segments); // you are left with : array( view, some-article, 17 ) // we'll assume the first segment is for a page, so lets get that require $segments[0] . '.php'; // inside "view.php" we can then access the other segments to load an article and use pagination This is crudely basic and has a lot of limitations, but you can extend the functionality with more advanced routing. Read the docs for the CodeIgniter router to get a better understanding of how it works. The code handleing CodeIgniters routing is very simplistic, I would recommend you check it out and use it to come up with your own solution. The code is located in /system/core/Router.php. The actual routing takes place in the "_parse_routes" method; function _parse_routes() { // Turn the segment array into a URI string $uri = implode('/', $this->uri->segments); // Is there a literal match? If so we're done if (isset($this->routes[$uri])) { return $this->_set_request(explode('/', $this->routes[$uri])); } // Loop through the route array looking for wild-cards foreach ($this->routes as $key => $val) { // Convert wild-cards to RegEx $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key)); // Does the RegEx match? if (preg_match('#^'.$key.'$#', $uri)) { // Do we have a back-reference? if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE) { $val = preg_replace('#^'.$key.'$#', $val, $uri); } return $this->_set_request(explode('/', $val)); } } // If we got this far it means we didn't encounter a // matching route so we'll set the site default route $this->_set_request($this->uri->segments); } This way, you can have all sorts of powerful dynamic URL's and easily accomplish your original problem. Quote Link to comment Share on other sites More sharing options...
LemonSquid Posted March 22, 2012 Author Share Posted March 22, 2012 I'll definitely research this when I get home but for now I used: $split = explode('=', $_SERVER['REQUEST_URI']); array_shift($split); $pagenum = $split[0]; as it's the easiest I could think of, major thankyou though dude!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.