Jump to content

Help with pagination in php


Go to solution Solved by omardavinci,

Recommended Posts

Hi i am trying to do a simple pagination in php but when i try for example go to some page like localhost/page=1 i get this result:The requested URL /www/page=2 was not found on this server. Obviously this is because i did some mistake in the code. I share with members the php and html file for if someone has some suggestion. Thanks in advance:

 

html:

 

<!DOCTYPE html>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><?php echo $title; ?></title>
        <link rel="stylesheet" type="text/css" href="Styles/Stylesheet.css" />
    </head>
    <body>
        <div id="wrapper">
            <div id="banner">             
            </div>
            
            <nav id="navigation">
                <ul id="nav">
                    <li><a href="index.php">Home</a></li>
                    <li><a href="tool.php">Coffee</a></li>
                    <li><a href="manager.php">Shop</a></li>
                    <li><a href="#">About</a></li>
                </ul>
            </nav>
            
            <div id="content_area">
                <?php
                require_once("tool.php");
                foreach ($articles as $article):
                    echo $article['id'];
                endforeach;?></br><?php
                for($x=1;$x<=$pages;$x++):?>
                    <a href="page=<?php echo $x;?>"><?php echo $x;?></a>
              <?php  endfor;?>
            </div>
            
            <div id="sidebar">
                
            </div>
            
            <footer>
                <p>All rights reserved</p>
            </footer>
        </div>
    </body>
</html>
 

 

php:
 

 

<?php

 
require_once 'database.php';
$database_connection = database_connect();
$title='hola';
 
$content='<div>';
 
//user input
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = 1;
//Positioning
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
$art = $database_connection->query("SELECT id FROM coffee");
//Query
$articles = $database_connection->prepare("SELECT id FROM coffee LIMIT 0,5");
 
$articles->execute();
$articles = $articles->fetchAll();
 
$resultado = $database_connection->query("SELECT COUNT(*) AS total FROM coffee");
$fila = $resultado->fetch(PDO::FETCH_ASSOC);
$total = $fila["total"];
$pages = ceil($total/$perPage);
 
echo $pages;
$content .= '<div>';
 
 
include 'Template_1.php';
?>
 

any suggestion, dissapointment is good received

Link to comment
https://forums.phpfreaks.com/topic/302180-help-with-pagination-in-php/
Share on other sites

I'm not even going to look at the code. Your URL is definitely not a URL. Just look at it - where's the domain name? Obviously your(?) code is missing something and I think it is up to you to at least TRY to figure out why you don't have a better looking URL. Not us.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.