ECshop前台分页

代码如下:

---------------------------------------在要分页的sql上面加上如下代码 ----------------------------------------------------

$page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
$record_count = $db->getOne('总数的sql语句');
//例如 $record_count = $db->getOne('select count(*) from '.$ecs->table('goods'));
$pager = get_pager('php页面', php页面的参数 , $record_count, $page,每页显示几个,分页样式);
//例如 $pager = get_pager('test.php', array('act' => $_REQUEST['act'],'id' => $_REQUEST['id']), $record_count, $page,5,0);
$smarty->assign('pager', $pager);

//分页的sql语句

$sql= 分页sql语句.' LIMIT '.$pager['start'].','.$pager['size'];

//例如 $sql= 'select count(*) from '.$ecs->table('goods').' LIMIT '.$pager['start'].','.$pager['size'];

------------------------------------------------------------------get_pager 方法----------------------------------------------

function get_pager($url, $param, $record_count, $page = 1, $size = 10, $styleid=1)
{
    $size = intval($size);
    if ($size < 1)
    {
        $size = 10;
    }

    $page = intval($page);
    if ($page < 1)
    {
        $page = 1;
    }

    $record_count = intval($record_count);

    $page_count = $record_count > 0 ? intval(ceil($record_count / $size)) : 1;
    if ($page > $page_count)
    {
        $page = $page_count;
    }
    /* 分页样式 */
    $pager['styleid'] = $styleid;

    $page_prev = ($page > 1) ? $page - 1 : 1;
    $page_next = ($page < $page_count) ? $page + 1 : $page_count;

    /* 将参数合成url字串 */
    $param_url = '?';
    foreach ($param AS $key => $value)
    {
        $param_url .= $key . '=' . $value . '&';
    }

    $pager['url']          = $url;
    $pager['start']        = ($page -1) * $size;
    $pager['page']         = $page;
    $pager['size']         = $size;
    $pager['record_count'] = $record_count;
    $pager['page_count']   = $page_count;

    if ($pager['styleid'] == 0)
    {
        $pager['page_first']   = $url . $param_url . 'page=1';
        $pager['page_prev']    = $url . $param_url . 'page=' . $page_prev;
        $pager['page_next']    = $url . $param_url . 'page=' . $page_next;
        $pager['page_last']    = $url . $param_url . 'page=' . $page_count;
        $pager['array'] = array();
        for ($i = 1; $i <= $page_count; $i++)
        {
            $pager['array'][$i] = $i;
        }
    }
    else
    {
        $_pagenum = 10;     // 显示的页码
        $_offset = 2;       // 当前页偏移值
        $_from = $_to = 0; // 开始页, 结束页
        if($_pagenum > $page_count)
        {
            $_from = 1;
            $_to = $page_count;
        }
        else
        {
            $_from = $page - $_offset;
            $_to = $_from + $_pagenum - 1;
            if($_from < 1)
            {
                $_to = $page + 1 - $_from;
                $_from = 1;
                if($_to - $_from < $_pagenum)
                {
                    $_to = $_pagenum;
                }
            }
            elseif($_to > $page_count)
            {
                $_from = $page_count - $_pagenum + 1;
                $_to = $page_count;
            }
        }
        $url_format = $url . $param_url . 'page=';
        $pager['page_first'] = ($page - $_offset > 1 && $_pagenum < $page_count) ? $url_format . 1 : '';
        $pager['page_prev'] = ($page > 1) ? $url_format . $page_prev : '';
        $pager['page_next'] = ($page < $page_count) ? $url_format . $page_next : '';
        $pager['page_last'] = ($_to < $page_count) ? $url_format . $page_count : '';
        $pager['page_kbd'] = ($_pagenum < $page_count) ? true : false;
        $pager['page_number'] = array();
        for ($i=$_from;$i<=$_to;++$i)
        {
            $pager['page_number'][$i] = $url_format . $i;
        }
    }
    $pager['search'] = $param;

    return $pager;
}

------------------------------------------------------ html 分页代码 ---------------------------------------------------------

<!--翻页 start-->
<form name="selectPageForm" action="{$smarty.server.PHP_SELF}" method="get">
<!-- {if $pager.styleid eq 0 } -->
<div id="pager">
总计{$pager.record_count}个记录,共{$pager.page_count} 页。 <span> <a href="{$pager.page_first}">{$lang.page_first}</a> <a href="{$pager.page_prev}">{$lang.page_prev}</a> <a href="{$pager.page_next}">{$lang.page_next}</a> <a href="{$pager.page_last}">{$lang.page_last}</a> </span>
    <!--{foreach from=$pager.search key=key item=item}-->
      {if $key eq 'keywords'}
          <input type="hidden" name="{$key}" value="{$item|escape:decode_url}" />
        {else}
          <input type="hidden" name="{$key}" value="{$item}" />
      {/if}
    <!--{/foreach}-->
    <select name="page" id="page" onchange="selectPage(this)">
    {html_options options=$pager.array selected=$pager.page}
    </select>
</div>
<!--{else}-->
<div id="pager" class="pagebar">
<span class="f_l f6" style="margin-right:10px;">总计<b>{$pager.record_count}</b> 个记录</span>
<!-- {if $pager.page_first} --><a href="{$pager.page_first}">{$lang.page_first} ...</a><!-- {/if} -->
<!-- {if $pager.page_prev} --><a class="prev" href="{$pager.page_prev}">{$lang.page_prev}</a><!-- {/if} -->
<!-- {if $pager.page_count neq 1} -->
    <!--{foreach from=$pager.page_number key=key item=item}-->
      <!-- {if $pager.page eq $key} -->
      <span class="page_now">{$key}</span>
      <!-- {else} -->
      <a href="{$item}">[{$key}]</a>
      <!-- {/if} -->
    <!--{/foreach}-->
<!-- {/if} -->
<!-- {if $pager.page_next} --><a class="next" href="{$pager.page_next}">{$lang.page_next}</a><!-- {/if} -->
<!-- {if $pager.page_last} --><a class="last" href="{$pager.page_last}">...{$lang.page_last}</a><!-- {/if} -->
<!-- {if $pager.page_kbd} -->
    <!--{foreach from=$pager.search key=key item=item}-->
      {if $key eq 'keywords'}
          <input type="hidden" name="{$key}" value="{$item|escape:decode_url}" />
        {else}
          <input type="hidden" name="{$key}" value="{$item}" />
      {/if}
    <!--{/foreach}-->
    <kbd style="float:left; margin-left:8px; position:relative; bottom:3px;"><input type="text" name="page" onkeydown="if(event.keyCode==13)selectPage(this)" size="3" class="B_blue" /></kbd>
    <!-- {/if} -->
</div>
<!-- {/if} -->
</form>
<!--翻页 END-->

 

评论关闭