Jump to content

PHP/SQL - Pagination help


EazYKevin

Recommended Posts

Hello Guys

 

I Have been working on this site of mine for quite some time now, and just uploaded til to my webhost.

 

then i get this error on my pagination, that i dont quite understand, so i hope you guys can help me, because its works perfectly fine on my localhost, i have the complete same database whit all the same information in the tables

 

Localhost: COqcmHD.png

 

 

Webhost: KYxhAHY.png

 

 

Error Code:

 

Fatal error: Uncaught Error: Call to a member function fetch_array() on boolean in /var/www/eshooting.dk/public_html/nyheder.php:91 Stack trace: #0 {main} thrown in /var/www/eshooting.dk/public_html/nyheder.php on line 91

 

 

My Complete PHP File:

 



<!-- Session -->
<?php
error_reporting(1);
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<!-- Meta Tags -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<!-- Database Include -->
<?php
require_once 'include/incl_db.php';
?>

<!-- Functions -->
<?php
require_once 'functions/functions.php';
?>

<!-- Classes -->
<?php
function myAutoloader($class) {
$class = strtolower($class);
require_once $class . '.php';
}
spl_autoload_register('myAutoloader');
?>

<!-- GET -->

<!-- Title -->
<title>eShooting.dk - Nyheder</title>

<!-- Css -->
<link href="css/style.css" type="text/css" rel="stylesheet">
<link href="css/font-awesome.min.css" type="text/css" rel="stylesheet">

<!-- Fonts -->
<link href='http://fonts.googleapis.com/css?family=Lato:100,300,400,700,900,100italic,300italic,400italic,700italic,900italic' rel='stylesheet' type='text/css'>

<!-- Favicon -->
<link rel="icon" href="img/csgo.png" type="image/x-icon"/>
<link rel="shortcut icon" href="img/csgo.png" type="image/x-icon"/>

<!-- jQuery -->
<script src="js/jquery-1.11.1.min.js"></script>
<script src="js/jquery-2.1.1.min.js"></script>

</head>

<body>
<!-- Modal Login -->
<?php
require_once 'include/modal_login.php';
?>
<div id="container">
<div id="wrapper">
<div id="logo">
<a href="index.php"><img src="img/logo.png" /></a>
</div>
<?php
require_once 'include/menu.php';
?>
<div id="content">
<div id="contenttitle">
<h1>Nyheder!</h1>
</div>
<div id="nyheder">
<?php
$per_page=5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
$prev = $page - 1;
$next = $page + 1;
}
else {
header('location:nyheder.php?page=1');
}
// Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page;
//Selecting the data from table but with limit
$sql = "SELECT * FROM es_nyheder LIMIT $start_from, $per_page";
$res = $objCon->query($sql);
?>
<?php
while ($row = $res-> fetch_array()) {
$id = $row['id'];
echo "<div class='nyhed2'>";
echo "<div class='nyhedimg2'>";
echo "<img src='img/csgo.png' />";
echo "</div>";
echo "<div class='nyhedtitle2'>";
echo "<h3>";
echo substr ($row['title'] , 0, 18) . "...";
echo "</h3>";
echo "</div>";
echo "<div class='nyhedtext2'>";
echo "<p>";
echo substr ($row['tekst'] , 0, 38) . ".....";
echo "</p>";
echo "</div>";
echo "<div class='nyheddato2'>";
echo "<p>";
echo "D. ";
echo date('d-m-Y', strtotime($row['dato']));
echo "</p>";
echo "</div>";
echo "<div class='nyhedmere2'>";
echo "<a href='nyhed2.php?id=$id'>Læs mere</a>";
echo "</div>";
echo "</div>";
};
?>
<?php
//Now select all from table
$sql = "SELECT * FROM es_nyheder";
$res = mysqli_query($objCon, $sql);
// Count the total records
$total_records = mysqli_num_rows($res);
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
//Going to first page
echo "<div id='paginationcenter'><div id='pagination'><a href='nyheder.php?page=1'>".'First Page'."</a> ";
// Going to prev page
echo "<a href='nyheder.php?page=$prev'>".'Prev Page'."</a>";
// All Pages
for ($i=1; $i<=$total_pages; $i++) {
echo "<a ";
if($page == $i){
echo "class='currentpage'";
}
echo " href='nyheder.php?page=".$i."'>".$i."</a> ";
};
// Going to next page
echo "<a href='nyheder.php?page=$next'>".'Next Page'."</a>";
// Going to last page
echo "<a href='nyheder.php?page=$total_pages'>".'Last Page'."</a></div></div>";
?>
</div>
</div>
</div>
<div id="scoial">
<div id="scoialtitle1">
<h2>Sponsorer / Partnere!</h2>
</div>
<div id="scoialtitle2">
<h2>Social!</h2>
</div>
<div id="sponsorer">
<?php
$sql = "SELECT * FROM es_sponsorer ORDER BY id ASC";
$res = $objCon->query($sql);
while ($row = $res-> fetch_array()) {
echo '<a href="'.$row["hjemmeside"].'"><img src="img/sponsor-partner/'.$row["billede"].'" alt="'.$row["navn"].'" /></a>';
}
?>
</div>
<div id="scoiale">
<?php
$sql = "SELECT * FROM es_scoial ORDER BY id ASC";
$res = $objCon->query($sql);
while ($row = $res-> fetch_array()) {
echo '<a href="'.$row["hjemmeside"].'"><img src="img/social/'.$row["billede"].'" alt="'.$row["navn"].'" /></a>';
}
?>
</div>
</div>
<div id="footer">
<p>Copyright © <?php $datetime = new DateTime(); echo $datetime->format('Y'); ?> eShooting.dk All Rights Reserved</p>
</div>
</div>
</div>

</body>

</html>



I hope you guys can help whit my problem, since i cant figure it out.

 

Best Regrads

Kevin P


 

Link to comment
Share on other sites

All that code posted but so little of it is actually PHP. Do you do tests on your connection results and db select results? You don't do it on your query results so there are several possibilities for failure right there. Also - echo out your query statement before executing it to be sure you have it setup correctly.

 

A good design practice is to completely separate the bulk of your html code from the php code and perform the latter first. Build your dynamic content into php vars and place them within the static html and only THEN do your output. Makes for a more readable (and understandable) script which also becomes easier to modify and to maintain as time goes by. Comments help to.

Link to comment
Share on other sites

The reason for that error message?

 

You can't issue a 'header()' command once you have already done ANY output to your client. Even an inadvertent space char at the beginning of a line of code outside of php mode would do it. Usually headers are issued only in php mode before you get to the point of issuing any html for that reason.

Link to comment
Share on other sites

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.