1

I want to create pagination using while loop.

I already show data limit to 10. But I have no idea how to show the pagination number in time calculation below.

Any solutions?

Thank you.

<table border=0 width=800 cellspacing=1 cellpadding=2>
  <tr height=22>
    <td align=center bgColor=red width=5%>No.</td>
    <td align=center bgColor=red width=15%>Date</td>
    <td align=center bgColor=red width=20%>Time</td>
    <td align=center bgColor=red width=15%>Status</td>
  </tr>

<?php
    $start_hour = "05";
    $start_min = "00";
    $end_hour = "15";
    $end_min = "00";
    
    $start = new DateTime("$start_hour$start_min");
    $end = new DateTime("$end_hour$end_min");   
    $article_num = 1;
    $resultPerPage = 10;
    $pageNum = 1;
    
    while (($start < $end) && ($article_num <= $resultPerPage)) {
        $start_time = $start->format('H:i');
        $key_time = $start->format('Hi');
        $start->modify('+10 minute');
        $end_time = $start->format('H:i');
        
        $link_reserv = "#";
        
        echo ("<tr>");
        echo ("<td align=center>$article_num</td>");
        echo ("<td align=center>2013/12/16</td>");
        echo ("<td align=center>$start_time - $end_time</td>");
        echo ("<td align=center><input type=button value=Book&nbsp;Now onclick=\"window.location.href='$link_reserv';\"></td>");
        echo ("</tr>");
        
        $article_num++;
    }
?>
</table>
3
  • 1
    Hint: pages = floor(total / recordsPerPage). Commented Dec 4, 2013 at 3:47
  • 1
    Just get the total_num_rows and divide by how many records to be displayed in a page Commented Dec 4, 2013 at 3:50
  • I already get number of pages from this while (($start < $end)) ... I remove the ($article_num <= $resultPerPage), and the pages number successfully i get. And one problem is. How to limit the while loop? Commented Dec 4, 2013 at 4:04

1 Answer 1

0

Full CODE

<?php
$conn = mysql_connect("localhost", "root", "");
$sdb = mysql_select_db("pagination", $conn);

$tableName = "emp_details"; //page name
$targetpage = "pagination.php";
$limit = 3; //limit of dispaly

$query = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

$stages = 3;
$page = mysql_escape_string($_GET['page']);
if ($page) {
    $start = ($page - 1) * $limit;
} else {
    $start = 0;
}

// Get page data
$sel = mysql_query("select * FROM $tableName LIMIT $start, $limit");
while ($row1 = mysql_fetch_array($sel)) {
    $id = $row1['emp_id'];
    $name = $row1['emp_name'];
    $doj = $row1['doj'];
    $experince = $row1['experince'];
    $salary = $row1['salary'];
    ?>
    <tr>
        <td height="20"><?php echo $id; ?></td>
        <td><?php echo $name; ?></td>
        <td><?php echo $doj; ?></td>
        <td><?php echo $experince ?>year</td>
        <td><?php echo $salary ?></td>

    </tr>
<?php } ?>
<tr>
    <td colspan="5">
        <?php

        // Initial page num setup
        if ($page == 0) {
            $page = 1;
        }
        $prev = $page - 1;
        $next = $page + 1;
        $lastpage = ceil($total_pages / $limit);
        $LastPagem1 = $lastpage - 1;


        $paginate = '';
        if ($lastpage > 1) {

            $paginate .= "<div class='paginate'>";
            // Previous
            if ($page > 1) {
                $paginate .= "<a href='$targetpage?page=$prev'>previous</a>";
            } else {
                $paginate .= "<span class='disabled'>previous</span>";
            }


            // Pages
            if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
            {
                for ($counter = 1; $counter <= $lastpage; $counter++) {
                    if ($counter == $page) {
                        $paginate .= "<span class='current'>$counter</span>";
                    } else {
                        $paginate .= "<a href='$targetpage?page=$counter'>$counter</a>";
                    }
                }
            } elseif ($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
            {
                // Beginning only hide later pages
                if ($page < 1 + ($stages * 2)) {
                    for ($counter = 1; $counter < 4 + ($stages * 2); $counter++) {
                        if ($counter == $page) {
                            $paginate .= "<span class='current'>$counter</span>";
                        } else {
                            $paginate .= "<a href='$targetpage?page=$counter'>$counter</a>";
                        }
                    }
                    $paginate .= "...";
                    $paginate .= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                    $paginate .= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
                } // Middle hide some front and some back
                elseif ($lastpage - ($stages * 2) > $page && $page > ($stages * 2)) {
                    $paginate .= "<a href='$targetpage?page=1'>1</a>";
                    $paginate .= "<a href='$targetpage?page=2'>2</a>";
                    $paginate .= "...";
                    for ($counter = $page - $stages; $counter <= $page + $stages; $counter++) {
                        if ($counter == $page) {
                            $paginate .= "<span class='current'>$counter</span>";
                        } else {
                            $paginate .= "<a href='$targetpage?page=$counter'>$counter</a>";
                        }
                    }
                    $paginate .= "...";
                    $paginate .= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                    $paginate .= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
                } // End only hide early pages
                else {
                    $paginate .= "<a href='$targetpage?page=1'>1</a>";
                    $paginate .= "<a href='$targetpage?page=2'>2</a>";
                    $paginate .= "...";
                    for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++) {
                        if ($counter == $page) {
                            $paginate .= "<span class='current'>$counter</span>";
                        } else {
                            $paginate .= "<a href='$targetpage?page=$counter'>$counter</a>";
                        }
                    }
                }
            }

            // Next
            if ($page < $counter - 1) {
                $paginate .= "<a href='$targetpage?page=$next'>next</a>";
            } else {
                $paginate .= "<span class='disabled'>next</span>";
            }

            $paginate .= "</div>";


        }
        echo $total_pages . ' Results';
        // pagination
        echo $paginate;
        ?></td>
</tr>
</table>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.