I was wondering how do I get my site to stop showing empty pages if no more data exists. Example: http://www.toppremadelayouts.com/layouts/holiday/page-4.htm No more data entries are found to fill page 4 yet it's still showing the Next page button. How would I make to where if there is no more data to show, then no next page link. This is what i'm using to get to the next page and previous page, it's simple, but creates an infinite amound of pages.... $npage = 1 if($_GET['page'] != ""){ $npage = $_GET['page']; } $prevpage = $npage - 1; $nextpage = $npage + 1; $plink = '<a href="http://www.toppremadelayouts.com/layouts/'.$desc1.'/page-'.$prevpage.'.htm"><< Previous Page</a>'; if ($prevpage == 0){ $prevpl = ""; } else { $prevpl = $plink; } } PHP:
Before making the link to the next page, you will have to check if the database has enough records... $sql= mysql_query("SELECT * FROM table where condition"); $tn= mysql_numrows($sql); //$tn now has total number of results available. $rpp= 10; // results on 1 page. if(ceil($tn/$rpp)<=($npage+1)) { //show next link } else { //No need for else statement... }
That works in a way, but it counts all the rows in the table. I need it to count the number of rows that are in a certain category.
What I guess i'm trying to do exatly is count the number of rows from a specific datafield in a table. Example: datafield 'category' table 'data' I want the query to count the amount of of rows that have the same category name in the table data. That way I can show a next-page link if more rows are available and if not don't show a next page link, so I don't get blank pages of data.
You only need to modify the sql query in the example code above. example: $cat= "some category"; $sql= mysql_query("SELECT * FROM `data` WHERE `category`='$cat'"); $tn= mysql_numrows($sql); Bye
I have multiple categories. i'm trying to do $cat = $row['category']; but i'm not getting any results. i'm sorry if this seems easy, but i just can't visualize the solution. :\
Here my complete php code.: include("includes/dbaccess2.php"); $cats = array(" ","General","Music","Sports"); $catsp = array(" ","general","music","sports"); $npage = 1; $s = 1; $pp = 10; if($_GET['cat'] != ""){ $catter = $_GET['cat']; } if($_GET['page'] != ""){ $npage = $_GET['page']; } if($_GET['pp'] != ""){ $pp = $_GET['pp']; } if($_GET['s'] != ""){ $s = $_GET['s']; } $prevpage = $npage - 1; $nextpage = $npage + 1; $spage = (($npage-1)*$pp) + 1; $fpage = $spage + ($pp-1); $sql = "SELECT * FROM `data`"; $result = mysql_query($sql); if ($_GET['ID'] == ""){ while($row = mysql_fetch_array($result)) { if($_GET['cat'] == "" || $_GET['cat'] == $row['cat_id']){ $grabber = $row['cat_id']; $desc = ""; $desc1= ""; } else { $desc = $cats[$grabber]; $desc1 = $catsp[$grabber];} } $category = $row['cat_id']; $sq = mysql_query("SELECT * FROM `data` WHERE `cat_id`='$category'"); $tn = mysql_numrows($sq); $nextl = $tn; $plink = '<a href="http://site.com/data/'.$desc1.'/page-'.$prevpage.'.htm"><< Previous Page</a>'; if ($prevpage == 0){ $prevpl = ""; } else { $prevpl = $plink; } $rpp = 10; if(ceil($tn/$rpp)<=($npage+1)) { $next = '<a href="http://site.com/data/'.$desc1.'/page-'.$nextpage.'.htm">Next Page >></a>'; } else { $nextl = ''; } PHP:
for previous page $prevpage = $npage - 1; PHP: for next page $nextpage = $npage + 1; PHP: for current page $npage PHP: this code $spage = (($npage-1)*$pp) + 1; PHP: is for finding the records starting number, but i didnt understand why you are using +1 in the end, may be you have some reason for that. Each records in the mysql is starting from "0", above code miss one record...but if you are starting mysql row ids from "1" then its ok....otherwise use this $spage = ($npage - 1) * $pp; PHP: this is not needed because you have $pp $fpage = $spage + ($pp-1); PHP: You are calling all the datas in the database table 'data', with this query, $sql = "SELECT * FROM `data`"; PHP: you are using the correct query for finding total records categorised, use the same for listing also.. $sql = "SELECT * FROM `data` WHERE `cat_id`='$category'"; PHP: if you want to do pagination, then you have to use the below code....means start listing records from $spage to $spage + $pp (records start from '0') $sql = "SELECT * FROM `data` WHERE `cat_id`='$category' LIMIT $spage, $pp"; PHP: I think this will solve your problem if not then post here, here you can find lots of experts....