careym1989 Posted December 24, 2008 Share Posted December 24, 2008 Hello everyone! I'm having trouble figuring out what I should do. I have a table in my database that will have tons of information, as it's recording traffic on my website. However, in my home-grown control panel, I want to be able to view all of the recorded information (what I imagine would be hundreds and hundreds of records). How would I limit the number of records retrieved based on the ID per page? Additionally, how would I tell PHP to create pages of information after the first page? Obviously I could do include("Connect.php"); $sql = "SELECT * FROM mytable ORDER BY id ASC LIMIT 25"; $query = mysql_query($query); However, that would only show the first 25 and wouldn't let me view the pages and pages of information after those 25 hits. Let me know if anyone has a solution and/or if there needs to be more clarification. Appreciate it, all! -Carey Quote Link to comment https://forums.phpfreaks.com/topic/138285-specified-number-of-hits-per-page/ Share on other sites More sharing options...
RussellReal Posted December 24, 2008 Share Posted December 24, 2008 LIMIT 0,25 LIMIT 25,25 what you could do is.. LIMIT ((page - 1) * 25),25 that would do.. lets say page is 3 (3 - 1) = 2 2 * 25 = 50 start from 50 and get 25 rows.. being 51 - 75 Quote Link to comment https://forums.phpfreaks.com/topic/138285-specified-number-of-hits-per-page/#findComment-723015 Share on other sites More sharing options...
careym1989 Posted December 24, 2008 Author Share Posted December 24, 2008 That sounds perfect. How would I go about coding that in the SELECT statement and using the URL and $_GET to put hits on a page number? (e.g. Records.php?page=3) Quote Link to comment https://forums.phpfreaks.com/topic/138285-specified-number-of-hits-per-page/#findComment-723036 Share on other sites More sharing options...
RussellReal Posted December 24, 2008 Share Posted December 24, 2008 include("Connect.php"); $page = ((!is_nan($_GET['page']))? $_GET['page']:1); $sql = "SELECT * FROM `mytable` ORDER BY `id` ASC LIMIT ".(($page - 1) * 25).",25"; $query = mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/138285-specified-number-of-hits-per-page/#findComment-723039 Share on other sites More sharing options...
careym1989 Posted December 24, 2008 Author Share Posted December 24, 2008 Perfect. I even hate to ask this question because you've been so helpful, but how would I go about making links to keep the pages rolling dynamically instead of me having to statically adding the number and links at the bottom? Long question, probably an easy answer. Thanks RussellReal! Quote Link to comment https://forums.phpfreaks.com/topic/138285-specified-number-of-hits-per-page/#findComment-723048 Share on other sites More sharing options...
RussellReal Posted December 24, 2008 Share Posted December 24, 2008 okay.. assuming you want ALWAYS at max 10 page links at the bottom. <? include("Connect.php"); $page = ((!is_nan($_GET['page']))? $_GET['page']:1); $sql = "SELECT * FROM `mytable` ORDER BY `id` ASC LIMIT ".(($page - 1) * 25).",25"; $sql2 = mysql_fetch_assoc(mysql_query("SELECT count(*) As num FROM `mytable`")); $query = mysql_query($query); $start = ((($a = ($page - 5)) < 1)? 1:$a); $pages = ($sql2['num'] / 25); $end = ((($a = ($page + 4)) > $pages)? $pages:$a); $range = range($start,$end); $links = array(); if ($page > 1) $links[] = "<a href=\"Records.php?page=".($page - 1)."\">Prev</a>"; foreach ($range as $k => $v) { $links[] = (($v == $page)? $v:"<a href=\"Records.php?page={$v}\">$v</a>"); } if ($page < $pages) $links[] = "<a href=\"Records.php?page=".($page + 1)."\">Next</a>"; echo implode(" ",$links); ?> THERE MAY BE A FEW ERRORS.. AS I HAVN'T TESTED THIS.. Quote Link to comment https://forums.phpfreaks.com/topic/138285-specified-number-of-hits-per-page/#findComment-723056 Share on other sites More sharing options...
careym1989 Posted December 24, 2008 Author Share Posted December 24, 2008 Wow -- thanks. That's intense. I can do some, from what I know of my own skills, basic-intermediate programming. What book / website did you check out to learn all of that incredible code? -Carey Quote Link to comment https://forums.phpfreaks.com/topic/138285-specified-number-of-hits-per-page/#findComment-723060 Share on other sites More sharing options...
RussellReal Posted December 24, 2008 Share Posted December 24, 2008 to be honest.. I really didn't read a book.. or use any tutorials.. I just kind've browsed php.net everytime I needed to find a function lol.. also, looking @ php codes that were already written kinda made me curious as to WHAT MAKES THIS SCRIPT DO WHAT IT DOES.. basically lol if you want to keep in touch, heres my email/MSN: RussellonMSN [AT] hotmail.com Quote Link to comment https://forums.phpfreaks.com/topic/138285-specified-number-of-hits-per-page/#findComment-723074 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.