2012-04-06 5 views
0

PHPで単純なページ区切りコードを書きましたが、index.phpにないindex.tplのループを表示したい 何ができますか?いくつかの助けのためのわかりやすいfor-looping

リンク: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!"; 

} 
+0

http://www.smarty.net/docs/en/language.function.foreach.tpl – c69

+0

iPhoneのセクションでPHPの質問は何ですか?間違ってタグ付けされていますか? –

答えて

2

ちょうどあなたがindex.tplにものをエコースクリプトの最後の部分を投げます。余分なパフォーマンスを得るために

<?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) 

index.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} 

index.php$pagesのサイズを計算し、それを割り当てる:

このような何かを試してみては(Smarty3ますforeach構文を使用しています)。私はちょうどあなたがそれを必要とする場合のためにvariable modifierとしてPHP関数を使用する方法を示すことを試みていた。

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} 
関連する問題