CUTCODEDOWN
Minimalist Semantic Markup

Welcome Guest
Please Login or Register

If you have registered but not recieved your activation e-mail in a reasonable amount of time, or have issues with using the registration form, please use our Contact Form for assistance. Include both your username and the e-mail you tried to register with.

Author Topic: Migrating pagination code from PHP 7 to PHP 8  (Read 8858 times)

fgm

  • Jr. Member
  • **
  • Posts: 60
  • Karma: +5/-0
Migrating pagination code from PHP 7 to PHP 8
« on: 25 Feb 2022, 02:47:41 pm »
I'm using this snippet to paginate a static blog generator but, although it works fine in PHP 7.4, it doesn't work in PHP 8.1. This is the most complex part of the code I use and I'm lost, so any help will be appreciated.

I did some modifications to this pagination code to use a subfolder for each page, but it happens in the original code too.
Code: [Select]
<?php

class Paginate {

private
$perPage,
$maxShow,
$middle,
$href,
$current,
$lastPage;

public function __construct(
$href '/',
$current 0,
$total 0,
$perPage 10,
$maxShow 10 //,
//$field = 'page'
) {

//$this->href = ' href="' . $href . (
// strpos($href, '?') === FALSE ? '?' : '&amp;'
//) . $field . '=';
$this->href ' href="/blog/';
if (request::value()==='etiqueta') { 
$perPage 5000;
$maxShow 5000;
}
$this->current $current;
$this->total $total;
$this->perPage $perPage;
$this->maxShow $maxShow;
$this->middle floor($maxShow 2);
$this->lastPage floor(($total 1) / $perPage);

// Paginate::__construct

public function getLimits() {
return [
':offset' => $this->current $this->perPage,
':limit' => $this->perPage
];
}

private function anchorLine($page$text$title false$rel false) {
$tag $page 'span' 'a';
if ($page===&& request::value(1)!==''
echo '
<li><'
$tag, (
$page >= $this->href '"' ''
), (
$page == -' class="disabled"' ''
), (
$title ' title="' $title '"' ''
), (
$rel ' rel="' $rel '"' ''
), '>'$text'</'$tag'></li>';
else if ($page===0)
echo '
        <li><'
$tag, (
          
$page >= $this->href '/"' ''
        
), (
          
$page == -' class="disabled"' ''
        
), (
          
$title ' title="' $title '"' ''
        
), (
          
$rel ' rel="' $rel '"' ''
        
), '>'$text'</'$tag'></li>';
  
else echo '
<li><'
$tag, (
$page >= $this->href $page '/"' ''
), (
$page == -' class="disabled"' ''
), (
$title ' title="' $title '"' ''
), (
$rel ' rel="' $rel '"' ''
), '>'$text'</'$tag'></li>';
 
}

public function show() {

  if ($this->total <= $this->perPage) return;

echo '<hr>
<ul class="pagination">'
;

if (($this->lastPage 0) && ($this->current 0)) {
$this->anchorLine(0'Primera');
$this->anchorLine($this->current 1'&#38;#38;#38;#x25C0;''Anterior''prev');
} else {
$this->anchorLine(-2'Primera');
$this->anchorLine(-2'&#38;#38;#38;#x25C0;');
}

if ($this->lastPage >= $this->maxShow) {
$counter = ($this->current <= $this->middle) ? $this->current $this->middle;
$endPage $counter $this->maxShow;
if ($endPage $this->lastPage) {
$counter $this->lastPage $this->maxShow;
if ($counter 0$counter 0;
$endPage $this->lastPage;
}
} else {
$counter 0;
$endPage $this->lastPage;
}

while ($counter <= $endPage$this->anchorLine(
$counter == $this->current ? -$counter,
++$counter
);

if (($this->lastPage 0) && ($this->current $this->lastPage)) {
$this->anchorLine($this->current 1'&#38;#38;#38;#x25B6;''Siguiente''next');
$this->anchorLine($this->lastPage'Última');
} else {
$this->anchorLine(-2'&#38;#38;#38;#x25B6;');
$this->anchorLine(-2'Última');
}
echo '
</ul>'
;

// Paginate::show

// Paginate
« Last Edit: 25 Feb 2022, 03:54:38 pm by fgm »

Dave

  • Junior Member
  • *
  • Posts: 38
  • Karma: +12/-0
Re: Migrating pagination code from PHP 7 to PHP 8
« Reply #1 on: 26 Feb 2022, 03:44:00 pm »
Doesn't work isn't really helpful I'm afraid. What errors do you get using the code?
Dave

fgm

  • Jr. Member
  • **
  • Posts: 60
  • Karma: +5/-0
Re: Migrating pagination code from PHP 7 to PHP 8
« Reply #2 on: 26 Feb 2022, 03:50:44 pm »
Hello Dave, you are right, and that's part of my problem... I don't get any error, the output just stops when the code calls to the pagination functions.

I tried using error_reporting(E_ALL) without success. I suppose the code works fine but not the way I want.
« Last Edit: 26 Feb 2022, 03:56:40 pm by fgm »

John_Betong

  • Full Member
  • ***
  • Posts: 218
  • Karma: +24/-1
    • The Fastest Joke Site On The Web
Re: Migrating pagination code from PHP 7 to PHP 8
« Reply #3 on: 26 Feb 2022, 08:29:58 pm »
@fgm,
> Hello Dave, you are right, and that's part of my problem... I don't get any error, the output just stops when the code calls to the pagination functions.


A couple of points to note:

1. error_reporting(E_ALL); // can be abbreviated to error_reporting(-1), see PHP Manual

2. Displaying errors depends on the php.ini -> display_errors  setting,  usually set to On locally and Off when online

3. Overriding the php.ini setting can be achieved by setting ini_set(‘display_errors’, ‘True’);

4. Errors still might not show if php.ini -> php_startup_errors = Off (default). This setting cannot be called using ini_set(‘display_startup_errors’, ‘True’); because the setting is ignored after the PHP page starts :)

5. When using PHP 8.?, at the start of the PHP file use declare(strict_types=1); which usually catches all errors and warnings. Unlike most php.ini settings the declaration is only valid for that file and does not affect any other include or require PHP files
« Last Edit: 27 Feb 2022, 03:03:56 am by John_Betong »
Retired in the City of Angels where the weather suits my clothes

fgm

  • Jr. Member
  • **
  • Posts: 60
  • Karma: +5/-0
Re: Migrating pagination code from PHP 7 to PHP 8
« Reply #4 on: 27 Feb 2022, 02:53:17 am »
That you a lot, John, that definitely helped. Now I get interesting information:
Code: [Select]
Fatal error: Uncaught TypeError: Unsupported operand types: string * int in /htd
ocs/example.com/libs/paginate.lib.php:41 Stack trace: #0 /htdocs/example.com/sta
tics/blog.php(46): Paginate->getLimits() #1 /htdocs/example.com/index.php(57): r
equire_once('/htdocs/example...') #2 {main} thrown in /htdocs/example.com/libs/p
aginate.lib.php on line 41

I'm going to look again in the PHP 7.4 to PHP 8.0 migration manual. I'd say this is the problematic line, but I don't know how to correct it:
Code: [Select]
':offset' => $this->current * $this->perPage,

-- update. Solved! thank you all a lot.

I changed this:
Code: [Select]
':offset' => (int)$this->current * (int)$this->perPage,

And this:
Code: [Select]
          $this->anchorLine($this->current - 1, '&#38;#x25C0;', 'Anterior', 'prev');

to this:
Code: [Select]
          $this->anchorLine((int)$this->current - 1, '&#38;#x25C0;', 'Anterior', 'prev');

I notice the forum inserts #38; in the code when modifying the post.
« Last Edit: 27 Feb 2022, 03:14:50 am by fgm »

John_Betong

  • Full Member
  • ***
  • Posts: 218
  • Karma: +24/-1
    • The Fastest Joke Site On The Web
Re: Migrating pagination code from PHP 7 to PHP 8
« Reply #5 on: 27 Feb 2022, 03:13:46 am »
The error states that strings cannot be multiplied by a numeric:

Try typecasting the variables:
Code: [Select]
offset' => (int) $this->current * (int) $this->perPage,

Also to find the variable type and value:


Code: [Select]
echo ‘<pre>’; // add line feeds
  echo var_dump($this->current):
  echo var_dump($this->perPage);
echo ‘</pre>’;

« Last Edit: 27 Feb 2022, 03:18:59 am by John_Betong »
Retired in the City of Angels where the weather suits my clothes

fgm

  • Jr. Member
  • **
  • Posts: 60
  • Karma: +5/-0
Re: Migrating pagination code from PHP 7 to PHP 8
« Reply #6 on: 27 Feb 2022, 03:15:27 am »
It's fixed now, it just needed to cast some strings. Thank you!

John_Betong

  • Full Member
  • ***
  • Posts: 218
  • Karma: +24/-1
    • The Fastest Joke Site On The Web
Re: Migrating pagination code from PHP 7 to PHP 8
« Reply #7 on: 27 Feb 2022, 03:30:46 am »
Looks like I was a little late with my suggestions:)


If you have access to php.ini try searching for “<span…”and “</span”  and replace with “<pre…” and “</pre>”.


I have added CSS background colour yellow, linefeed and pre-overflow-auto, etc which makes the displayed errors far more readable. Only complication I have found is that php.ini is overwritten when updating :(
Retired in the City of Angels where the weather suits my clothes

fgm

  • Jr. Member
  • **
  • Posts: 60
  • Karma: +5/-0
Re: Migrating pagination code from PHP 7 to PHP 8
« Reply #8 on: 27 Feb 2022, 03:56:30 am »
Thank you for the suggestions.

I have both PHP 7.4 and 8.1 installed, each of them with its own php.ini file: /etc/php-7.4.ini and /etc/php-8.1.ini
« Last Edit: 27 Feb 2022, 07:50:00 am by fgm »

 

SMF spam blocked by CleanTalk

Advertisement