简体   繁体   中英

how to make php auto page number count?

I'm currently using this code which will force me to keep adding page number line code.

<div align="center"><font size="6">Page: </font> <?php
if (strpos($so, "1")) echo $_SESSION['config']['page']==1 ? '<a href="?page=1"><b><font color="red" size="6">[1]</font></b></a> ' : '<a href="?page=1"><font size="5">[1]</font></a> ';

if (strpos($so, "2")) echo $_SESSION['config']['page']==2 ? '<a href="?page=2"><b><font color="red" size="6">[2]</font></b></a> ' : '<a href="?page=2"><font size="5">[2]</font></a> ';
?> </div>

my question is if its possible to make a simple code to just count and add pages ?

thanks in advance

<div align="center"><font size="6">Page: </font>
<?php
for($i=1;$i<10;$i++) {
    if (strpos($so, $i)) echo $_SESSION['config']['page']==$i ? '<a href="?page='.$i.'"><b><font color="red" size="6">['.$i.']</font></b></a> ' : '<a href="?page='.$i.'"><font size="5">['.$i.']</font></a> ';
}
?>
</div>

Do you mean something like this?

Yes it is possible. What you need to know is how many pages you need to list, and which is selected. You could store these in a variable in a number of different ways (i've kept it simple below). Then you can just loop to display the page options:

$pages = 6;        // number of pages to show
$current_page = 3; // to highlight selected page

Now for actually outputting the values into HTML.

<ol id="pagination">
    <?php for ($i = 1; $i <= $pages; $i++): ?>
    <li <?php if ($i === $current_page): ?>class="selected"<?php endif; ?>>
        <a href="?page=<?php echo $i; ?>">[<?php echo $i; ?>]</a>
    </li>
    <?php endfor; ?>
</ol>

I've separated the style from the HTML (you can add your font-size and whatever else here too):

#pagination .selected {
    color: red;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM