Hi,

I am very new to PHP, and i need small help. i need to fetch data on selection box onchange event and then apply pagination to the results. i have done etching data with ajax and when i tried applying pagination i am unable to load parent page of the ajax page. here is the code follows.please help me to solve this problem.

<?php
    include('auth.php');
    $id=$_GET["id"];

    //for pagination
    define('MAX_REC_PER_PAGE', 10);
    $qry ="SELECT count(*) from user_info,time_data where user_info.user_id = time_data.user_id and manager_id= '$timeapp_id' ";
    if($id != 0){
        $qry = $qry." and user_info.user_id = '$id'";
    }

    $results = mysql_query($qry);

    list($total) = mysql_fetch_row($results);
    $total_pages = ceil($total / MAX_REC_PER_PAGE);
    $page = intval(@$_GET["page"]);     
    if (0 == $page){
        $page = 1;
    }       
    $start = MAX_REC_PER_PAGE * ($page - 1);
    $max = MAX_REC_PER_PAGE;

    $query = "select user_info.user_id,fname,lname,description,data_date,hours,notes from user_info,time_data,time_types where user_info.user_id = time_data.user_id 
    and time_data.type_id = time_types.type_id and manager_id= '$timeapp_id'";
    $query2 = " and user_info.user_id = '$id'";
    $query3 = " ORDER BY time_data.data_date LIMIT $start,$max";
    $sqlQuery="";

    if($id != 0){
        $sqlQuery = $query.$query2.$query3;
    }else{
        $sqlQuery = $query.$query3;
    }
    $results = mysql_query($sqlQuery);

?>
<table id="box-table-a" width="100%">
<tr>
    <th>Employee ID</th>
    <th>Employee Name</th>
    <th>Task Type</th>
    <th>Date</th>
    <th>Hours</th>
    <th>Description</th>
</tr>
<div style="width:500px">
<?
    while (list($user_id,$fname,$lname,$description,$data_date,$hours,$notes) = mysql_fetch_row($results)) {
?>
<tr>
    <td><?= htmlspecialchars($user_id) ?></td>
    <td><?= htmlspecialchars($fname) ?> <?= htmlspecialchars($lname) ?></td>
    <td><?= htmlspecialchars($description) ?></td>
    <td nowrap><?= htmlspecialchars($data_date) ?></td>
    <td align="center"><?= htmlspecialchars($hours) ?></td>
    <td><?= htmlspecialchars($notes) ?></td>
    </td>
</tr>
<?
    }
?>
</table>
<table border="0" cellpadding="5" align="center">
        <tr>
            <td>Page:</td>
            <?php
                for ($i = 1; $i <= $total_pages; $i++) {
                    $txt = $i;
                    if ($page != $i)
                        $txt = "<a href=\"" . $_SERVER["PHP_SELF"] . "?page=$i&id=$id\">$txt</a>";
            ?>          
            <td align="center"><?= $txt ?></td>
            <?php
                }
            ?>
        </tr>
</table>
</div>

Thanks in advance.

Dani AI

Generated

The root cause is client-side: the PHP-generated page links point to a full-page URL, so when the list is loaded into the page via AJAX a plain click on a pagination anchor navigates the whole browser to that URL instead of refreshing the AJAX container. The server-side pagination math and SQL (as discussed by and ) can be fine either way, but the interaction between the AJAX loader and the anchors is what needs fixing.

Two practical patterns work well together: keep anchors as real links (for accessibility/fallback) but add a data-page attribute and a consistent class (for example ajax-page), and then intercept clicks with a delegated handler that prevents default navigation and fetches the fragment for insertion. A minimal, widely compatible example:

document.addEventListener('click', function (e) {
  var a = e.target.closest && e.target.closest('a.ajax-page');
  if (!a) return;
  e.preventDefault();
  var page = a.dataset.page || a.getAttribute('data-page');
  var id = document.querySelector('#user-select') && document.querySelector('#user-select').value;
  fetch('time_data.php?id='+encodeURIComponent(id)+'&page='+encodeURIComponent(page))
    .then(function(r){ return r.text(); })
    .then(function(html){ document.querySelector('#results').innerHTML = html; })
    .catch(function(err){ console.error('AJAX pagination error', err); });
});

Server-side should return only the table + pager fragment (not a full HTML wrapper) so insertion is clean. Ensure the count query and the paged query use exactly the same WHERE filters so $total_pages matches the result set. Also hard-validate id and page (e.g. cast to integer) and migrate away from deprecated mysql_* to mysqli or PDO with prepared statements to avoid injection risks.

Reference notes: ’s suggestion about forming a correct SQL is important; is correct that splitting strings for readability is fine. The immediate fix is intercepting pagination clicks (event delegation + AJAX) and keeping server responses as fragments.

Recommended Answers

All 2 Replies

Member Avatar for Member #949455

i have done fetching data with ajax and when i tried applying pagination i am unable to load parent page of the ajax page. here is the code follows.please help me to solve this problem

The reason why you didn't any data is that your query is wrong:

$query = "select user_info.user_id,fname,lname,description,data_date,hours,notes from user_info,time_data,time_types where user_info.user_id = time_data.user_id and time_data.type_id = time_types.type_id and manager_id= '$timeapp_id'";
 $query2 = " and user_info.user_id = '$id'";
 $query3 = " ORDER BY time_data.data_date LIMIT $start,$max";

It should be 1 query:

$query = "SELECT user_info.user_id,fname,lname,description,data_date,hours,notes 
FROM user_info,time_data,time_types 
WHERE user_info.user_id = time_data.user_id 
AND time_data.type_id = time_types.type_id 
AND manager_id= $timeapp_id
AND user_info.user_id = $id 
ORDER BY time_data.data_date 
LIMIT $start,$max";

Then you query should work correctly.

$results = mysql_query($query);

If the query works in one variable, it should/will work separated out into three. He is just breaking it apart for his if statements. I am not seeing a problem with the query in a whole.

Do you see results at all, or does it come up false without the pagination as well?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.