如何将sprintf传输到print_r或echo

时间:2011-05-28 22:43:17

标签: php printing echo printf

这是一些分页功能代码。如何将sprintf转移到print_recho?我希望将class添加到a href并添加更多onclick内容。感谢。

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 href="%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;
    }

1 个答案:

答案 0 :(得分:2)

通过从类中调用函数并打印其内容

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

echo $obj->display();

print_r($obj->display());

是你想要完成的事情吗?

也是小写的:

有些时候使用像$return.= sprintf('</ul>');这样的sprintf()是没有意义的 只需将其分配给变量,如下所示:

$return.= '</ul>';

更新:

要回答您的评论,您只需输入两次或两次相同的变量:

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

或者对于这个特殊的例子你可以使用javascript修复:

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

UPDATE2:

您的代码不容易调试,我建议您重写它,以及在您想要调试时更有意义。

$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);