Multipage function, not showing correct results for next page

Hi, I am having a nightmare trying to fix a multipage search string problem. Can some one let me know if this can be done… Within a database of 200 posts (15 posts per page) you have a simple search field, that searches through each cell of data inserted. When the search results return and there are more than 15, when you click the Next page link it will display just the relevant results for the second page.

And if there is no search made (just shows every post in the database) when you click the Next page link it will go to the second page of posts.

Is this achievable? - I have been working on this since the crack of dawn (UK time) and have had no joy.

Cheers,

Paul

They call this “pagination”. You can search that term.

Ok, I have just done some reading on pagnation, and I already do that - If I have 90 rows in my table, I would have 6 seperate pages as I have it setup to 15 posts per page (which if I read it correctly is pagination).

I can go through all 6 pages no problem by clicking the Next and Previous links.

But as soon as I do a search, the results show all the relevant search criteria only on the first page. As soon as I click next, it will just load the default second page of data posts rather than the search criteria.

I thought setting the body with the search string and then having the link like /db/post/index.php?search_string=My Search&page=2 when you search My Search would work, but I had no joy with this. Does any one have any other suggestions as to another method I could try?

That’s a great way to do it. Even google uses it.

Check it out:
http://www.google.com/search?q=php+pagination&start=10

Yeah, see I thought that it seemed like a good way - but for some peculiar reason it’s not working for me. This is how I set the search string for the body:

$body->set('search_string', isset($_REQUEST['search_string']) ? check_input($_REQUEST['search_string']) : '');

And this is the multipage link:

<?php echo multipage(get_num_posts(), 15, $page, '/admin/database/posts/index.php?search_string=' . $search_string . '&amp;page='); ?>

I am not sure if it is something to do with the multipage function, I am pretty sure that it’s not, but heres the code for that.

<?php function multipage($num, $per_page, $page, $page_url, $show_next_links = true) {
	$multipage = array();

	if ($num > 0) {
		$multipage[] = 'Page:';
	}

	if ($page > 1 && $show_next_links == true) {
		$multipage[] = '<a href="' . $page_url . 1 . '">&laquo;</a>';
		$multipage[] = '<a href="' . $page_url . ($page - 1) . (substr($page_url, -1) == '/' ? '/' : '') . '">&#8249;</a>';
	}

	for ($i = ($page <= 2 ? 1 : $page - 2); $i <= ($page >= ceil($num / $per_page) - 2 ? ceil($num / $per_page) : $page + 2); $i++) {
		$multipage[] = $i == $page ? '<u>' . $i . '</u>' : '<a href="' . $page_url . $i . (substr($page_url, -1) == '/' ? '/' : '') . '">' . $i . '</a>';
	}

	if ($page < ceil($num / $per_page) - 2) {
		$multipage[] = '... ' . '<a href="' . $page_url . ceil($num / $per_page) . (substr($page_url, -1) == '/' ? '/' : '') . '">' . ceil($num / $per_page) . '</a>';
	}

	if ($page < ceil($num / $per_page) && $show_next_links == true) {
		$multipage[] = '<a href="' . $page_url . ($page + 1) . (substr($page_url, -1) == '/' ? '/' : '') . '">&#8250;</a>';
		$multipage[] = '<a href="' . $page_url . ceil($num / $per_page) . (substr($page_url, -1) == '/' ? '/' : '') . '">&raquo;</a>';
	}

	return implode('&nbsp;', $multipage);
} ?>

This has been one of those days where I have scracthed my head until there is no hair left and had no success. - I thought the method I wrote could be done, but I just can’t seem to pull it off.

Do some debugging. It’s how you make code work. You need to understand what the code is actually doing in order to have any hope to fix it.

Check the values of all variables and function return values, to make sure they contain the values you expect them to.
Check your conditional statements(if, loops, etc…) to see what the results of those statements are.

Here’s an example


function multipage($num, $per_page, $page, $page_url, $show_next_links = true) { 
    $multipage = array(); 
    echo $num;
    echo $per_page;
    if ($num > 0) {
        echo 'test point 1';
        $multipage[] = 'Page:'; 
    } else {
        echo 'test point 2';
    }

Run that, and then if everything checks out ok, proceed to test code before or after that point. Do this until you find something that is not what you expected.

When checking the values of variables, make use of var_dump() and print_r() as they give a lot more info than echo. I only used echo in my example to get the point across.