Jump to content

Pagination Help


LemonSquid

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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']

Link to comment
Share on other sites

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 )

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.