Resize it

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
egturnkey
Forum Commoner
Posts: 34
Joined: Sun Jul 26, 2009 7:35 pm

Resize it

Post by egturnkey »

Hello dear friends,

I've many titles and the page that show it , show only 15 per page
and as the titles are more than 15 so it shows me 15 and select page below

the bad news is that the pages number are getting out of the page cause it is too many

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20---> ect
(goes out the page)

so how can i resize it to be as following

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
(pages number 10 ber line)

here is the code that responsible to show the pages number

Code: Select all

if($page > 1){
   $prev = ($page - 1);
   echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">Previous</a>";
} 

for($i = 1; $i <= $total_pages; $i++){
   if(($page) == $i){
      echo "$i";
   } else {
      echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a>";
   }
}
if($page < $total_pages){
   $next = ($page + 1);
   echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next</a>";
}
where $total_pages is the total pages will be shown

thanks so much
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Resize it

Post by John Cartwright »

I've indented your code for you, this time. Failure to do so in the future will result in most people, including myself, to completely ignore your post.

To keep things simple, what I would do is store all your "pages" inside an array, then you can easily chunk this array into sections of 10, or whatever number of columns.

Code: Select all

$pages = array();
for($i = 1; $i <= $total_pages; $i++){
   if(($page) == $i){
      $pages[] = $i;
   } else {
      $pages[] = "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a>";
   }
}

foreach (array_chunk($pages, 10) as $chunk) {
   echo implode(' ', $chunk) .'<br />';
}
which should give you the right idea (may involve some tweaking).
egturnkey
Forum Commoner
Posts: 34
Joined: Sun Jul 26, 2009 7:35 pm

Re: Resize it

Post by egturnkey »

thank you for helping me.
i've learned something now not only for the code but a new way of thinking which depends on thinking of simple ways as you can.

:banghead: i was thinking in very complex solutions such as

if the current page is greater than 0, and the current page divided by 10 leaves no remainder (remainder == 0), insert a line break
or
for each page number that can be divided by 10 give break line
ect

never thinking of array
thanks alot i promise i will do me best :drunk:
Post Reply