Jump to content

simple pagination question: Displaying 1-3 of 5 results


xfezz

Recommended Posts

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.

[code]
$min_num = $offset+ 1;
$max_num = ($min_num + $max_results)-1;
[/code]

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.
Link to comment
Share on other sites

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.

[url=http://www.amazon.com/s/ref=nb_ss_gw/102-6043809-7736157?url=search-alias%3Dstripbooks&field-keywords=php&Go.x=0&Go.y=0&Go=Go]http://www.amazon.com/s/ref=nb_ss_gw/102-6043809-7736157?url=search-alias%3Dstripbooks&field-keywords=php&Go.x=0&Go.y=0&Go=Go[/url]

I think that will work
Link to comment
Share on other sites

[code]
$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";
[/code]
Link to comment
Share on other sites

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

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

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

edit: changed the above to this. and I still get the error
[code]
$sql = "SELECT * FROM articles ORDER BY id DESC LIMIT $from, $max_results";
$getlist = mysql_query($sql, $conn) or die(mysql_error());
[/code]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

I connect to the database with the following

[code]
//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");
[/code]

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
[code]
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'];
}
[/code]

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

Link to comment
Share on other sites

[quote author=Crayon Violent link=topic=113200.msg459859#msg459859 date=1162201226]
[code]
$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)){

[/code]
[/quote]
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.