Page 1 of 1
PHP Calculation
Posted: Mon Aug 23, 2004 8:49 pm
by davy2001
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
bout dis?
Posted: Mon Aug 23, 2004 8:55 pm
by neophyte
Code: Select all
$check = 50;
$nextpage=$check-10;
$nextpage=$check+10;
Posted: Mon Aug 23, 2004 8:57 pm
by davy2001
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
Posted: Mon Aug 23, 2004 9:02 pm
by feyd
$nextpage = $limit + 10;
$prevpage = $limit - 10
standard math..
add a comma before the 10 in your query string..

Posted: Mon Aug 23, 2004 9:03 pm
by davy2001
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
Posted: Mon Aug 23, 2004 9:04 pm
by feyd
you need to write a default value for $limit if it's not set....
Posted: Mon Aug 23, 2004 9:04 pm
by davy2001
how?
Posted: Mon Aug 23, 2004 9:11 pm
by feyd
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.
Posted: Mon Aug 23, 2004 9:14 pm
by davy2001
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

Posted: Mon Aug 23, 2004 9:30 pm
by feyd
what's your code look like now?
Posted: Mon Aug 23, 2004 9:31 pm
by davy2001
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>"
?>
Posted: Mon Aug 23, 2004 9:33 pm
by feyd
remove the quotes from $nextpage and $prevpage.. otherwise you are creating a string "0 - 10" and "0 + 10" not calculating it.
Posted: Mon Aug 23, 2004 9:36 pm
by davy2001
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

Posted: Mon Aug 23, 2004 9:43 pm
by feyd
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...
Posted: Mon Aug 23, 2004 9:44 pm
by davy2001
ohhhh ok, thx allot