Pagination data

You need to study their code more, dont just copy&paste. You told the program to echo those variables and it echoed them for you. In the tutorial they seem to store all the ‘paginatable’ data in the $list, so you need to look into that. $id = $row["id"]; prints id column from the current table in mySql

Edit: I will try to explain what most of the lines in the website you provided do.
I do not know if you are familiar with SQL, but you need to know basics of mysql to be able to create your pagination script.

$sql = "SELECT COUNT(id) FROM testimonials WHERE approved='1'";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_row($query);

$sql prepares the sql statement that will count entries in the mysql table testimonials whose approved column is set to 1. Then $query uses the mysqli_query to process the sql statement ($db_con is declared in mysqli_connection.php, it tells the server how to connect to the database). Check here

$row is an array and here it only consists of 1 element – the number of entries in testimonials with approved=1. Therefore $rows is declared and it is set to that number — $row[0].

$page_rows = 10;
$last = ceil($rows/$page_rows);

ceil is a php function that rounds a number up to the closest integer (0.4 -> 1, 5.1 -> 6). $page_rows tells how many testimonials you want to be displayed per page, so $last takes total number of testimonials and divides them by 10 and the result is the number of pages. Check here

I will skip the next few lines of their code because they are very straightforward.

    $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);

$_GET is a superglobal that collects data from the URL. If you have 18 testimonials, if you go to page 2, the url will take the form of “your_website/?pn=2”. So here $pagenum is set to the value of the superglobal pn only if it is set (because by default, when you are on page 1 pn is not set) Check here

$limit="LIMIT " .($pagenum - 1) * $page_rows .',' .$page_rows;

This is also an SQL statement that tells the server that the number of testimonials to be shown should be limited to 10 Check here

$sql = "SELECT id, firstname, lastname, datemade FROM testimonials WHERE approved='1' ORDER BY id DESC $limit";
$query = mysqli_query($db_conx, $sql);

These two are once again mysql statements that select the columns id, firstname, lastname, datemade from your table with approved=1 and order all of the entries by their id in reverse order Check here

Now I am pretty sure after you study all of the above, you will be able to figure out what the next lines do (their comments should help as well).

Important part is here:

while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
    $id = $row["id"];
    $firstname = $row["firstname"];
    $lastname = $row["lastname"];
    $datemade = $row["datemade"];
    $datemade = strftime("%b %d, %Y", strtotime($datemade));
    $list .= '<p><a href="".$id.'">'.$firstname.' '.$lastname.' Testimonial</a> - Click the link to view this testimonial<br>Written '.$datemade.'</p>';

while statement is essential to know in PHP. It basically keeps executing the code in { code } while the condition in while (condition) is true. Important to note that $row is an array (type var_dump($row) to see what $row contains). $row here should contain your mysql table entry values. So if your mysql table has a column called post_content, you can output its value using $row[“post_content”].

mysqli_close closes the connection to the database.

If there is anything you dont understand let me know

Browse More Popular Posts

Leave a Comment