PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
davy2001
Forum Commoner
Posts: 37 Joined: Tue Feb 24, 2004 5:59 pm
Post
by davy2001 » Mon Aug 23, 2004 8:49 pm
Is it possible to have PHP to do a calculation..let me explain:
Code: Select all
$nextpage="$check-10";
$nextpage="$check+10";
I think you know what i mean from that?
David
neophyte
DevNet Resident
Posts: 1537 Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota
Post
by neophyte » Mon Aug 23, 2004 8:55 pm
Code: Select all
$check = 50;
$nextpage=$check-10;
$nextpage=$check+10;
davy2001
Forum Commoner
Posts: 37 Joined: Tue Feb 24, 2004 5:59 pm
Post
by davy2001 » Mon Aug 23, 2004 8:57 pm
well....i dont think that would work for me
Here is what i have:
Code: Select all
$sql = "SELECT * FROM users ORDER BY id ASC LIMIT ".$limit."10";
$nextpage="$limit-10";
$prevpage="$limit+10";
print "<a href=users.php?limit=$prevpage>last 10 results</a>
<a href=users.php?limit=$nextpage>next 10 results</a>"
Some did tell me to go search for pagilation or something but there must be another way
David
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Aug 23, 2004 9:02 pm
$nextpage = $limit + 10;
$prevpage = $limit - 10
standard math..
add a comma before the 10 in your query string..
davy2001
Forum Commoner
Posts: 37 Joined: Tue Feb 24, 2004 5:59 pm
Post
by davy2001 » Mon Aug 23, 2004 9:03 pm
Can't select entries from table php_blog.
SELECT * FROM users ORDER BY id ASC LIMIT ,10
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 '10' at line 1
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/username/public_html/users.php on line 11
last 10 results next 10 results
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Aug 23, 2004 9:04 pm
you need to write a default value for $limit if it's not set....
davy2001
Forum Commoner
Posts: 37 Joined: Tue Feb 24, 2004 5:59 pm
Post
by davy2001 » Mon Aug 23, 2004 9:04 pm
how?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Aug 23, 2004 9:11 pm
where's $limit supposed to be coming from? the url?
Code: Select all
$limit = isset($_GET['limit']) ? abs(intval($_GET['limit'])) : 0;
although you may want to name the variable $start with foo.php?start=5 for example.. since that's how you are using it.
davy2001
Forum Commoner
Posts: 37 Joined: Tue Feb 24, 2004 5:59 pm
Post
by davy2001 » Mon Aug 23, 2004 9:14 pm
hmm, that sort of worked but when i click next page, or previous page it goes to "mypage.php?limit=0".
every time i press it
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Aug 23, 2004 9:30 pm
what's your code look like now?
davy2001
Forum Commoner
Posts: 37 Joined: Tue Feb 24, 2004 5:59 pm
Post
by davy2001 » Mon Aug 23, 2004 9:31 pm
Code: Select all
<?php
mysql_connect ('localhost', 'username', 'password') ;
mysql_select_db ('users');
$limit = isset($_GET['limit']) ? abs(intval($_GET['limit'])) : 0;
$sql = "SELECT * FROM users ORDER BY id ASC LIMIT ".$limit.",10";
$result = mysql_query($sql) or
print ("Can't select entries from table php_blog.<br />" . $sql . "<br />" . mysql_error());
while ($row = mysql_fetch_array($result))
{
$username = $row["username"];
$email = $row["email"];
$id = $row["id"];
print "<table width=250 border=1 cellspacing=0 cellpadding=0>";
print "<tr>";
print "<td width=3>'$id'</td>";
print "<td width=247>'$username'</td>";
print "</tr>";
print "<tr>";
print "<td colspan=250>'$email'</td>";
print "</tr>";
print "</table><br><br>";
}
?>
<?php
$nextpage="$limit - 10";
$prevpage="$limit + 10";
print "<a href=users.php?limit=$prevpage>last 10 results</a>
<a href=users.php?limit=$nextpage>next 10 results</a>"
?>
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Aug 23, 2004 9:33 pm
remove the quotes from $nextpage and $prevpage.. otherwise you are creating a string "0 - 10" and "0 + 10" not calculating it.
davy2001
Forum Commoner
Posts: 37 Joined: Tue Feb 24, 2004 5:59 pm
Post
by davy2001 » Mon Aug 23, 2004 9:36 pm
THX that worked great! but one more small prob.
when it gets to the last page you can press next page and it goes back to number page 1
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Aug 23, 2004 9:43 pm
because you didn't add anything to know where the end is..
if you worked through the pagination threads like we suggested, you'd probably see how that's done better...
davy2001
Forum Commoner
Posts: 37 Joined: Tue Feb 24, 2004 5:59 pm
Post
by davy2001 » Mon Aug 23, 2004 9:44 pm
ohhhh ok, thx allot