Jump to content
phpsane

Teach Me To Convert OOP Pagination To Procedural

Recommended Posts

Folks,

Look what I found here:
https://codeshack.io/how-to-create-pagination-php-mysql/
It is oop pagination using mysqli.
I only know mysqli and procedural. So, teach me to convert it to procedural.

My procedural style code is this alongside their OOP:

	<?php 
if (!$conn) 
{ 
    $error = mysqli_connect_error(); 
    $errno = mysqli_connect_errno(); 
    print "$errno: $error\n"; 
    exit(); 
} 
	// Get the total number of records from our table "students".
$total_pages = $conn->query('SELECT * FROM browsing_histories')->num_rows; //I NEED HELP TO SUBSTITUTE THIS TO PROCEDURAL STYLE
	// Check if the page number is specified and check if it's a number, if not return the default page number which is 1.
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1;
	// Number of results to show on each page.
$num_results_on_page = 5;
	
//    if ($stmt = $conn->prepare('SELECT * FROM following_histories ORDER BY id LIMIT ?,?')) {
    if($query = "SELECT id,date_and_time,query_type,followed_word,query_string,browsed_page_original,browsed_page_converted,referral_page_original,referral_page_converted,username,gender,age_range,date_of_birth,skin_complexion,height,weight,sexual_orientation,religion,education,profession,marital_status,working_status,country_of_birth,home_town,home_neighbourhood,home_borough,home_council,home_city,home_county,home_district,home_region,home_state,home_country FROM browsing_histories WHERE username = ? ORDER BY id LIMIT ? OFFSET ?"){ //my substitution of above line
    $stmt = mysqli_prepare($conn,$query); //MY SUBSTITUTION OF ABOVE LINE
    
    // Calculate the page to get the results we need from our table.    
    $calc_page = ($page - 1) * $num_results_on_page;
    
    //$stmt->bind_param('ii', $calc_page, $num_results_on_page);
    mysqli_stmt_bind_param($stmt,'sii',$followee_username,$calc_page,$num_results_on_page); //MY SUBSTITUTION OF ABOVE LINE
    
    //$stmt->execute(); 
    mysqli_stmt_execute($stmt); //MY SUBSTITUTION OF ABOVE LINE
    
    // Get the results...
    //$result = $stmt->get_result();
    $result = mysqli_stmt_get_result($stmt)    //MY SUBSTITUTION OF ABOVE LINE
    
    ?>
    <!DOCTYPE html>
    <html>
        <head>
            <title>PHP & MySQL Pagination by CodeShack</title>
            <meta charset="utf-8">
            <style>
            html {
                font-family: Tahoma, Geneva, sans-serif;
                padding: 20px;
                background-color: #F8F9F9;
            }
            table {
                border-collapse: collapse;
                width: 500px;
            }
            td, th {
                padding: 10px;
            }
            th {
                background-color: #54585d;
                color: #ffffff;
                font-weight: bold;
                font-size: 13px;
                border: 1px solid #54585d;
            }
            td {
                color: #636363;
                border: 1px solid #dddfe1;
            }
            tr {
                background-color: #f9fafb;
            }
            tr:nth-child(odd) {
                background-color: #ffffff;
            }
            .pagination {
                list-style-type: none;
                padding: 10px 0;
                display: inline-flex;
                justify-content: space-between;
                box-sizing: border-box;
            }
            .pagination li {
                box-sizing: border-box;
                padding-right: 10px;
                }
            .pagination li a {
                box-sizing: border-box;
                background-color: #e2e6e6;
                padding: 8px;
                text-decoration: none;
                font-size: 12px;
                font-weight: bold;
                color: #616872;
                border-radius: 4px;
            }
            .pagination li a:hover {
                background-color: #d4dada;
            }
            .pagination .next a, .pagination .prev a {
                text-transform: uppercase;
                font-size: 12px;
            }
            .pagination .currentpage a {
                background-color: #518acb;
                color: #fff;
            }
            .pagination .currentpage a:hover {
                background-color: #518acb;
            }
            </style>
        </head>
        <body>
            <table>
                <tr>
                    <th>Name</th>
                    <th>Age</th>
                    <th>Join Date</th>
                </tr>
                <?php while ($row = $result->fetch_assoc()): ?>
                <tr>
                    <td><?php echo $row['id']; ?></td>
                    <td><?php echo $row['followee_username']; ?></td>
                    <td><?php echo $row['follower_username']; ?></td>
                </tr>
                <?php endwhile; ?>
            </table>
            <?php if (ceil($total_pages / $num_results_on_page) > 0): ?>
            <ul class="pagination">
                <?php if ($page > 1): ?>
                <li class="prev"><a href="pagination.php?page=<?php echo $page-1 ?>">Prev</a></li>
                <?php endif; ?>
	                <?php if ($page > 3): ?>
                <li class="start"><a href="pagination.php?page=1">1</a></li>
                <li class="dots">...</li>
                <?php endif; ?>
	                <?php if ($page-2 > 0): ?><li class="page"><a href="pagination.php?page=<?php echo $page-2 ?>"><?php echo $page-2 ?></a></li><?php endif; ?>
                <?php if ($page-1 > 0): ?><li class="page"><a href="pagination.php?page=<?php echo $page-1 ?>"><?php echo $page-1 ?></a></li><?php endif; ?>
	                <li class="currentpage"><a href="pagination.php?page=<?php echo $page ?>"><?php echo $page ?></a></li>
	                <?php if ($page+1 < ceil($total_pages / $num_results_on_page)+1): ?><li class="page"><a href="pagination.php?page=<?php echo $page+1 ?>"><?php echo $page+1 ?></a></li><?php endif; ?>
                <?php if ($page+2 < ceil($total_pages / $num_results_on_page)+1): ?><li class="page"><a href="pagination.php?page=<?php echo $page+2 ?>"><?php echo $page+2 ?></a></li><?php endif; ?>
	                <?php if ($page < ceil($total_pages / $num_results_on_page)-2): ?>
                <li class="dots">...</li>
                <li class="end"><a href="pagination.php?page=<?php echo ceil($total_pages / $num_results_on_page) ?>"><?php echo ceil($total_pages / $num_results_on_page) ?></a></li>
                <?php endif; ?>
	                <?php if ($page < ceil($total_pages / $num_results_on_page)): ?>
                <li class="next"><a href="pagination.php?page=<?php echo $page+1 ?>">Next</a></li>
                <?php endif; ?>
            </ul>
            <?php endif; ?>
        </body>
    </html>
	<?php
//$stmt->close();
mysqli_stmt_close($stmt); //MY SUBSTITUTION OF ABOVE LINE
}
?>
	

Edited by phpsane

Share this post


Link to post
Share on other sites

I commented-out their lines and added mine beneath their lines that I substituted.

Share this post


Link to post
Share on other sites

The only difference between the "OOP" and "procedural" versions is that one uses $object->function_name($arguments) and the other uses similar_function_name($object, $arguments).

I didn't look hard at what you did but I did see you did more than just change the function calls.

  • Like 1

Share this post


Link to post
Share on other sites
14 hours ago, requinix said:

The only difference between the "OOP" and "procedural" versions is that one uses $object->function_name($arguments) and the other uses similar_function_name($object, $arguments).

I didn't look hard at what you did but I did see you did more than just change the function calls.

If you look closely then you will see I originally did what you suggested. However, saying all this, I am stuck on this line and so care to help me convert that ?

$total_pages = $conn->query('SELECT * FROM browsing_histories')->num_rows; //I NEED HELP TO SUBSTITUTE THIS TO PROCEDURAL STYLE
	

Share this post


Link to post
Share on other sites
8 hours ago, phpsane said:

If you look closely then you will see I originally did what you suggested. However, saying all this, I am stuck on this line and so care to help me convert that ?

 


$total_pages = $conn->query('SELECT * FROM browsing_histories')->num_rows; //I NEED HELP TO SUBSTITUTE THIS TO PROCEDURAL STYLE
	

 

->query is mysqli_query(), ->num_rows is mysqli_num_rows().'

8 hours ago, phpsane said:

I think this line found in the tutorial is an error:


$total_pages = $conn->query('SELECT * FROM browsing_histories')->num_rows; 

 

It should not have been $total_pages but $total_records. Correct ?
Tutorial here:
https://codeshack.io/how-to-create-pagination-php-mysql/

I think you should be able to answer that on your own.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.