#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    25
    Rep Power
    0

    Pagination Link to open in the same div


    Hi, I need to make my pagination links to open in the same div as the pagination pages are.

    The index.php contains 3 divs, left, main and right. When a link on the left div is clicked, it will open arr0.php into main div with the pagination. But when I click on page 2, the arr0.php was opened into a new window.

    My pagination code:

    foreach(range($page_start,$page_end,1) as $i) {
    $return.= sprintf(
    '<li%s>%s%s%s</li>'
    ,$page == $i?' class="current"':''
    ,$page == $i?'':sprintf('<a href="%s">',str_replace($page_flag,$i,$base_path))

    ,'&nbsp-&nbsp'
    ,$i
    ,$page == $i?'':'</a>'
    );

    --------------

    I tried inserting

    "javascript:ajaxpage('arr0.php', 'main') into the <a href="%s"> but still not able to.

    Can someone help me, many thanks.
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    12
    We would need to see the ajaxpage function in order to see what's going on there ...
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    25
    Rep Power
    0
    OK, here is my page code.

    ----

    [PHP=""]
    class Pagination {

    protected

    //Configuration defaults
    $_arrConfig = array(
    'limit'=>20
    ,'page'=>1
    ,'basePath'=>'#'
    ,'label'=>'Items'
    ,'visiblePages'=>7
    ,'pageFlag'=>'{page}'
    )

    //Data to be passed to template
    ,$_arrTemplateData = array()

    //function to pass resolved offset and limit
    ,$_onPaginate

    //extra arguments to pass to to pagination callback such as data source or db connection
    ,$_arrArgs;

    public function __construct($onPaginate,$arrConfig=array(),$arrArgs=array()) {

    $this->_onPaginate = $onPaginate;
    $this->_arrArgs = $arrArgs;

    foreach($arrConfig as $strKey=>$mixValue) {
    if(array_key_exists($strKey,$this->_arrConfig)) {
    $this->_arrConfig[$strKey] = $mixValue;
    }
    }
    }

    public function paginate() {

    //Calculate page offset
    $intOffset = ($this->_arrConfig['page']-1)*$this->_arrConfig['limit'];

    //Paginate and get number of found rows
    $intFoundRows = call_user_func_array($this->_onPaginate,array_merge(array($intOffset,$this->_arrConfig['limit']),$this->_arrArgs));

    //Assign template data
    $this->_arrTemplateData['limit'] = $this->_arrConfig['limit'];
    $this->_arrTemplateData['page'] = $this->_arrConfig['page'];
    $this->_arrTemplateData['offset'] = $intOffset;
    $this->_arrTemplateData['found_rows'] = $intFoundRows;
    $this->_arrTemplateData['total_pages'] = $intFoundRows < $this->_arrConfig['limit']?1:ceil($intFoundRows/$this->_arrConfig['limit']);
    $this->_arrTemplateData['visible_pages'] = $this->_arrConfig['visiblePages'];
    $this->_arrTemplateData['base_path'] = $this->_arrConfig['basePath'];
    $this->_arrTemplateData['label'] = $this->_arrConfig['label'];
    $this->_arrTemplateData['page_flag'] = $this->_arrConfig['pageFlag'];

    //Return the pagination template
    return $this->display();
    }

    public function display() {
    extract($this->_arrTemplateData);

    if($total_pages <= $visible_pages) {
    $page_start = 1;
    $page_end = $total_pages;
    } else if($page <= ceil($visible_pages/2)) {
    $page_start = 1;
    $page_end = $visible_pages;
    } else if($page > ($total_pages - ceil($visible_pages/2))) {
    $page_start = $total_pages - (ceil(($visible_pages/2)*2)-1);
    $page_end = $total_pages;
    } else {
    $page_start = $page-(floor($visible_pages/2));
    $page_end = $page+(floor($visible_pages/2));
    }

    $return.= sprintf('<ul class="pagination">');
    $return.= sprintf(
    '<li class="first">%s%s%s</li>'
    ,$page == 1?'':sprintf('<a href="%s">',str_replace($page_flag,1,$base_path))
    ,'First'
    ,'&nbsp-&nbsp'
    ,$page == 1?'':'</a>'
    );
    $return.= sprintf(
    '<li class="previous">%s%s%s</li>'
    ,$page == 1?'':sprintf('<a href="%s">',str_replace($page_flag,($page-1),$base_path))
    ,'Previous'
    ,$page == 1?'':'</a>'
    );
    foreach(range($page_start,$page_end,1) as $i) {
    $return.= sprintf(
    '<li%s>%s%s%s</li>'
    ,$page == $i?' class="current"':''
    ,$page == $i?'':sprintf('<a href="%s" onclick="pagediv()">',str_replace($page_flag,$i,$base_path))

    ,'&nbsp-&nbsp'
    ,$i
    ,$page == $i?'':'</a>'
    );
    }
    $return.= sprintf(
    '<li class="next">%s%s%s</li>'
    ,$page == $total_pages?'':sprintf('<a href="%s">',str_replace($page_flag,($page+1),$base_path))
    ,'&nbsp-&nbsp'
    ,'Next -&nbsp'
    ,$page == $total_pages?'':'</a>'
    );
    $return.= sprintf(
    '<li class="last">%s%s%s</li>'
    ,$page == $total_pages?'':sprintf('<a href="%s">',str_replace($page_flag,$total_pages,$base_path))

    ,'Last'
    ,$page == $total_pages?'':'</a>'
    );
    $return.= sprintf('</ul>');

    return $return;

    }

    }

    class Action {

    private $_objXML;
    private $_arrMessages = array();

    public function __construct() {

    $this->_objXML = simplexml_load_file("catalog.xml");

    }

    public function paginate($offset,$limit) {

    $arrMessages = $this->_objXML->Category[0]->Site;


    if(isset($arrMessages[$offset])) {
    for($i=0;$i<$limit;$i++) {

    if(!isset($arrMessages[($i+$offset)])) break;

    $ID = $arrMessages[($i+$offset)]->Id;
    $affid = "test.";
    $url = "http://".$affid.$ID.".net";

    $this->_arrMessages[] = array(
    'Title'=>"<a href=\"$url\">".(string) $arrMessages[($i+$offset)]->Title.'</a>'
    ,'Description'=>(string) $arrMessages[($i+$offset)]->Description
    ,'ActivateDate'=>(string) $arrMessages[($i+$offset)]->ActivateDate
    );
    }
    }

    // return number of total found rows
    return count($arrMessages);

    }

    public function getMessages() {
    return $this->_arrMessages;
    }

    }

    $action = new Action();

    $func = create_function('$offset,$limit,$action','return $action->paginate($offset,$limit);');
    $pg = new Pagination($func,array(
    'basePath'=>$_SERVER['PHP_SELF'].'?p={replace_me_with_page}'
    ,'pageFlag'=>'{replace_me_with_page}'
    ,'label'=>'Messages'
    ,'limit'=>20
    ,'page'=>isset($_GET['p']) && is_numeric($_GET['p']) && $_GET['p'] != 0?$_GET['p']:1
    ,'visiblePages'=>7
    ),array($action));

    [/PHP]

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>Catalog</title>
    <style type="text/css">
    .pagination {
    text-align: center;
    }
    .pagination li {
    display: inline-block;
    }
    *+html .pagination li {
    display: inline;
    }
    * html .pagination li {
    display: inline;
    }
    </style>

    </script>

    <script type="text/javascript" src="java.js" ></script>


    </head>
    <body>
    [PHP=""]echo $pg->paginate(); ?>
    [/PHP]
    [PHP=""] if($action->getMessages()) {
    echo '<ol>';
    foreach($action->getMessages() as $arrMessage) {
    printf(
    '<li>
    <p>%s</p>
    <p>%s</p>
    <p>%s</p>
    </li>'
    ,$arrMessage['Title']
    ,$arrMessage['Description']
    ,$arrMessage['ActivateDate']
    );
    }
    echo '</ol>';
    } else {
    echo "<p>No messages available</p>";
    }[/PHP]

    [PHP=""]echo $pg->paginate(); [/PHP]

    </body>
    </html>

    -----Java.js----


    var loadedobjects=""
    var rootdomain="http://"+window.location.hostname

    function ajaxpage(url, containerid){
    var page_request = false
    if (window.XMLHttpRequest) // if Mozilla, Safari etc
    page_request = new XMLHttpRequest()
    else if (window.ActiveXObject){ // if IE
    try {
    page_request = new ActiveXObject("Msxml2.XMLHTTP")
    }
    catch (e){
    try{
    page_request = new ActiveXObject("Microsoft.XMLHTTP")
    }
    catch (e){}
    }
    }
    else
    return false
    page_request.onreadystatechange=function(){
    loadpage(page_request, containerid)
    }
    page_request.open('GET', url, true)
    page_request.send(null)
    }

    function loadpage(page_request, containerid){
    if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
    document.getElementById(containerid).innerHTML=page_request.responseText
    }

    function loadobjs(){
    if (!document.getElementById)
    return
    for (i=0; i<arguments.length; i++){
    var file=arguments[i]
    var fileref=""
    if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
    if (file.indexOf(".js")!=-1){ //If object is a js file
    fileref=document.createElement('script')
    fileref.setAttribute("type","text/javascript");
    fileref.setAttribute("src", file);
    }
    else if (file.indexOf(".css")!=-1){ //If object is a css file
    fileref=document.createElement("link")
    fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
    fileref.setAttribute("href", file);
    }
    }
    if (fileref!=""){
    document.getElementsByTagName("head").item(0).appendChild(fileref)
    loadedobjects+=file+" " //Remember this object as being already added to page
    }
    }
    }
    Last edited by wargamon; December 17th, 2012 at 08:55 PM. Reason: Code Tagging
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    25
    Rep Power
    0

    Angry


    Originally Posted by wargamon
    foreach(range($page_start,$page_end,1) as $i) {
    $return.= sprintf(
    '<li%s>%s%s%s</li>'
    ,$page == $i?' class="current"':''
    ,$page == $i?'':sprintf('<a href="%s" onclick="pagediv()">',str_replace($page_flag,$i,$base_path))

    ,'&nbsp-&nbsp'
    ,$i
    ,$page == $i?'':'</a>'
    );
    }

    The paginate in arr0.php is current shown on the div "main" so if I call an ajaxpage within the same div "main" on arr0.php, can this work?

  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    12
    I still don't see the definition for the ajaxpage function.

    Also please wrap your code in php tags so that it's easier to read.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    25
    Rep Power
    0
    Originally Posted by msteudel
    I still don't see the definition for the ajaxpage function.

    Also please wrap your code in php tags so that it's easier to read.
    Updated. The ajaxpage in html head tags

    <script type="text/javascript"> function pagediv() { ajaxpage ('arr0.php', 'main'); } </script>


    I tried a simple javascript function to scroll to top it works, but not this ajaxpage.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    25
    Rep Power
    0
    Thanks to those who responded and advices. I got this solved.

IMN logo majestic logo threadwatch logo seochat tools logo