Jump to content


Photo

simple pagination question: Displaying 1-3 of 5 results


  • Please log in to reply
10 replies to this topic

#1 xfezz

xfezz
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 30 October 2006 - 08:17 AM

Im sure ill be kicking myself for asking this.  I know it has to do with the offset, but I cant quite put my finger on it on how to code it.
I came up with this, I know its not correct since it doesnt give back the right results all the time.

$min_num = $offset+ 1;
$max_num = ($min_num + $max_results)-1;

where max_results is the limit to how many results per page.
max_num doesnt display correctly if the total entries in the database is an odd number. but min_num displays the correct value each and every time.

#2 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,430 posts
  • LocationBelgium

Posted 30 October 2006 - 08:31 AM

oh ok, i will look into the problem and post the correct code

#3 xfezz

xfezz
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 30 October 2006 - 08:42 AM

Thanks for the reply I probably should of been more clear. Im not looking to display the number of pages, but the number of entries in the database for the corresponding page that the user is currently on. Pretty much like how amazon.com has their results set up.

http://www.amazon.co...=0&Go.y=0&Go=Go

I think that will work

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 30 October 2006 - 08:54 AM

you mean the "Showing 1 - 12 of 11784 Results" part?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 xfezz

xfezz
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 30 October 2006 - 08:58 AM

you mean the "Showing 1 - 12 of 11784 Results" part?


yep

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 30 October 2006 - 09:04 AM

$sql = "select * from table order by $sortby limit $from, $max_results";
$getlist = mysql_query($sql, $conn) or die(mysql_error());

$sql = "select count(*) as num from table";
$getcount = mysql_query($sql, $conn) or die(mysql_error());
$total_results = mysql_result($getcount, 0) or die(mysql_error());

$fr = $from + 1;
$to = $from + mysql_num_rows($getlist);

echo "showing $fr - $to of $total_results results";

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 xfezz

xfezz
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 30 October 2006 - 09:14 AM

hmm i get an error on line 40 of my code. which is the $getlist = mysql_query($sql, $conn) or die(mysql_error()); statement

$sql = mysql_query("SELECT * FROM articles ORDER BY id DESC LIMIT $from, $max_results");
$getlist = mysql_query($sql, $conn) or die(mysql_error());

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource

edit: changed the above to this. and I still get the error
$sql = "SELECT * FROM articles ORDER BY id DESC LIMIT $from, $max_results";
$getlist = mysql_query($sql, $conn) or die(mysql_error());


#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 30 October 2006 - 09:19 AM

how did you connect to the database? my code assumes the following:

$conn = mysql_connect('localhost','username','password') or die(mysql_error());

and then the $getlist query uses the optional 2nd argument to specify the connection.  Either change $conn to your connection var or if you did assign the connection to a var, simply remove the $conn argument from the mysql_query:

$getlist = mysql_query($sql) or die(mysql_error());

edit: also, you need to re-look at my code.  I have the query seperated from the function call. The way you listed it, you have the query string combined with the function call, which returns and assigns the result source to $sql.  Then you turn around and try to use that result source in your $getlist query function call, instead of a query string.

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#9 xfezz

xfezz
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 30 October 2006 - 09:34 AM

I connect to the database with the following

//set server and database parameters
    $user_name = "root";
    $password = "some really cool password";
    $database = "article_db";
    $server = "localhost";
	
//make connection to database    
    $db_handle = mysql_connect($server, $user_name, $password)or die("cannot connect to server");
    $db_found = mysql_select_db($database, $db_handle)or die("cannot select database"); 

I removed the $conn from the query and now it doesnt like my while loop.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Apache\htdocs\port\admin_article_selec.php on line 43
while($row = mysql_fetch_array($sql)){
    // Build your formatted results here.
	$now_format = $row['article_date'];
	$new_format = date('F d, Y',strtotime($now_format)); // transform the date
	echo "<div id=\"article_date\">". $new_format ."</div> \n";  // prints out: date in following format October 20, 2006
	echo "<div id=\"article_chk\">"."<input type=\"checkbox\" name=\"del_select[]\" value=\"$row[id]\" />"."</div>"."<br>\n";
	$row['message']=nl2br($row['message']);
	echo "<div id=\"article_body\">" .$row['message'] ."</div>\n";
    echo $row['title'];
}

Fix one thing and then cause another problem.  8) ill look more into it tomorrow. There may be something im missing. As for now im off for bed. thanks for the help



#10 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 30 October 2006 - 09:40 AM

$sql = "SELECT * FROM articles ORDER BY id DESC LIMIT $from, $max_results";
$getlist = mysql_query($sql, $db_handle) or die(mysql_error());
.
.
while($row = mysql_fetch_array($getlist)){


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#11 xfezz

xfezz
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 30 October 2006 - 06:06 PM

$sql = "SELECT * FROM articles ORDER BY id DESC LIMIT $from, $max_results";
$getlist = mysql_query($sql, $db_handle) or die(mysql_error());
.
.
while($row = mysql_fetch_array($getlist)){

Yep I woke up and that was the first thing I changed. I suppose I was too tired to catch that early this morning. Its working now. Thanks a bunch




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users