RealDrift Posted December 6, 2007 Author Share Posted December 6, 2007 lol i changed the db name from i_horses to my one i added WHERE Sender='$username' to the second SQL query as it gave me the wrong info now it is giving right info like so: page1=10 rows page2=10 rows page3=7 rows Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 6, 2007 Share Posted December 6, 2007 thats what u did, didn't you? I don't even know anymore...my brain went to poo from all this thinking Did the code work for you? Quote Link to comment Share on other sites More sharing options...
RealDrift Posted December 6, 2007 Author Share Posted December 6, 2007 Page1= SELECT * FROM goldies WHERE Sender='WeedHunteR' LIMIT 0,10 Page2= SELECT * FROM goldies WHERE Sender='WeedHunteR' LIMIT 7,10 Page3= SELECT * FROM goldies WHERE Sender='WeedHunteR' LIMIT 14,10 thought i might tell u the sql queries the script echoes on each page Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 6, 2007 Share Posted December 6, 2007 OMG, I gave you the wrong code now! hold on... Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 6, 2007 Share Posted December 6, 2007 Okay, hopefully this is right now. <?php $username = $_SESSION['username']; $limit = 10; $query_count = "SELECT count(*) FROM goldies WHERE Sender='$username'"; $result_count = mysql_query($query_count) or die("Error: " . mysql_error()); $totalrows = mysql_result($result_count, 0, 0); $numofpages = ceil($totalrows/$limit); $rowsperpage = ceil($totalrows/$numofpages); if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; $offset = ($page - 1) * $rowsperpage; $query = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $offset,10"; echo $query .'<br>'; $result = mysql_query($query) or die("Error: " . mysql_error()); //Exit if no records to display if (mysql_num_rows($result) == 0) { exit("Nothing to Display!"); } //Create the table of output data echo "<table>\n"; while ($row = mysql_fetch_array($result)) { //Alternate the row background color $bgcolor = ($bgcolor == "#737373")?'#737373':'#737373'; //Display the row of data echo "<tr bgcolor=\"$bgcolor\">\n"; echo " <td>".$row["Receiver"]."</td>\n"; echo " <td>".$row["Amount"]."</td>\n"; echo " <td>".$row["Date"]."</td>\n"; echo "</tr>"; } echo "</table>\n"; //Enable the Prev link if not first page if ($page > 1) { echo("<a href=\"creditshistory.php?page=".($page-1)."\">PREV</a> "); } else { echo("PREV "); } //Create links for each page in report for ($i=1; $i<=$numofpages; $i++) { if ($i == $page) { echo "$i "; } else { echo "<a href=\"creditshistory.php?page=$i\">$i</a> "; } } //Enable the Next link if not last page if ($page < $numofpages) { echo("<a href=\"creditshistory.php?page=".($page+1)."\">NEXT</a>"); } else { echo("NEXT"); } ?> Quote Link to comment Share on other sites More sharing options...
RealDrift Posted December 6, 2007 Author Share Posted December 6, 2007 results: page1=10 rows page2=10 rows page3=7 rows Page1= SELECT * FROM goldies WHERE Sender='WeedHunteR' LIMIT 0,10 Page2= SELECT * FROM goldies WHERE Sender='WeedHunteR' LIMIT 7,10 Page3= SELECT * FROM goldies WHERE Sender='WeedHunteR' LIMIT 14,10 Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 6, 2007 Share Posted December 6, 2007 -shakes head- Try this <?php $username = $_SESSION['username']; $limit = 10; $query_count = "SELECT count(*) FROM goldies WHERE Sender='$username'"; $result_count = mysql_query($query_count) or die("Error: " . mysql_error()); $totalrows = mysql_result($result_count, 0, 0); $numofpages = ceil($totalrows/$limit); $rowsperpage = $totalrows/$numofpages; if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; $offset = ($page - 1) * $rowsperpage; $query = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $offset,10"; echo $query .'<br>'; $result = mysql_query($query) or die("Error: " . mysql_error()); //Exit if no records to display if (mysql_num_rows($result) == 0) { exit("Nothing to Display!"); } //Create the table of output data echo "<table>\n"; while ($row = mysql_fetch_array($result)) { //Alternate the row background color $bgcolor = ($bgcolor == "#737373")?'#737373':'#737373'; //Display the row of data echo "<tr bgcolor=\"$bgcolor\">\n"; echo " <td>".$row["Receiver"]."</td>\n"; echo " <td>".$row["Amount"]."</td>\n"; echo " <td>".$row["Date"]."</td>\n"; echo "</tr>"; } echo "</table>\n"; //Enable the Prev link if not first page if ($page > 1) { echo("<a href=\"creditshistory.php?page=".($page-1)."\">PREV</a> "); } else { echo("PREV "); } //Create links for each page in report for ($i=1; $i<=$numofpages; $i++) { if ($i == $page) { echo "$i "; } else { echo "<a href=\"creditshistory.php?page=$i\">$i</a> "; } } //Enable the Next link if not last page if ($page < $numofpages) { echo("<a href=\"creditshistory.php?page=".($page+1)."\">NEXT</a>"); } else { echo("NEXT"); } ?> Quote Link to comment Share on other sites More sharing options...
RealDrift Posted December 6, 2007 Author Share Posted December 6, 2007 same results poco Quote Link to comment Share on other sites More sharing options...
RealDrift Posted December 6, 2007 Author Share Posted December 6, 2007 feeling tired poco, back hurting its 3am here, falling asleep on pc Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 6, 2007 Share Posted December 6, 2007 Okay...this is like my last hope <?php $username = $_SESSION['username']; $limit = 10; $query_count = "SELECT count(*) FROM goldies WHERE Sender='$username'"; $result_count = mysql_query($query_count) or die("Error: " . mysql_error()); $totalrows = mysql_result($result_count, 0, 0); $numofpages = ceil($totalrows/$limit); if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; $offset = ($page - 1) * $limit; $query = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $offset,10"; echo $query .'<br>'; $result = mysql_query($query) or die("Error: " . mysql_error()); //Exit if no records to display if (mysql_num_rows($result) == 0) { exit("Nothing to Display!"); } //Create the table of output data echo "<table>\n"; while ($row = mysql_fetch_array($result)) { //Alternate the row background color $bgcolor = ($bgcolor == "#737373")?'#737373':'#737373'; //Display the row of data echo "<tr bgcolor=\"$bgcolor\">\n"; echo " <td>".$row["Receiver"]."</td>\n"; echo " <td>".$row["Amount"]."</td>\n"; echo " <td>".$row["Date"]."</td>\n"; echo "</tr>"; } echo "</table>\n"; //Enable the Prev link if not first page if ($page > 1) { echo("<a href=\"creditshistory.php?page=".($page-1)."\">PREV</a> "); } else { echo("PREV "); } //Create links for each page in report for ($i=1; $i<=$numofpages; $i++) { if ($i == $page) { echo "$i "; } else { echo "<a href=\"creditshistory.php?page=$i\">$i</a> "; } } //Enable the Next link if not last page if ($page < $numofpages) { echo("<a href=\"creditshistory.php?page=".($page+1)."\">NEXT</a>"); } else { echo("NEXT"); } ?> Quote Link to comment Share on other sites More sharing options...
RealDrift Posted December 6, 2007 Author Share Posted December 6, 2007 Poco...it....works.... so sleepy, thanks a lot for this man. You won't hate me for putting you through this will you? also is this script stable? it wont fuk up onc ei move my site to the real server will it? Quote Link to comment Share on other sites More sharing options...
RealDrift Posted December 6, 2007 Author Share Posted December 6, 2007 also if i change $limit to something like 15 or so it wll display 15 rows per page right or i gotta change sutting else? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 6, 2007 Share Posted December 6, 2007 Poco...it....works.... so sleepy, thanks a lot for this man. You won't hate me for putting you through this will you? also is this script stable? it wont fuk up onc ei move my site to the real server will it? Finally! It should be fine now. Na, I don't hate you for it. If anything, it made me better. Now I could probably code pagination without looking at any outside resources...blindfolded xP hah. Don't forget to press "topic solved". It should feel good to press it after 100+ posts. And yes, you should be able to change limit to anything you want and it will work. Quote Link to comment Share on other sites More sharing options...
RealDrift Posted December 6, 2007 Author Share Posted December 6, 2007 consider this topic closed! thanks again to Poco and everyone else who helped. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 6, 2007 Share Posted December 6, 2007 No problem. Now you can see why I was so hesitant to help in the beginning. Pagination is like my pet peeve. I guess now it won't be Well...goodnight. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 6, 2007 Share Posted December 6, 2007 Wow, imagine my surprise to come into work and see this post going to 8 pages! Anyway, I looked over that final code and changing the $limit variable will NOT increase the number of records displayed on a page because there is a bug in the code: [code]$offset = ($page - 1) * $limit; $query = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $offset,10"; Change the "10" at the end of that query to "$limit" Also, there will be errors if the page variable on the query is bad. You should always validate the data. Change this: if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; To this {code]$page = (isset($_GET['page']) && is_int($_GET['page']))?$_GET['page']:1;[/code] Lastly, if your results start becoming very large you will want to create some code to limit the amount of page numbers that are displayed. But that is another problem to be tackled later. Quote Link to comment Share on other sites More sharing options...
RealDrift Posted December 10, 2007 Author Share Posted December 10, 2007 damato i changed what usaid and after i changed 10 to '$limit' i got following error: Error: 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 Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 10, 2007 Share Posted December 10, 2007 Did you change this line $query = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $offset,10"; To $query = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $offset, $limit"; Quote Link to comment Share on other sites More sharing options...
RealDrift Posted December 10, 2007 Author Share Posted December 10, 2007 Did you change this line $query = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $offset,10"; To $query = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $offset, $limit"; yeh that fixed it but now pages dnt change, not again Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 10, 2007 Share Posted December 10, 2007 But if you change $limit to "10" it works? That doesn't make sense... Quote Link to comment Share on other sites More sharing options...
RealDrift Posted December 10, 2007 Author Share Posted December 10, 2007 [code][quote author=pocobueno1388 link=topic=170599.msg758980#msg758980 date=1197321223] But if you change $limit to "10" it works? That doesn't make sense... [/quote] no lol. the changing 10 to $limit is fine. it changes the amount of rows displayed to wahtever $limit is set to but damato told me to: change: Also, there will be errors if the page variable on the query is bad. You should always validate the data. if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; To this: due to somethign abotu validating your stuff. {code]$page = (isset($_GET['page']) && is_int($_GET['page']))?$_GET['page']:1; the $limit thing works, but the pages dnt change now due to what i changed above. [/code] Quote Link to comment Share on other sites More sharing options...
RealDrift Posted December 10, 2007 Author Share Posted December 10, 2007 oh god damato has put us in another problem lol i was so happy that this script was working Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 10, 2007 Share Posted December 10, 2007 Ah, my error. You can't use is_int() on a GET variable since they are all considered as strings. Instead you should declare the value as an int - if it isn't an int is will be converted to one. So, something like 4.5 will be set to the int 4. A non number value will be converted to the int 0. You can then ensure the value is greater or equal to 1. This will ensure that page is always a whole number that is greater than or equal to 1. $page = ((int)$_GET['page']<1)?1:(int)$_GET['page']; I have tested this Quote Link to comment Share on other sites More sharing options...
RealDrift Posted December 10, 2007 Author Share Posted December 10, 2007 works like a charm thanks damato btw any technical names for the function where it hides lots of page numbers? Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 11, 2007 Share Posted December 11, 2007 works like a charm thanks damato btw any technical names for the function where it hides lots of page numbers? Not that I know of. You just need to decide what the rules are and build it. Let's say you want to show up to 5 pages in front and 5 pages in back; as well as the first and last pages. (that is the same as is used on this site). <?php //Enable the Prev link if not first page if ($page > 1) { echo("<a href=\"creditshistory.php?page=".($page-1)."\">PREV</a> "); } else { echo("PREV "); } //Number of pages to show front/back $pageSpan = 5; //Determine first and last pages to show $first = ($page>($pageSpan+1))?($page-$pageSpan):2; $last = ($page<($numofpages-$pageSpan))?($page+$pageSpan)$numofpages-1); //Show first page echo "<a href=\"creditshistory.php?page=1\">1</a> \n"; //Show ellipses if next page is not 2 if ($first!=2) { echo "... \n"; } //Create links for each page in report for ($i=$first; $i<=$last; $i++) { if ($i == $page) { echo "[$i] "; } else { echo "<a href=\"creditshistory.php?page=$i\">$i</a> "; } } //Show ellipses if next page is not last page if ($last!=($numofpages-1)) { echo "... \n"; } //Show the last page echo "<a href=\"creditshistory.php?page=$numofpages\">$numofpages</a> \n"; //Enable the Next link if not last page if ($page < $numofpages) { echo("<a href=\"creditshistory.php?page=".($page+1)."\">NEXT</a>"); } else { echo("NEXT"); } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.