TechnoDiver Posted October 10, 2021 Share Posted October 10, 2021 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 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 10, 2021 Share Posted October 10, 2021 (edited) 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 October 10, 2021 by ginerjm Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 10, 2021 Share Posted October 10, 2021 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. Quote Link to comment Share on other sites More sharing options...
TechnoDiver Posted October 10, 2021 Author Share Posted October 10, 2021 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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 10, 2021 Share Posted October 10, 2021 I didn't correct the italic tags. You can. As I said - you needed quotes to avoid that error message. And the solution to the minus 10 is to do what I did. Skip the sprintf. Too much work. Look where it got you! Quote Link to comment Share on other sites More sharing options...
Barand Posted October 10, 2021 Share Posted October 10, 2021 2 hours ago, ginerjm said: BTW - your outdated <i> tag is incorrectly formed. https://fontawesome.com/v4.7/icon/angle-left Quote Link to comment Share on other sites More sharing options...
TechnoDiver Posted October 10, 2021 Author Share Posted October 10, 2021 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 Quote Link to comment Share on other sites More sharing options...
kicken Posted October 10, 2021 Share Posted October 10, 2021 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. 1 Quote Link to comment Share on other sites More sharing options...
TechnoDiver Posted October 10, 2021 Author Share Posted October 10, 2021 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 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 10, 2021 Share Posted October 10, 2021 So what is the point of having the </i> tag now? Quote Link to comment Share on other sites More sharing options...
TechnoDiver Posted October 11, 2021 Author Share Posted October 11, 2021 19 hours ago, kicken said: $recordsPerPage = 10; $page = max($_GET['page'], 1); $from = ($page - 1) * $recordsPerPage; $to = $from + $recordsPerPage; Hay, Kicken, quick question - why the use of the max() function in the $page assign? Why not just $_GET['page']?? Quote Link to comment Share on other sites More sharing options...
kicken Posted October 11, 2021 Share Posted October 11, 2021 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. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.