syed syed - 1 month ago 6
MySQL Question

PHP Pagination (Data Is Not Showing)

I am having problem while pagination through php.. please help me to get out of this...
Problem : DATA LISTING IS NOT SHOWING AFTER I PASTE PAGINATION CODE.

function to fetch data from db



function get_all_adds($limit = 5, $offset = 0){


$query = "SELECT adds.*, adds.id AS ad_id, categories.*, users.*,


users.id AS user_id FROM adds LEFT JOIN categories ON
adds.cat_id=categories.id LEFT JOIN users ON adds.user_id=users.id
LIMIT " . $limit . " OFFSET " . $offset . "ORDER BY 'ad_id' DESC";

$data = array();

$result = mysql_query($query);

if($result > 0) {

while ($row = mysql_fetch_array($result)) {
$data[] = $row;
}
}

if($result) {
return $data;
} else {
return FALSE;
} }



Code On Index.php




$query = "SELECT * FROM adds";
$rs = mysql_query($query);
$limit = 4;
$offset = 0;
$total = mysql_numrows($rs);
$pages = ceil($total / $limit);


if (isset($_GET['page']) && $_GET['page'] != '') {
$offset = $_GET['page'] * $limit - $limit;


}


$posts = get_all_adds($limit, $offset); }



THE CSS FOR PAGINATION



h3 span {
color: #999;
}

.pages{
list-style: none;
margin-left: 400px;
padding: 0;
alignment-adjust: middle;
}

.pages li{
display: inline-block;
margin-top: 100px;
position: default;
padding: 0 30px;
}
</style>


FINAL TOUCH


<ul class="pagination">
<?php
if ($_GET['page'] < $pages || $_GET['page'] == $pages) {
$p = $_GET['page'] - 1;
if ($p != 0 && $p > 0) { // print markup if page number is not equal to 0
echo '<li><a href="?page=' . $p . '">Previous</a></li>';
}
}



for ($i = 1; $i <= $pages; $i++) {
echo '<li><a href="?page=' . $i . '">' . $i . '</a></li>';
}


if ($_GET['page'] < $pages) {
$p = $_GET['page'] + 1;
echo '<li><a href="?page=' . $p . '">Next</a></li>';
}
?> </ul>



HERE IS THE OUTPUT IMAGE HAVING NO DATA BUT PAGES LINKS ARE THERE

enter image description here

Answer

Thanks for your time... I reached to it..

QUERY SHOULD BE LIKE:

$query = "SELECT adds., adds.id AS ad_id, categories., users.*,
users.id AS user_id FROM adds LEFT JOIN categories ON adds.cat_id=categories.id LEFT JOIN users ON adds.user_id=users.id ORDER BY 'ad_id' DESC LIMIT " . $limit . " OFFSET " . $offset ;

INSTEAD :

$query = "SELECT adds., adds.id AS ad_id, categories., users.*,
users.id AS user_id FROM adds LEFT JOIN categories ON adds.cat_id=categories.id LEFT JOIN users ON adds.user_id=users.id LIMIT " . $limit . " OFFSET " . $offset . "ORDER BY 'ad_id' DESC";