开发者

how to transfer sprintf to print_r or echo

开发者 https://www.devze.com 2023-03-08 20:09 出处:网络
Here are some pagination function code. how to transfer sprintfto print_r or echo? For I want add class into a href and add more onclick thing. Thanks.

Here are some pagination function code. how to transfer sprintf to print_r or echo? For I want add class into a href and add more onclick thing. Thanks.

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(
               '<div class="summary"><p class="pages">%u %s</p><p class="total">%u %s</p></div>'
            ,$total_pages
            ,$total_pages == 1?'Page':'Pages'
            ,$found_rows
            ,$found_rows == 1?$label:$label
        );
        $return.= sprintf('<ul class="pagination">');
        $return.= sprintf(
            '<li class="first">%s%s%s</li>'
            ,$page == 1?'':sprintf('<a hr开发者_开发百科ef="%s">',str_replace($page_flag,1,$base_path))
            ,'First'
            ,$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">',str_replace($page_flag,$i,$base_path))
                ,$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))
            ,'Next'
            ,$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;
    }


By calling the function from the class and printing its content

$obj = new ClassName(); // your class name

echo $obj->display();

print_r($obj->display());

is that what you are trying to accomplish?

also in smaller note:

Some times there is no point in using sprintf() like $return.= sprintf('</ul>'); simply assign it to the variable like so:

$return.= '</ul>';

UPDATE:

To answer your comment, you can simply type the same variable twice or n times:

$return = sprintf('<a href="%s" onclick=\"javascript:func("%s")\" class="link">'
          $url,$url);

or for this particular caase u can use a javascript fix:

$return = sprintf('<a href="%s" onclick=\"func(this.href)\" class="link">',$url);

UPDATE2:

Your code is not easy to debug, here is how i suggest you rewrite it, and it will make much more sense when you want to debug.

$new_path = $page == $total_pages? '': str_replace($page_flag,$total_pages,$base_path));
$end = $page == $total_pages?'':'</a>';
$return.= sprintf('<li class="last">%s%s%s</li>',$new_path,'Last',$end);
0

精彩评论

暂无评论...
验证码 换一张
取 消