Jump to content

Pagination Cannot Select A Second Page


mallen

Recommended Posts

I have this list of files that are dynamically listed on a admin page. It lists all the files and I can control the amount per page. Also I can add and delete. So I know all those functions work fine. Here is the problem.

I am trying to use a pagination system. It has a drop down box with page numbers. But it will only alow me to select the page once. It lists for example all 4 pages. It doesn't matter if I select page two or four or what order. If I select another page it launches a different page and exits the pagination ect.. breadcrumb=1 Is there anything in my code that could cause this?

 

class literature
{
protected $numPerPage, $getLits, $totalLit;

function __construct()
{
$this->numPerPage = 9;
$this->getLit = $this->getLits();
$this->totalLit = count($this->getLits(true));
}

function getLits($count = false)
{
$curPage = 0;
$ltCategory = NULL;

if(isset($_REQUEST['breadcrumb']))
$curPage = $this->numPerPage * $_REQUEST['breadcrumb'];

if($count)
$curPage = -1;

return $this->getLitQuery($curPage);
}

function getLitQuery($curPage = 0)
{
global $wpdb;
$litQuery = "SELECT * FROM literature ORDER BY lit_id ASC";

if($curPage !== -1) $litQuery .= " LIMIT $curPage,". $this->numPerPage;
return $wpdb->get_results($wpdb->prepare($litQuery), ARRAY_A);
}



function getLitInfo($id)
{
global $wpdb;
$query =
$vals = $wpdb->get_row($wpdb->prepare("SELECT * FROM literature WHERE `lit_id`='$id' LIMIT 1"), ARRAY_A);

return $vals;

}


function getBreadcrumbs()
{
$output ="";
$totalPages = $this->totalLit / $this->numPerPage;

if($totalPages > 2)
{

$output .= "<label>Page:</label>";
$output .= "<select id='breadcrumb_page' onchange='getNewBreadcrumbPage(this.value,\"";

$output .= "\");'>";
for($i=1; $i<=ceil($totalPages); $i++)
{
$output .= "<option value='$i'";
if(isset($_REQUEST['breadcrumb']) && $_REQUEST['breadcrumb'] == $i -1)
$output.= "selected";
$output .= ">$i</option>\n";
}
$output .= "</select>";
echo $output;
}
}

}
And here are the javascript funtions:

function getNewBreadcrumbPage(p, c)
{
var newpage = p - 1;
var location = document.location.href;
var existing = gup('breadcrumb');

if(!existing)
window.location = location + '&breadcrumb=' + newpage;
else
{
window.location ="?page=edit_literature&productCategory=" + c +"&breadcrumb="+ newpage;
}

}

function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return false;
else
return true;
}


Edited by mallen
Link to comment
Share on other sites

Check that statement again and tell us where the code gets stuck.

 

if($totalPages > 2)
{
$output .= "<label>Page:</label>";
$output .= "<select id='breadcrumb_page' onchange='getNewBreadcrumbPage(this.value,\"";
$output .= "\");'>";
for($i=1; $i<=ceil($totalPages); $i++)
{
$output .= "<option value='$i'";
if(isset($_REQUEST['breadcrumb']) && $_REQUEST['breadcrumb'] == $i -1)
$output.= "selected";
$output .= ">$i</option>\n";
}
$output .= "</select>";
echo $output;
}
}

 

EDIT: also check the type of $totalPage:

 

echo gettype($totalPages)

Edited by jazzman1
Link to comment
Share on other sites

I think this line may be the issue

output .= "<select id='breadcrumb_page' onchange='getNewBreadcrumbPage(this.value,\"";

 

I can't tell what is getting stuck. For example it give choice of 1,2, 3. You can pick any one the first time. If you pick any other page after this first selection it loads another page. Seems it loses track of what page and breadcrumb its on. The page is page=edit-literature&breadcrumb=2 and it will leave this.

Edited by mallen
Link to comment
Share on other sites

I removed if(isset($_REQUEST['breadcrumb']) && $_REQUEST['breadcrumb'] == $i -1)

 

And that didn't work. Got the same results. It needs a dynamic variable for the bread crumb that gets fed to the switch statement.

Edited by mallen
Link to comment
Share on other sites

Wrong html!

 

I've tried that one and it works:

<?php
$totalPages = 2.8888888888889;
$output ="";
if($totalPages > 2) {
$output .= "<label>Page:</label>";
$output .= "<select id=breadcrumb_page onchange=getNewBreadcrumbPage(this.value)>";
for($i=1; $i<=ceil($totalPages); $i++)
{
$output .= "<option value='$i'";
if(isset($_REQUEST['breadcrumb']) && $_REQUEST['breadcrumb'] == $i -1)
$output.= "selected";
$output .= ">$i</option>\n";
}
$output .= "</select>";
echo $output;
}

?>

<script type="text/javascript">
function getNewBreadcrumbPage(data){

 alert(data);

}


</script>

 

EDIT: I added and your javascript function.

Don't use $REQUEST !

Edited by jazzman1
Link to comment
Share on other sites

Thanks Jazzman1. Not sure what I should use instead of _REQUEST.

 

I tired your code and yes it works by itself. But the URL is not changing. Also my total pages will need to be dynamic. Its displaying data to the javascript pop up window. But in the complete set up it still fails after you select a second page. In any order.

 

My url reads ?page=edit-literature&breadcrumb=1

 

Then I choose and page My url reads ?page=edit-literature&breadcrumb=2

 

And the second page in any order whether its page 1 again or page 3 it goes :

 

?page=different_page_alltogether&breadcrumb=0

 

And of course the admin page and pagination is all gone because its on another page.

Edited by mallen
Link to comment
Share on other sites

I was able to get it fixed. I am sure it had to do with a cookie being set. I learned using _REQUEST includes $_COOKIE. I use some similar pages on other sites I have installed locally and there may be a conflict. I did a search with Dreamweaver and found no other function that could be sending me to another URL. I changed _REQUEST to _POST and to _GET and got a permissions error message. Then I cleared my history and cookies and set it back to _REQUEST. I noticed it was sending me to a URL with and extra '=' and found that in the function inside the javascript. Also I renamed the link on the onchange to make sure it was a unique function and it worked.

function getBreadcrumbs()
{
$output ="";
$totalPages = $this->totalLit / $this->numPerPage;

if($totalPages > 2)
{

$output .= "<label>Page:</label>";
$output .= "<select id='breadcrumb_page' onchange='getNewBreadcrumbPageLiterature(this.value)';>";

$output .= "\");'>";
for($i=1; $i<=ceil($totalPages); $i++)
{
$output .= "<option value='$i'";
if(isset($_REQUEST['breadcrumb']) && $_REQUEST['breadcrumb'] == $i -1)

$output.= " selected";
$output .= ">$i</option>\n";
}
$output .= "</select>";
echo $output;
}
}

and the Javascript file that is linked:
function getNewBreadcrumbPageLiterature(p)
{
var newpage = p - 1;
var location = document.location.href;
var existing = gup('breadcrumb');

if(!existing)
window.location = location + '&breadcrumb=' + newpage;
else
{
window.location ="?page=edit-literature" +"&breadcrumb="+ newpage;
}

}

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.