0

我在 php 中编写了一个简单的分页代码,但我想在 index.tpl 中而不是在 index.php 中显示 for 循环我该怎么办?

一些帮助的链接:http ://www.smarty.net/syntax_comparison

$perPage = 10;
$PaginationSql = mysql_query("SELECT COUNT('ID') FROM VA_VIDEOS");
$Pages = ceil(mysql_result($PaginationSql, 0) / $perPage);
$Page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$Start = ($Page - 1) * $perPage;
$postDate = date("d-m-Y");
srand(time());
$randomNum = (rand()%20);
$sql = mysql_query("SELECT * FROM VA_VIDEOS ORDER BY ID DESC LIMIT ".$Start.", ".$perPage."");
while ($Videos = mysql_fetch_array($sql))
{

$ShowVideos[] = $Videos;

}
if($Page >= 1 && $Page<=$Pages){
echo "<div style='margin-left:20px;'>";
if($Page != 1){print "<b class='pagination'><a href='?page=1'>First</a></b>";}

    for($x=1; $x<=$Pages; $x++){
    $PaginationNum = ($x == $Page) ? "<b class='page-in'>".$x."</b>" : "<a href='?page=".$x."' class='pagination'>".$x."</a>";
    echo $PaginationNum;

    }

    if($Page != $Pages){
$LastPage = "<b class='pagination'><a href='?page=".$Pages."'>Last</a></b>";
echo $LastPage;
}
echo "</div>";
} else {

    print "Error!";

}
4

2 回答 2

2

只需将脚本的最后一部分在其中回显内容放入index.tpl.

尝试这样的事情(使用 Smarty3 foreach 语法):

<?php
// index.php

$perPage = 10;
// ...
while ($Videos = mysql_fetch_array($sql)) {
    $ShowVideos[] = $Videos;
}

$smarty->assign('current_page', $Page);
$smarty->assign('pages', range($pages)); // returns: array(1, 2, 3, ..., $Pages)

索引.tpl

{if $current_page < $pages|count}
    <b class="pagination"><a href="?page=1">First</a></b>
{/if}

{foreach $pages as $page}
    {if $page == $current_page}
         <b class="page-in">{$page}</b>
    {else}
        <a href="?page={$page}" class="pagination">{$page}</a>

    {/if}
{/foreach}

{if $page < $pages|count}
    <b class="pagination"><a href="?page={$pages|count}">Last</a></b>
{/if}

为了获得额外的性能,计算$pagesin的大小index.php并分配它。我只是想展示如何使用 PHP 函数作为变量修饰符以备不时之需。

于 2012-04-06T23:28:51.877 回答
0

像这样使用循环

while ($Videos = mysql_fetch_array($sql))
{

$ShowVideos[] = $Videos;

}
$smarty->assign("video", $ShowVideos);

TPL 文件如下所示:

{section name=video loop=$video}

<li>{$video[video].id}</li> //// will show the video id from database


{/section}
于 2013-10-01T18:42:17.567 回答