PHP Calculation

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

Post Reply
davy2001
Forum Commoner
Posts: 37
Joined: Tue Feb 24, 2004 5:59 pm

PHP Calculation

Post 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
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

bout dis?

Post by neophyte »

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 »

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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$nextpage = $limit + 10;
$prevpage = $limit - 10

standard math..

add a comma before the 10 in your query string..

:roll:
davy2001
Forum Commoner
Posts: 37
Joined: Tue Feb 24, 2004 5:59 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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 »

how?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
davy2001
Forum Commoner
Posts: 37
Joined: Tue Feb 24, 2004 5:59 pm

Post 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 :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what's your code look like now?
davy2001
Forum Commoner
Posts: 37
Joined: Tue Feb 24, 2004 5:59 pm

Post 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>"
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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 »

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 :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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...
davy2001
Forum Commoner
Posts: 37
Joined: Tue Feb 24, 2004 5:59 pm

Post by davy2001 »

ohhhh ok, thx allot
Post Reply