user2303233
user2303233

Reputation: 25

How to hide the next button when no pages to dispaly

Can someone help me please? I am sure it is easy for you guys. I am battling to find a solution on how to hide the next link when there are no pages to display my code is as follows:

if (!isset($_GET['page']) or !is_numeric($_GET['page'])) {
    $page = 0;
} else {
    $page = (int)$_GET['page'];
}
$pages_query=mysql_query ("SELECT COUNT * FROM hardware");
$result = mysql_query("SELECT * FROM hardware LIMIT $page, 3");
echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.($page+3).'">Next</a><p>';
$prev = $page - 3;

//only print a "Previous" link if a "Next" was clicked
if ($prev >= 0) {
    echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.$prev.'">Previous</a>';

}

Upvotes: 1

Views: 130

Answers (3)

Ali Alavi
Ali Alavi

Reputation: 2467

You can use mysql_num_rows($result) to get the number of records in hardware:

$result = mysql_query("SELECT * FROM hardware LIMIT $page, 3");
$record_count =  mysql_num_rows($result);
if ($record_count > 1)
    echo 'Next';

Upvotes: 2

Miles Wilson
Miles Wilson

Reputation: 891

something along the lines of:

$itemsPerPage = 3;
$sql = "SELECT * FROM hardware";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
$pageCount = $count/$itemsPerPage;
if($pageCount > $page) { //Are there more pages worth of items stored, than we're currently looking at?
echo 'next';
}

You want to be using OFFSET in your SQL syntax, as well as LIMIT.

LIMIT limits the number of rows returned. OFFSET tells it to start a number of rows into the result set.

You need to limit to the number of items you want on a page. and offset by that number*page.

Hopes this helps.

Upvotes: 0

Justin
Justin

Reputation: 81

in your if statement check if the $page is greater than 0 then according to the outcome of the value of $page write your code. you can use another if statement in the first if statement and make it detect the situation and decide what to do. The other thing is if the user clicked next then the user is on the second page so your previous should appear if $prev is higher than 1 it should make it

Upvotes: 0

Related Questions