/    Sign up×
Community /Pin to ProfileBookmark

Teach Me To Convert OOP Pagination To Procedural

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.
Their oop code is:

[code]
<?php
//Required PHP Files.
include ‘header_account.php’; //Required on all webpages of the Account.
?>

<?php
if (!$conn)
{
$error = mysqli_connect_error();
$errno = mysqli_connect_errno();
print “$errno: $errorn”;
exit();
}

// Get the total number of records from our table “students”.
$total_pages = $conn->query(‘SELECT * FROM browsing_histories’)->num_rows;

// 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 ?,?’)) {
// 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);
$stmt->execute();
// Get the results…
$result = $stmt->get_result();
?>
<!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();
}
?>

<?php
include ‘footer_account.php’; //Required on all webpages of the Account.
?>
[/code]

My procedural style code is this alongside their OOP:
I commented-out their lines and added mine beneath their lines that I substituted.

[code]
<?php
//Required PHP Files.
include ‘header_account.php’; //Required on all webpages of the Account.
?>

<?php
if (!$conn)
{
$error = mysqli_connect_error();
$errno = mysqli_connect_errno();
print “$errno: $errorn”;
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
}
?>

<?php
include ‘footer_account.php’; //Required on all webpages of the Account.
?>
[/code]

Is my conversion spot-on ?
Note my CAPITAL LETTERED COMMENTS

Here is my converted code on it’s own:

[code]
<?php
//Required PHP Files.
include ‘header_account.php’; //Required on all webpages of the Account.
?>

<?php
if (!$conn)
{
$error = mysqli_connect_error();
$errno = mysqli_connect_errno();
print “$errno: $errorn”;
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($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);

// Calculate the page to get the results we need from our table.
$calc_page = ($page – 1) * $num_results_on_page;

mysqli_stmt_bind_param($stmt,’sii’,$followee_username,$calc_page,$num_results_on_page);

mysqli_stmt_execute($stmt);

// Get the results…
$result = mysqli_stmt_get_result($stmt)

?>
<!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
mysqli_stmt_close($stmt);
}
?>

<?php
include ‘footer_account.php’; //Required on all webpages of the Account.
?>
[/code]

Note my CAPITAL LETTERED COMMENTS.
That is how I did it. What you say ?
If I did any mistake then kindly show how it should’ve been.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmFeb 03.2019 — You want to learn about mysqli using oop but you give us hundreds of lines of poorly written php and html to look at also?

Really - have you no idea of how to focus on the one thing that you want to talk about?

Learning a method? Try reading the manual and using the examples you will see there and try them out!!!

So ends my lesson (and attempt) at educating you on this subject...
Copy linkTweet thisAlerts:
@site-developerauthorFeb 03.2019 — Php Lovers,

I am stuck on this line and so care to help me convert that from oop to procedural style using mysqli ?

$total_pages = $conn->query('SELECT * FROM browsing_histories')->num_rows; //I NEED HELP TO SUBSTITUTE THIS TO PROCEDURAL STYLEThe rest I managed to convert all on my own with the help of the manual. Look at my original post for that.
Copy linkTweet thisAlerts:
@site-developerauthorFeb 03.2019 — I think this line found in the tutorial is an error:
<i>
</i>$total_pages = $conn-&gt;query('SELECT * FROM browsing_histories')-&gt;num_rows;


It should not have been $total_pages but $total_records. Correct ?

Tutorial here:

https://codeshack.io/how-to-create-pagination-php-mysql/
Copy linkTweet thisAlerts:
@ginerjmFeb 04.2019 — What error do you get from php? Hopefully you HAVE it turned on.

Try breaking up those stupid long run-on statements into their individual components so as to be better able to isolate a problem.

Such as

write the query:

$q = "select.......";

run it

$qresults = query(($conn, $q)

and then ask for the row count

$row_cnt = num_rows($qresults)

It will be easier to find the problem.

PS - the above is NOT real code since I do not use mysqli. Of course you could look it up in the Manual to resolve that.
Copy linkTweet thisAlerts:
@NogDogFeb 04.2019 — > @site-developer#1600713 It should not have been $total_pages but $total_records. Correct ?

It should be whatever you want to call it that makes sense with regard to how you plan to use that variable. If the only reason you care about how many rows the query returned is so that you know how many pages there will be in your pagination for that query, then it makes sense to call it "total_pages", even if the literal thing it returns is the number of query result rows. ?

Long story short: name it however you want to use it later in your code -- which uses the variabel $total_pages in a bunch of places.
×

Success!

Help @site-developer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...