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.

Member Avatar for LastMitch

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.