|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
prev next link headache
Hi,
I am trying to do a prev next link using php. But i seem to be having a problem and i dont know where went wrong. It would be nice if you guys could comment about it. Please bear with the code below cause i am very new at this. TIA Braendan <?php // // Connects to sql server and logs in with username and password and select the required database // $connect=@mysql_connect("localhost","username","password") or die ("couldnt connect to sql server"); $db= @mysql_select_db("test") or die ("couldnt select database"); // // This is where you choose the ammount you want to display in one page // $per_page = 5; // // Selects all of the data from database // $sql_text = ("SELECT * from pdf ORDER BY id DESC"); // // Sets page number, if no page is specified, it will create page 1 // if(!$page) { $page = 1; } $prev_page = $page - 10; $next_page = $page + 10; $query = @mysql_query($sql_text); // Sets up specified page $page_start = ($per_page * $page) - $per_page; $num_rows = @mysql_num_rows($query); if($num_rows <= $per_page) { $num_pages = 1; } else if (($num_rows % $per_page) == 0) { $num_pages = ($num_rows / $per_page); } else { $num_pages = ($num_rows / $per_page) + 1; } $num_pages = (int) $num_pages; if (($page > $num_pages) || ($page < 0)) { error("You have specified an invalid page number"); } $sql_text = $sql_text . " LIMIT $page_start, $per_page"; $query = mysql_query($sql_text); ?> <title>Output here</title> <font face="Tahoma" size="2"> There are currently<font color="red"> <?php echo "$num_rows"; ?> </font> files in this section<p></font> <?php while ($result = mysql_fetch_array($query)) { echo "<p><font face="Tahoma" size="2">Document ID:".$result['id']."</font><br>n"; echo "<font face="Tahoma" size="2">Title:".$result['title']."</font><br>n"; echo "<font face="Tahoma" size="2">File:</font><a href="$file" target="_blank"><font face="Tahoma" size="2">".$result['file']."</font></a></p>n"; } // This displays the "Previous" link if ($prev_page) { echo "<a href="$PHP_SELF?page=$prev_page">< Prev</a>"; } // This loops the Pages and displays individual links = for ($i = 1; $i <= $num_pages; $i++) { if ($i != $page) { echo " <a href=$PHP_SELF?page=$i>$i</a>"; } else { echo " $i "; } } // This displays the "Next" link. if ($page != $num_pages) { echo " |<a href="$PHP_SELF?page=$next_page"> Next ></a>" ; } ?> </body> </html> |
|
#2
|
|||
|
|||
|
RE: prev next link headache
$prev_page = $page - 1;
$next_page = $page + 1; Hi sorry bout that please ommit the prev post on these to line with the new one above. TIA, Braendan |
|
#3
|
|||
|
|||
|
RE: prev next link headache
Looks fine, what are the error msgs that your getting?
|
|
#4
|
|||||
|
|||||
|
RE: prev next link headache
Hey, I'm getting prev and next links trouble too, basicly its not showing the next 10 in the row, Its probably something so simple, but I just can't see the problem
php Code:
|
|
#5
|
||||||||
|
||||||||
|
RE: prev next link headache
Russ -> it looks like the problem is in your if($_GET['action'] == 'sortby') statments. The first thing I noticed is that you never do anything with:
Ok, I see what your trying to do. replace: php Code:
with unique variable names. Also, change: to" php Code:
Hope that helps. |
|
#6
|
|||
|
|||
|
RE: RE: prev next link headache
|
|
#7
|
|||||
|
|||||
|
RE: prev next link headache
Code with comments (I reformatted a bit, sorry):
php Code:
Cheers, Keith. |
|
#8
|
||||||||
|
||||||||
|
RE: prev next link headache
Thanks man for your help, actually with your extra comments on the side it helped me understand php alot more, I found the problem in the end, basicly i changed this
php Code:
to this php Code:
Altho I do get Prev. and Next Links on everypage. but as I got that kinda worked out, can i get rid of this section of code? |
|
#9
|
|||||
|
|||||
|
RE: prev next link headache
More code with a few tweaks...
php Code:
I think the next prev links will work, but what happens when $start=95 out of 100 records and you go $start+10 and run the query for the next batch? Also, based on the partial code you wrote, I would keep the $_GET['start'] check in, but move it to the top like I did in this example, or you risk the query bombing if start wasn't set. Mind you, with that logic, you should also check for $_GET['char'] and default it as well if someone removes it from the URL. Or display an error message if it isn't passed in, etc. Test your forward previous links all the way to the end of your recordset (and make sure the total number of records isn't a multiple of 10. If it works, the all is good I think I left checking the start and end of the recordset out of my code...but I am too tired to look right now Cheers, Keith |
|
#10
|
|||
|
|||
|
RE: prev next link headache
Thank you man, it works now, but I still get prev and next on everypage, but thats a minor fault. thanks again
|
|
#11
|
|||
|
|||
|
RE: prev next link headache
okay, I worked out why I have a prev and next on each page, it seems that it counts how many rows there are in the table instead of counting just the rows being display, ie, if I display just the rows starting with A, it shows just the A's, but it counts all the rows, any way around this? also how can I have a displaying 5 of 19 reviews or something like that?
|
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > prev next link headache |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|