Note that there are some explanatory texts on larger screens.

plurals
  1. POTough Pagination Script
    primarykey
    data
    text
    <p>Okay, so let me explain my situation then I will explain the code. The languages I am using are JavaScript, AJAX, PHP, MySQL. It is a very complicated code I will do my best to explain.</p> <p>Scenario: I created a script that fetches the projects stored in my database. Using javascript I have a function that says if my call to function "fetchProjectWebCall()" has a value within the parenthesis use it otherwise make start == 0.</p> <p>Now using ajax request I send it to PHP. My script in php fetches all the projects stored in my database. When sending the response back I include an array called pagination which gives me the start point, end point, records per page, total records in db, pages needed to display these records.</p> <pre><code>if($result) { if(isset($request-&gt;data-&gt;start)) { // Math for Pagination $start = $request-&gt;data-&gt;start; $end = $start + 9; $records_per_page = 10; $total_records = $result-&gt;num_rows; $total_pages = ceil($total_records / $records_per_page); $array = array(); while($row = $result-&gt;fetch_array()) { $user_id = $row["project_creator"]; $creator_name = User::GetUserName($user_id); $array[] = new Project($row["project_id"], $row["project_name"], $row["project_description"], $user_id, $creator_name); } $array['pagination'] = new Pagination($total_records, $total_pages, $records_per_page, $start, $end); $response-&gt;data = $array; $response-&gt;status = Response_Status::$_SC_SUCCESS; } </code></pre> <p>The PHP/MySQL/AJAX all work properly and generate the right results, so I am led to believe that the problem lies within the JavaScript. Using the AJAX response, I now want to display the records, starting from "start", ending at "end". Here is the JavaScript:</p> <pre><code>if(total &lt; end) { for (var index = start; index &lt; total; index++) { //Variables for Project Info projid = response.data[index].project_id; projname = response.data[index].project_name; htmlString += "&lt;tr class='rowLight' height='30'&gt; &lt;td&gt;" + response.data[index].project_name + "&lt;/td&gt;&lt;td&gt;" + response.data[index].project_description + "&lt;/td&gt;&lt;td&gt;" + response.data[index].project_creator_name + "&lt;/td&gt;" + "&lt;td&gt;&lt;input type='button' id='btnManageProject' value='Edit' onclick='manageProjectWebCall(" + projid + ");'/&gt;&lt;input type='button' id='btnAssignGameToProject' value='Games' onclick='fetchProjectGamesWebCall("+ projid +");'/&gt;&lt;input type='button' id='btnDuplicateProject' value='Duplicate' onclick='duplicateProjectWebCall(" + projid + ");'/&gt;&lt;/td&gt;&lt;/tr&gt;"; } } else { for (var index = start; index &lt;= end; index++) { //Variables for Project Info projid = response.data[index].project_id; projname = response.data[index].project_name; htmlString += "&lt;tr class='rowLight' height='30'&gt; &lt;td&gt;" + response.data[index].project_name + "&lt;/td&gt;&lt;td&gt;" + response.data[index].project_description + "&lt;/td&gt;&lt;td&gt;" + response.data[index].project_creator_name + "&lt;/td&gt;" + "&lt;td&gt;&lt;input type='button' id='btnManageProject' value='Edit' onclick='manageProjectWebCall(" + projid + ");'/&gt;&lt;input type='button' id='btnAssignGameToProject' value='Games' onclick='fetchProjectGamesWebCall("+ projid +");'/&gt;&lt;input type='button' id='btnDuplicateProject' value='Duplicate' onclick='duplicateProjectWebCall(" + projid + ");'/&gt;&lt;/td&gt;&lt;/tr&gt;"; } } </code></pre> <p>^^ this is where the display is created and ultimately where the issue lies. The condition if total &lt; end is used because if the end value was 59 (generated from the php script which is start + 9, start would be 50 in this case) and the total records in the db was 52 (generated from php script) it would not display. The first for block demands that the array start at the index position "start" generated from the original request and go to total, granted the condition if total &lt; end still exists</p> <p>If total isn't &lt; end it should go from index [start] to index &lt;= end, incrementing by 1. The rest of the code works fine.</p> <p><strong>The Issue/Problem</strong> Even though in the last block of code, for every other condition of total and end, the display should go up to and include the end value (starting from start which is 9 less then end). When total = end (example: total_records == 49, start == 40, end == 49, records_per_page == 10, total_pages == 5, generated from response data) it doesn't show the 5th page. It simply gets hung up at page 4 (start == 30, end == 39). This happens whenever total = end (39 records, end = 39 it won't show either)</p> <p>My question finally is how come the script doesn't display the final page, when total=end? I have tried everything including changing the inequalities and including a total=end condition.</p> <p>And another question, reviewing my code it possible to recode without adding any more conditions to the javascript (if total &lt; end etc.). I don't want to change the script much because it works perfectly every other time.</p> <p>Thank you for your time, and hopefully we can come up with a solution. If you need me to explain more please let me know.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload