Jump to content

Quick question about a simple loop


TechnoDiver

Recommended Posts

Hi, Freaks. I've got a quick question I'd like to ask out of curiosity

I coded a simple pagination functionality last night. It's nothing intricate. The page it's for pulls a large amount of data and is displayed 10 rows at a time.

To determine which data to display it uses a simple $_Get['from'] and $_GET['to']. They start out as $from = 0; and $to = 10; and are each incremented or decremented by 10 if the user clicks 'prev' or 'next'.

I got it functioning properly and then needed a quick if statement to disable the 'prev' if $from == 0 && $to == 9; (so it didn't go to -10 and -1 respectively and break the script).

This was the original try ->

<?php

if($from == 0 && $to == 9) {
    $state = "disabled";
} else {
    $state = "active";
}
$html = sprintf("<a href='/qcic/newsnet/pages/%s.php?act_id=%s&act_title=%s&from=%s&to=%s' class='%s'><i class='fa fa-angle-left'></i> previous</a>", $actor, $id, $actor, ($from-10), ($to-10), $state);
echo $html; 

And it did not work. $from became -10, $to became -1 and it broke the script. I changed it to this ->

<?php 
if($from == 0 && $to == 9) {
    $state = "disabled";
    $html = sprintf("<a href='#' class='%s'><i class='fa fa-angle-left'></i> previous</a>", $actor, $id, $actor, ($from-10), ($to-10), $state);
} else {
    $state = "active";
    $html = sprintf("<a href='/qcic/newsnet/pages/%s.php?act_id=%s&act_title=%s&from=%s&to=%s' class='%s'><i class='fa fa-angle-left'></i> previous</a>", $actor, $id, $actor, ($from-10), ($to-10), $state);
}
echo $html; 

This way worked

So I have 2 simple questions regarding this:

1. Why didn't the first method work and  I had to make the $html variable twice and actually remove the URL?

and

2. How would I provide the same functionality to the 'next' button with an undetermined amount of data? - i don't know how many entries there may be, but when the script gets there I want it to know it got there and act accordingly. Can I get open thoughts on ways to achieve this?

TIA and the best of Sundays to all you freaky geeks

Link to comment
Share on other sites

Do you have error checking enabled to see any possible messages? I"m guessing that you last two string parms are not valid since they are not simply variable names but real strings.  Quote them.

And why the use of sprintf?  A simple statement is all you need without the extra headache of organizing your arguments with your string contents.  Don't know what it buys you.

$from = $from - 10;
$to = $to - 10;
$html = "<a href='/qcic/newsnet/pages/$actor.php?act_id=$id&act_title=$actor&from=$from&to=$to' class='$state'>
  <i class='fa fa-angle-left'></i>
  previous</a>";

BTW - your outdated <i> tag is incorrectly formed.

Edited by ginerjm
Link to comment
Share on other sites

As for your Next button.  You will need to either know the amount of data prior to starting this or you will have to check how many results you obtained responding to the last button you pressed.  If it is not ten then you have to modify your from/to values to accomodate that.  I assume that you are doing a query with a limit/offset clause so that should not be hard to implement.

Link to comment
Share on other sites

2 hours ago, ginerjm said:

Do you have error checking enabled to see any possible messages?

The only errors I got are the warnings and the typeError because there are no -10 or -1 elements

 

2 hours ago, ginerjm said:

And why the use of sprintf?

I had it exactly how you wrote it at first and changed to sprintf so I could do the math ops on $from and $to inline

2 hours ago, ginerjm said:

BTW - your outdated <i> tag is incorrectly formed.

How? I see mine and the one you posted as the same. That's a statement that begs a follow up

1 hour ago, ginerjm said:

You will need to either know the amount of data prior to starting this or you will have to check how many results you obtained responding to the last button you pressed.

I was afraid of that, thanks.

Link to comment
Share on other sites

2 hours ago, ginerjm said:

I didn't correct the italic tags.  You can.

It's an icon tag directly from font-awesome

2 hours ago, ginerjm said:

Look where it got you!

I mean, in the end it got me properly functioning code with the math ops done inline like I was looking for. Thanks for your replies. I appreciate all guidance

Link to comment
Share on other sites

5 hours ago, TechnoDiver said:

To determine which data to display it uses a simple $_Get['from'] and $_GET['to'].

Rather than passing two variables in the URL, just pass one called 'page'.  You can use that to calculate your $to/$from variables.

$recordsPerPage = 10;
$page = max($_GET['page'], 1);
$from = ($page - 1) * $recordsPerPage;
$to = $from + $recordsPerPage;

For the previous button, disable it if $page == 1.   To handle your next button, you need to do a query to count the total possible results.  Take the same query you'd use to obtain your paginated data and remove the LIMIT clause and replace the selected columns with CEIL(COUNT(*)/10) as totalPages.  Once you have the total page count you can compare it to $page to determine whether or not to enable the next button.

  • Great Answer 1
Link to comment
Share on other sites

1 hour ago, kicken said:

Rather than passing two variables in the URL, just pass one called 'page'.  You can use that to calculate your $to/$from variables.

That's a really excellent idea. I feel I would have eventually evolved into using just 1 variable, I tend to be really sloppy on the first run through.. thank you

Link to comment
Share on other sites

4 hours ago, TechnoDiver said:

why the use of the max()

If someone decides to be funny and set $_GET['page'] to "-42" you don't end up with messed up calculations.  Using max, any number <= 0 will get ignored and $page will get set to 1 instead.  If you calculate your total page count you should do a min($totalPageCount, $page) as well so that $page doesn't get set to something larger than $totalPageCount.

 

  • Thanks 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.