How to put a. php pagination script on my website?

Asked

Viewed 521 times

0

I’ve already built a website, but I’m having trouble with the paging system. I don’t know how to handle php, I found some videos on youtube and got a script of php pagination. Now I need to put this script inside my site.

I think for this I need to link the . php file inside my . html type file when the guy uses the 'link' command in html. But I don’t know how to do it.

This is the php pagination script:

The part of database connection is in the first 15 lines of the script I pasted.

<?php
$servername = "Localhost";
$username = "id6683339_unknn";
$password = "****";

// Create connection
$conn = mysql_connect("mysql#.000webhost.com","id6683339_unknn","****") or die(mysql_error());

// Check connection
if ($conn->connect_error) {
    `insira o código aqui`die("Connection failed: " . $conn->connect_error);} 
echo "Connected successfully";
?>

<?php
    $maxlinks = 4;
    $pagina = (isset($_GET['pagina'])) ? (int)$_GET['pagina'] : 1;
    $maximo = 12;
    $inicio = (($maximo * $pagina) - $maximo);

    $selecao = $pdo->prepare("SELECT * FROM `posts` ORDER BY id DESC LIMIT $inicio, $maximo");
    $selecao->execute();
    while($posts = $selecao->fetchObject()):
?>
    <li><?php echo utf8_encode($posts->titulo);?></li>
<?php endwhile;?>

<?php
    $seleciona_2 = $pdp->prepare("SELECT * FROM `posts`");
    $seleciona_2->execute();
    $total = $seleciona_2->rowCount();
    $total_paginas = ceil($total/$maximo);

if($total > $maximo){

    echo '<a href="?pagina=1">Primeira pagina</a>';
    for($i = $pagina - $maxlinks; $i <= $pagina -1; $i++){
        if($i >= 1){
            echo '<a href="?pagina='.$i.'">'.$i.'</a>';
        }
    }
    echo '<span>'.$pagina.'</span>';
    for($i = $pagina +1; $i <= $pagina + $maxlinks; $i++){
        if($i <= $total_paginas){
            echo '<a href="?pagina='.$i.'">'.$i.'</a>';
        }
    }
    echo '<a href="?pagina='.$total_paginas.'">Ultima pagina</a>';
}
?>

I don’t know if the paging script is right, but the question is I don’t know how to put it on my website. I hosted my website on 000webhost.com, I found that to mess with file. php needs to use phpmyadmin, 000webhost uses phpmyadmin database but I don’t know how to work it, I started learning programming a couple of weeks ago.

In phpmyadmin there are 3 folders, I don’t know if you need to put the file. php inside a special folder.phpmyadmin

It would be easier to use apache server, but at the moment it is not working on my pc, I need to format it.

  • 1

    places it in the place where it will be in html include "nome_do_arquivo_de_paginacao.php;"

  • 1

    Your HTML file must have the php extension to work what @Weessmith said. Type home.html has to be home.php

  • 1

    Thanks for the comment @Andreicoelho

  • Before doing this I suggest learning php, and at least have a notion of database, make a functional pagination is something a little advanced for those who started programming 2 weeks

  • @You’re absolutely right, and I’m gonna do just that.

  • Friend, if you need to do php/html paging, do not complicate your life by making the entire pagination in hand, I would advise, first of all, to do what @Guilhermecostamilam said, study php/database. Another thing, this pagination can be used datagrid. Much simpler, nicer and more efficient https://www.pontikis.net/labs/bs_grid/demo/

  • @gabrielfalieri datagrid is certainly simpler, beautiful is opinion, but it is not more efficient, imagine that the paging is 1000 lines from the bank, having to load all this for at the end the user go at most to page two, can even be considered efficient in relation to the agility in the development, but not in the performance of the system

  • Dude, have you seen how datagrid works? That it sends the request with an order by 10 to 19, 20 to 29 and so on.. never the 1000 bank lines at once

  • I haven’t seen this, but anyway you have to make the changes in php, validate these LIMIT and OFFSET not to have SQL Injection. At the end of more work. Obs: ORDER BY does not make pagination

Show 4 more comments

1 answer

1

First let’s define a few things, phpMyAdmin is your interface for database management, where it is done storing the records that will be recovered at some point, such as texts and etc. Your file . php, should be stored in your hosting, along with other files . html and etc... It will communicate with your database to retrieve the information to be paged. In order for your paging to work, you’ll need to embed php in your html page, using include or require, read more about it:

Include: http://php.net/manual/en/function.include.php

Require: http://php.net/manual/en/function.require.php

Example with include:

include 'diretorio1/diretorio2/paginacao.php';

But from what I understand, only this information will not solve your problem, because other knowledge needs to be acquired in order for this pagination to work and be implemented more appropriately. I believe that in your learning of php, you see that some attitudes should be taken with respect to this paging file, an example would be the extraction of your database connection to another file. I don’t know what the purpose of your project is, but in many cases it is worth using a CMS (Content Manager System), like Joomla or Wordpress, things like paging, it would be something not to worry about, BUT that doesn’t mean you won’t have to study how it works.

  • I researched a lot about this and nothing, I think I better go learn the basics of php. Maybe it’s because I’m new but comparing HTML, JS, CSS, PHP, I found php very difficult. I just wanted a pagination system so my site would be organized but all right kk.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.