如何在活跃的分页上进行分页

时间:2014-12-02 17:08:18

标签: php mysql css pagination

我正在尝试在列表中激活课程

如果有人可以帮我在显示的分页页面上添加class="active"

$perpage= $conf['perpage'];
if (isset($_GET["page"])) { 
    $page  = $_GET["page"]; 
} else { 
    $page=1; 
}; 
$start_from = ($page-1) * $perpage; 
$result = "SELECT * FROM topics LIMIT $start_from, $perpage"; 
$result = mysql_query ($result); 
$n = 0;
while ($row = mysql_fetch_array ($result)){
    echo '<tr>';
    echo '<td>'.$row['topic_no'].'</td>';
    echo '</tr>';
    ++$n;
}


$sql = "SELECT * FROM topics"; 
$result = mysql_query($sql); 
$total_records = mysql_num_rows($result);  
$total_pages = ceil($total_records / $perpage); 
echo '<ul class="pagination">'; 
echo "<li><a href='topics.php?page=1'>".'<'."</a></li> ";  

for ($i=1; $i<=$total_pages; $i++) { 
    echo "<li  ><a href='topics.php?page=".$i."'>".$i."</a></li> "; 
}; 
echo "<li><a href='topics.php?page=$total_pages'>".'>'."</a> </li>"; 

echo '</ul> ';

先谢谢

4 个答案:

答案 0 :(得分:3)

您也可以尝试此代码。它的工作

    $c="active"; 
    for ($i=1; $i <$total_page ; $i++) {

    if($page==$i)
    {
        $c="active";
    }
    else
    {
        $c="";
    }
    echo "<li class=\"$c\"><a href=\"archive.php?page=$i\">$i</a></li>";
}

答案 1 :(得分:2)

$active = $i == $page ? 'class="active"' : '';
echo "<li  ><a {$active} href=\"topics.php?page={$i}\">{$i}</a></li> "; 

我们只需要在页面为当前时添加class =“active”,否则我们不添加任何内容。如果你已经有行的类 - 你只需要使用像

这样的smth
$activeClass = $i == $page ? 'active' : '';
echo "<li  ><a class=\"my-row-class1 {$activeClass}\" href=\"topics.php?page={$i}\">{$i}</a></li> ";

答案 2 :(得分:1)

  echo "<li if($_GET['page']==$i){ class='active'}><a href='topics.php?page=".$i."'>".$i."</a></li> ";

请试试这个。它可能对你有帮助

答案 3 :(得分:0)

$search = @$_GET['page'];  // get value form other page

$page ='A';

for ($Page=1; $Page <27 ; $Page++) {  ?>  // abcdef... create

<li class="<?php if($search==$page){ echo 'active'; } ?>"> <?php echo  '<a href="alphabetic-result.php?page='.$page.' ">'. $page++ . '</a>' ;?> </li>  

<?php } ?>

/* if($search==$page){ echo 'active'; } this code means if search value == alphabet then class active call automatically */