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: Strange Pagination Result Problem  (Read 678 times)

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 787
  • Karma: +8/-0
    • Grumpy Young Man
Strange Pagination Result Problem
« on: 28 Aug 2020, 04:36:17 am »
Code: [Select]
// Pagination:
$resultsPerPage = 14;

  // Pagination
$page = ( ! empty($_GET['Page']) ? $_GET['Page'] : ( ! empty($_POST['Page']) ? $_POST['Page'] : 1 ) );

// Count ALL the MOT records:
$resultsMOT = $DB->query("
    SELECT
      COUNT(mot_id)
    FROM
      mots   
      ");

$numOfResults = $resultsMOT->fetch();

$numberOfPages = ceil( ( $numOfResults['COUNT(mot_id)'] / $resultsPerPage ) );

// PAGE Range test:
if( filter_var( $page, FILTER_VALIDATE_INT, [ "options" => [ 'min_range' => 1, 'max_range' => $numberOfPages ] ] ) === false ) { $page = 1; }
 
$start = ( ( $page - 1 ) * $resultsPerPage );

echo 'Start: ', $start . ' Number of pages required (unrounded): ', ( $numOfResults['COUNT(mot_id)'] / $resultsPerPage ), ' Rounded:', $numberOfPages;

  pagination_Build( ['current' => $page, 'max' => $numberOfPages, 'link' => "?module=MOT&action=Search"] );

$fetchMots = $DB->query("
SELECT
m.*, v.*, c.client_name
FROM
mots as m
INNER JOIN
vehicles as v
ON
( v.vehicle_mot = m.mot_id )
INNER JOIN
clients as c
ON
( c.client_id = m.mot_client )
ORDER BY
m.mot_expiry
LIMIT
$start, $resultsPerPage
");

For some reason, some results are repeated across two pages - the strange thing is it doesn't seem to effect EVERY page span...
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 1049
  • Karma: +188/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: Strange Pagination Result Problem
« Reply #1 on: 28 Aug 2020, 11:28:45 am »
Why the goofy array pass to pagination_build? What does pagination_build look like? Did you say page SPAN? Pagination is a list, don't you mean UL?

Nitpicks:
Generally I wouldn't allow something like subpages to be sent via $_POST, so that's kinda ... no. And if you REALLY insist on using both $_GET and $_POST at the same time, use $_REQUEST instead as it merges the two.

Is $DB a DEFINE? No it is not, so why the uppercase?  Remember, CamelFirst is for classes, camelCase is for functions, variables, and object instances (which are in fact variables), UPPERCASE is for define.

Or at least that's how it's supposed to go... which PHP can't even maintain internally despite claiming that's their own standard for it.

I would also likely combine the filter_var with the empty test.

Also I assume that bottom query is unrelated?

Bottom line though, if it's doubling up on your output, we need to see the output routine.

We are all, we are all, we are all FRIENDS! For today we're all brothers, tonight we're all friends. Our moment of peace in a war that never ends.

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 787
  • Karma: +8/-0
    • Grumpy Young Man
Re: Strange Pagination Result Problem
« Reply #2 on: 29 Aug 2020, 02:21:56 am »
Actually it’s more a result problem the last result on the page is sometimes repeated as the first result on the next page, but it doesn’t seem to happen on EVERY page... and if I change the result per page to 13 or 15 it seems to go?

I can’t post the extra code Jason as I’m on my phone not laptop but thanks for the feedback I’ll make a few changes and will post it back, are you saying the page links should come via a POST request?
« Last Edit: 29 Aug 2020, 02:24:43 am by GrumpyYoungMan »
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 787
  • Karma: +8/-0
    • Grumpy Young Man
Re: Strange Pagination Result Problem
« Reply #3 on: 29 Aug 2020, 03:54:26 am »
Sorry yes, my question was worded incorrectly - for some reason on SOME pages the last result on the page is repeated as the first result on the next page - BUT it doesn't affect EVERY page...

Code: [Select]
function mot_showMOTs($DB) {

require_once "skin/s0/page_header.php";

// Pagination:
$resultsPerPage = 14;

  // Pagination
$page = ( ! empty($_GET['Page']) ? $_GET['Page'] : ( ! empty($_POST['Page']) ? $_POST['Page'] : 1 ) );

// Count ALL the MOT records:
$resultsMOT = $DB->query("
    SELECT
      COUNT(mot_id)
    FROM
      mots   
      ");

$numOfResults = $resultsMOT->fetch();

$numberOfPages = ceil( ( $numOfResults['COUNT(mot_id)'] / $resultsPerPage ) );

// PAGE Range test:
if( filter_var( $page, FILTER_VALIDATE_INT, [ "options" => [ 'min_range' => 1, 'max_range' => $numberOfPages ] ] ) === false ) { $page = 1; }
 
$start = ( ( $page - 1 ) * $resultsPerPage );

echo 'Start: ', $start . ' Number of pages required (unrounded): ', ( $numOfResults['COUNT(mot_id)'] / $resultsPerPage ), ' Rounded:', $numberOfPages;

  pagination_Build( ['current' => $page, 'max' => $numberOfPages, 'link' => "?module=MOT&action=Search"] );

$fetchMots = $DB->query("
SELECT
m.*, v.*, c.client_name
FROM
mots as m
INNER JOIN
vehicles as v
ON
( v.vehicle_mot = m.mot_id )
INNER JOIN
clients as c
ON
( c.client_id = m.mot_client )
ORDER BY
m.mot_expiry
LIMIT
$start, $resultsPerPage
");

$first_date = new DateTime(date("Y-m-d"));

echo '<table class="searchTable">

   <thead>
    <tr>
      <th>VRM</th>
      <th>Expiry</th>
      <th>Client</th>
      <th>Status</th>
      <th>Actions</th>
    </tr>
  </thead>

  <tbody>';

while( $r = $fetchMots->fetch() ) {

// DATE Stuff:
$second_date = new DateTime($r['mot_expiry']);
$difference = $first_date->diff($second_date);

$status = date_IntervalFormat($difference);

echo '<tr>
      <td>
        <h2 class="vrm">', vrm_Format($r['vehicle_vrm']), ' </h2>
      </td>
      <td>
          ', date("F dS Y", strtotime($r['mot_expiry'])), '
      </td>
      <td>
          ', $r['client_name'], '
      </td>
      <td>
          ', $status, '
      </td>
        <td>
          <form method="post" action="?">

          <input type="hidden" name="module" value="MOT">

            <input type="hidden" name="mot_id" value="', $r['mot_id'], '">

            <button name="action" value="View">View</button>

            <button name="action" value="Update">Update</button>

            <button name="action" value="Delete">Delete</button>

          </form>
      </td>
    </tr>';

}

echo '</tbody>
  </table>';

  pagination_Build( [ 'current' => $page, 'max' => $numberOfPages, 'link' => "?module=MOT&action=Search" ] );

  require_once "skin/s0/page_footer.php";

Pagination:
Code: [Select]
function pagination_Build($data) {

$page_linkSpan = 2;

$page_setupSpan = [

'start' => ($data['current'] - $page_linkSpan),
'previous' => $data['current']-1,
'next' => $data['current']+1,
'max' => $data['max'],

];

$requiredLoops = ( ( $page_linkSpan * 2 ) + 1 );

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

// FIRST PAGE:
echo ( $data['current'] >= 3 ) ? '<li><a href="' . $data['link'] . '">&laquo;</a></li>' : "";

// PREVIOUS PAGE:
echo ( $data['current'] >= 2 ) ? '<li><a href="' . $data['link'] . '&Page=' . $page_setupSpan['previous'] . '">&lt;</a></li>' : "";

do {

if( $page_setupSpan['start'] > 0 && $page_setupSpan['start'] <= $page_setupSpan['max'] ) {

if ( $page_setupSpan['start'] != $data['current']) {

echo '<li><a href="', $data['link'], '&Page=', $page_setupSpan['start'], '">', $page_setupSpan['start'], '</a></li>';

}
else {

echo '<li><span>', $page_setupSpan['start'], '</span></li>';

}

}

$page_setupSpan['start']++;

$requiredLoops--;

}
while( $requiredLoops > 0 );

// NEXT PAGE
echo ( $data['current'] < $data['max'] ) ? '<li><a href="' . $data['link'] . '&Page=' . $page_setupSpan['next'] . '">&gt;</a></li>' : "";

// LAST PAGE:
echo ( $data['current'] < ( $data['max'] - 1 ) ) ? '<li><a href="' . $data['link'] . '&Page=' . $page_setupSpan['max'] . '">&raquo;</a></li>' : "";

echo '</ul>';

}

I haven't had time to implement all your changes Jason, but I will work towards it! :) Thanks again....
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 1049
  • Karma: +188/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: Strange Pagination Result Problem
« Reply #4 on: 30 Aug 2020, 06:11:37 pm »
What is this nonsense?

   $requiredLoops = ( ( $page_linkSpan * 2 ) + 1 );

and the associated do/while? You know how many there are, why is this not a simple for loop? Also why are you futzing around making a local array for values you're only using internally?!? Just TRYING to make the code run slower?!?

Same could be said of how you're passing data to said function as a pointless array. There should not be enough information for that to even need to be a thing. I mean there's only four real pieces of data to pass. currentPage, perPage, totalItems, and the URIPrefix. (I let the function handle the number of pages based off perPage and totalItems)
We are all, we are all, we are all FRIENDS! For today we're all brothers, tonight we're all friends. Our moment of peace in a war that never ends.

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 787
  • Karma: +8/-0
    • Grumpy Young Man
Re: Strange Pagination Result Problem
« Reply #5 on: 1 Sep 2020, 04:03:56 am »
I'll work on the changes Jason! It's funny as soon as I posted it - I knew you would pass comment!

It doesn't explain why Pages 35, 36 and 37 have duplicated results... (last and first result) does it? (could be others but that's the only one I have noticed so far)
« Last Edit: 1 Sep 2020, 04:06:37 am by GrumpyYoungMan »
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 787
  • Karma: +8/-0
    • Grumpy Young Man
Re: Strange Pagination Result Problem
« Reply #6 on: 1 Sep 2020, 04:29:27 am »
I found out my query on page limit is wrong... it is missing an entry every time due to a query/coding mistake...

I didn't allow for the fact that ZERO is counted as one in the array - back to the drawing board......

Although it is only affecting 14 results per page?
« Last Edit: 1 Sep 2020, 06:59:49 am by GrumpyYoungMan »
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

 

SMF spam blocked by CleanTalk

Advertisement