spiritonline Posted March 12, 2009 Share Posted March 12, 2009 Hi, I'm really new to php so please forgive me my n00bness. My teacher helped me with a bit of php code for a website of mine. this code would make previous / next buttons function. It works by a scandir that scans the dictionary with all the files in it. all files are named index00.php index01.php etc. The problem: now this piece of code works 50% because -going from page 1 to page 2 will work fine -going back from page 2 to page one won't work. -Neither will it go from 2 to three, instead it will just stay at it's current page. (page2) Thanks a million in advance ! Cheers, Jason. The code the site can be seen at: http://www.hotels-and-suites.com/demo/index00.php <? $pages= array(scandir("/mnt/web4/42/79/52006579/htdocs/demo")); //echo "test" . count($pages)-2; if (!$_GET["counter"]) { $counter = $_GET["counter"]; } else { $counter = 0; } ?> <a href="index0<? echo $counter+1; ?>.php?counter=<? echo $counter; ?>"><img src="../images/next_07.png" border="0" align="right""" /></a><br /> <a href="index0<? echo $counter-1; ?>.php?counter=<? echo $counter; ?>"><img src="../images/previous_05.png" border="0" align="left""" /></a></div Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted March 12, 2009 Share Posted March 12, 2009 1.) Are you using different pages for each link? That really defeats the entire object of using pagination. 2.) Your logic is wrong where you define counter. You have it as if $_GET['counter'] is false(roughly the same as not set) then set counter to it's value. Try: if (isset($_GET["counter"])) { $counter = $_GET["counter"]; } else { $counter = 0; } 3.) You might like to check out this tutorial It's written using a database rather than for files, but the same principles still apply. Quote Link to comment Share on other sites More sharing options...
DjMikeS Posted March 12, 2009 Share Posted March 12, 2009 Let me explain this piece of code: if (!$_GET["counter"]) { $counter = $_GET["counter"]; } else { $counter = 0; } If $counter is not found in the url, I should try to set $counter to the variable I couldn't find. Else I should set $counter to 0... Or in short: remove the ! What he said.... Quote Link to comment Share on other sites More sharing options...
spiritonline Posted March 12, 2009 Author Share Posted March 12, 2009 Hi, many thanks for your suggestions ! I will try them out and keep you updated once more thanks ! I really appreciate it (PS: yes I use different pages for each time the previous or next button is pressed.) EDIT: Ok what i've done is the following: I replaced if (!$_GET["counter"]) { $counter = $_GET["counter"]; } else { $counter = 0; } for if (isset($_GET["counter"])) {     $counter = $_GET["counter"]; } else {     $counter = 0; } When I now view the page it will give me the following error: Parse error: syntax error, unexpected '&' in /mnt/web4/42/79/52006579/htdocs/demo/index00.php on line 14 On line 14 is:     $counter = $_GET["counter"]; you could see for yourself at: http://www.hotels-and-suites.com/demo/index00.php I hope some one can help me out thanks once more ! Quote Link to comment Share on other sites More sharing options...
DjMikeS Posted March 12, 2009 Share Posted March 12, 2009 You can't use the & like that because that is an operator in PHP... if (isset($_GET["counter"])) { $counter = $_GET["counter"]; } else { $counter = 0; } Quote Link to comment Share on other sites More sharing options...
spiritonline Posted March 12, 2009 Author Share Posted March 12, 2009 Hey thanks for taking time to help me out ! I've tried replacing it with your code however I end up with the problem I started with. Please reference to http://www.hotels-and-suites.com/demo/index00.php Quote Link to comment Share on other sites More sharing options...
spiritonline Posted March 12, 2009 Author Share Posted March 12, 2009 bump Quote Link to comment Share on other sites More sharing options...
spiritonline Posted March 18, 2009 Author Share Posted March 18, 2009 bump... please ? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted March 18, 2009 Share Posted March 18, 2009 Can you please re-explain what you're trying to do? Do you have a load of separate files that are named something like page0, page1, page2 etc? Or are you passing an ID to a page so you can select information from a database? Quote Link to comment Share on other sites More sharing options...
spiritonline Posted March 18, 2009 Author Share Posted March 18, 2009 The first, A load of pages named index00.php, index01.php etc thanks for ur reply !!! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted March 18, 2009 Share Posted March 18, 2009 That's really not a particularly good way to go about managing your pages, but never mind. In answer to the question, you need to be a little bit careful. Though it might be tempting to keep track of the numbers at the end from page-to-page, what happens if you make a mistake with your naming? How are you going to work out the highest number to go to? I think what i'd attempt to do is read the directory and look for all files that are in the form index*.php. You can then work out where in the array the current page is and whether or not to show next/previous links. The only caveat with this is that it's only going to work if you've named all your files using two characters for the number. Otherwise they wont sort in a natural order. If you haven't then you'll need to use the natsort function to get a natural ordering. And even in this case, you'll need to be a little careful - it'll sort index02 before index1 so you'll need to rid yourself of the preceding zeros. <?php //create an array of all index pages $pages = array(); foreach(glob('index*.php') as $page){ $pages[] = $page; } //get the current page, but we only want the file name without any slashes $currpage = $_SERVER['SCRIPT_NAME']; $parts = explode('/',$currpage); $currpage = $parts[count($parts)-1]; //find the index of the page $index = array_search($currpage,$pages); if($index > 0){//previous exists $prevpage = $pages[$index-1]; //show previous link echo "Previous page:".$prevpage; } if($index < count($pages) -2){//next exists $nextpage = $pages[$index+1]; //show next link echo "Next page:".$nextpage; } ?> Quote Link to comment Share on other sites More sharing options...
spiritonline Posted March 20, 2009 Author Share Posted March 20, 2009 Hi there, thanks a ton for your replies ! I'm sorry i didn't respond earlier but I saw your post only just now In reply to what you've written, I've tried to put in your script but I still end up with the same errors. ( I also tried re-naming the files from 00,01, 02 etc to 0,1,2 etc) Would you have any clues ? Once more thanks a billion ! (ps: you can reffrence to hotels-and-suites.com source) thanks ! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted March 20, 2009 Share Posted March 20, 2009 Can you post the code you're actually using? Quote Link to comment Share on other sites More sharing options...
spiritonline Posted March 21, 2009 Author Share Posted March 21, 2009 Sure, here's the whole page: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- TemplateBeginEditable name="doctitle" --> <title>offerte voor: hotel naam hier</title> <?php $pages= array(scandir("/mnt/web4/42/79/52006579/htdocs/demo")); //echo "test" . count($pages)-2; if (@$_GET["counter"]) { $counter = $_GET["counter"]; } else { $counter = 0; } //create an array of all index pages $pages = array(); foreach(glob('index*.php') as $page){ $pages[] = $page; } //get the current page, but we only want the file name without any slashes $currpage = $_SERVER['SCRIPT_NAME']; $parts = explode('/',$currpage); $currpage = $parts[count($parts)-1]; //find the index of the page $index = array_search($currpage,$pages); if($index > 0){//previous exists $prevpage = $pages[$index-1]; //show previous link echo "Previous page:".$prevpage; } if($index < count($pages) -2){//next exists $nextpage = $pages[$index+1]; //show next link echo "Next page:".$nextpage; } ?> <!-- TemplateEndEditable --> <!-- TemplateBeginEditable name="head" --> <!-- TemplateEndEditable --> <style type="text/css"> <!-- body { margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; background-image: url(../images/background-gradient_02.png); background-repeat: repeat-x; } body,td,th { font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #333; } #menu { margin: auto; height: 81px; width: auto; background-image: url(../images/header_02.png); padding-top: 30px; } #wrapper { width: 1024px; margin: auto; } #footer { background-image: url(../images/footer_03.png); height: 93px; } #header { background-image: url(../images/header_01.png); height: 125px; } #content { background-image: url(../images/images/content00_02.png); height: 582px; } #tekst { height: 60px; width: 200px; margin-top: 103px; margin-right: auto; margin-bottom: auto; margin-left: auto; text-align: center; font-size: 16px; } a:link { text-decoration: none; } a:visited { text-decoration: none; } a:hover { text-decoration: none; } a:active { text-decoration: none; } --> </style> </head> <body> <div id="menu"> <a href="index0<? echo $counter+1; ?>.php?counter=<? echo $counter; ?>"><img src="../images/next_07.png" border="0" align="right""" /></a><br /> <a href="index0<? echo $counter-1; ?>.php?counter=<? echo $counter; ?>"><img src="../images/previous_05.png" border="0" align="left""" /></a><br /> </div> <div id="wrapper"> <div id="header"></div> <div id="content"> <div id="tekst"><br /> Naam hotel hier<br /> </div> </div> <div id="footer"></div> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
spiritonline Posted March 21, 2009 Author Share Posted March 21, 2009 Or just the PHP code: <?php $pages= array(scandir("/mnt/web4/42/79/52006579/htdocs/demo")); //echo "test" . count($pages)-2; if (@$_GET["counter"]) { $counter = $_GET["counter"]; } else { $counter = 0; } //create an array of all index pages $pages = array(); foreach(glob('index*.php') as $page){ $pages[] = $page; } //get the current page, but we only want the file name without any slashes $currpage = $_SERVER['SCRIPT_NAME']; $parts = explode('/',$currpage); $currpage = $parts[count($parts)-1]; //find the index of the page $index = array_search($currpage,$pages); if($index > 0){//previous exists $prevpage = $pages[$index-1]; //show previous link echo "Previous page:".$prevpage; } if($index < count($pages) -2){//next exists $nextpage = $pages[$index+1]; //show next link echo "Next page:".$nextpage; } ?> <a href="index0<? echo $counter+1; ?>.php?counter=<? echo $counter; ?>"><img src="../images/next_07.png" border="0" align="right""" /></a><br /> <a href="index0<? echo $counter-1; ?>.php?counter=<? echo $counter; ?>"><img src="../images/previous_05.png" border="0" align="left""" /></a><br /> (PS: how come I can't edit my posts anymore after a while ?) Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted March 21, 2009 Share Posted March 21, 2009 I think you missed the point of the code i posted slightly. You're use of variables passed in the URL are redundant, seeing as you're actually redirecting to separate pages. The next/previous text that my code outputs are the pages you need to link to. If you look at your page, you'll see text at the top "Next page:index01.php " - that's what you need to link to. (PS: how come I can't edit my posts anymore after a while ?) There's a time limit on the edit in order to prevent an otherwise inevitable complete communication breakdown. 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.