Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Adding dropdown pagination limit
Old 11-08-2009, 10:02 AM Adding dropdown pagination limit
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
Im trying to add a pagination limit to my pagination script, so I can limit the amount.

I got this working when the page is loaded with a number at the end like:

http://www.example.com?limit=10

How do I do it so it automatically sets the number to 5 when there is no variable?

Code:
<form>
<select name="file" size="1"
  onchange="loadPage(this.form.elements[0])" target="_parent._top"
            <option value="?limit=9">9</option>
            <option value="?limit=15">15</option>
            <option value="?limit=30">30</option>
            <option value="?limit=all">All</option>
</select>
</form>


<?php

require_once("inc/config.php");
if (isset($_POST['del'])) 
{
  for ($count = 0;$count<count($_POST[delchk]);$count++)
    {
           $delete = $_POST[delchk][$count];
           $query = "DELETE FROM accounts WHERE id = '$delete'";
           $result = mysql_query($query);
           if (!$result) 
        {
            die("Error deleting accounts! Query: $query<br />Error: ".mysql_error());
        }
    }
}
$result = mysql_query("SELECT * FROM accounts");
if(mysql_num_rows($result) > 0)
{
  echo "<table class=\"gridtable\">
<thead>
    <tr>
        <th align=\"center\" scope=\"col\">Username</th>
        <th align=\"center\" scope=\"col\">Password</th>
        <th align=\"center\" scope=\"col\">Highscores</th>
        <th align=\"center\" scope=\"col\">Date</th>
        <th align=\"center\" scope=\"col\">IP Address</th>
        <th align=\"center\" scope=\"col\">Status</th>
       <th align=\"center\" scope=\"col\">Delete?</th>
    </tr>
</thead>
<tbody>";
echo "<form name = 'myform' action='' method='post'>";
$data = mysql_query("SELECT * FROM `accounts`") or die(mysql_error());
$rows = mysql_num_rows($data);
$page_rows = $_GET["limit"];
if(!isset($_GET['pagenum']))
{
$page = 1;
}
else
{
$page = (int) $_GET['pagenum'];
}
?>
I get this error when I try to load the page like: http://www.example.com

Code:
Warning: Division by zero in /home/cherdak/public_html/accounts.php on line 310

Warning: Division by zero in /home/cherdak/public_html/accounts.php on line 311
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

but no error when I load the page like: http://www.example.com?limit=10


I made bold where the $_Get is.
__________________
MY MSN:
Please login or register to view this content. Registration is FREE

PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
sith717 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-08-2009, 11:37 AM Re: Adding dropdown pagination limit
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
Got it to work.

PHP Code:
$page_rows $_GET["limit"];
if (
$page_rows == '')
{
$page_rows "5";

Basicly if the ?limit= is blank just show 5 of them. Now how do I add a All dropdown?

Like ?limit=all

What code would i add? Like show all the rows.
__________________
MY MSN:
Please login or register to view this content. Registration is FREE

PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-08-2009, 03:28 PM Re: Adding dropdown pagination limit
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Do you want to limit the number of items per page, or the number of pages?

I'm guessing the first...
Then it doen't really matter what you set it to, just something that represents "not a number between 1 and max_nbr_of_pages". So, you can set it to 'all', or -1 i.e.

Then when you build your SQL LIMIT clause, check if the variable has been set to this value, and if so don't add a LIMIT clause.

PHP Code:
// make sure the limit is set and is a number
$default 3;
$itemsPerPage = isset($_GET['limit'] && is_numeric($_GET['limit'])) ? $_GET['limit'] : $default;

// ... some other code ...

// build a limit clause
$limit = ($itemsPerPage == -1) ? null " LIMIT $itemsPerPage";
$query "SELECT ... $limit"
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to Adding dropdown pagination limit
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11431 seconds with 12 queries