FlyingIsFun1217 Posted June 10, 2008 Share Posted June 10, 2008 Hey! I'm trying to do an interesting page thing so that I can do multiple pages for listings. Here's my current code: $pageNumber = $_GET['page']; $parsedNumber = 1; if($pageNumber == '') { $pageNumber = 1; } foreach(glob('*.jpg') as $picFileJPG) { while((($pageNumber*20)-19)<= $parsedNumber && ($pageNumber*20)>= $parsedNumber) { $simplePicName = substr_replace($picFileJPG,'',-4); if($tableRowNumber == 1) { echo '<td>'; echo '<table>'; } echo '<tr>'; echo '<td>'; echo '<a href="'.$baseURL.'viewPic.php?pID='.$picFileJPG.'">'.$simplePicName.'</a>'; echo '</td>'; echo '</tr>'; if($tableRowNumber == 10) { echo '</table>'; echo '</td>'; $tableRowNumber = 1; } else { $tableRowNumber++; } } $parsedNumber++; } Now, I'm trying to get it to do the following: On page 1: List first 20 results (result 1-21) On page 2: List second 20 results (result 21-40) On page 3: List third 20 results (result 41-60) But right now, it seems to be cycling through printing the first result OVER AND OVER AND OVER. What am I missing here? I'm incrementing $parsedResult every time... FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/109502-solved-tryin-a-little-trick/ Share on other sites More sharing options...
hansford Posted June 10, 2008 Share Posted June 10, 2008 looks like $tableRowNumber never gets set before entering the loop Quote Link to comment https://forums.phpfreaks.com/topic/109502-solved-tryin-a-little-trick/#findComment-561699 Share on other sites More sharing options...
FlyingIsFun1217 Posted June 10, 2008 Author Share Posted June 10, 2008 Thats a previous variable; Here is the full source file: <?php include("header.php"); include("password.php"); include("baseURL.php"); $cookiePassword = $_COOKIE['pMD5']; $pageNumber = $_GET['page']; $parsedNumber = 1; if($pageNumber == '') { $pageNumber = 1; } if(!$cookiePassword) { echo '<script type="text/javascript">'; echo 'window.location = "'.$baseURL.'login.php"'; echo '</script>'; } if($cookiePassword != $encryptedPass) { setcookie("pMD5", "", time()-1); echo '<script type="text/javascript">'; echo 'window.location = "'.$baseURL.'error.php"'; echo '</script>'; } else { // For tracking the number of rows that have been filled in any certain table $tableRowNumber = 1; echo '<center>'; //=================================================================================================// echo '<table>'; echo '<tr>'; foreach(glob('*.jpg') as $picFileJPG) { while((($pageNumber*20)-19)<= $parsedNumber && ($pageNumber*20)>= $parsedNumber) { $simplePicName = substr_replace($picFileJPG,'',-4); if($tableRowNumber == 1) { echo '<td>'; echo '<table>'; } echo '<tr>'; echo '<td>'; echo '<a href="'.$baseURL.'viewPic.php?pID='.$picFileJPG.'">'.$simplePicName.'</a>'; echo '</td>'; echo '</tr>'; if($tableRowNumber == 10) { echo '</table>'; echo '</td>'; $tableRowNumber = 1; } else { $tableRowNumber++; } } $parsedNumber++; } echo '</table>'; echo '</td>'; echo '</tr>'; echo '</table>'; //=====================================================================================================// echo '</center>'; } include("footer.php"); ?> FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/109502-solved-tryin-a-little-trick/#findComment-561703 Share on other sites More sharing options...
hansford Posted June 10, 2008 Share Posted June 10, 2008 if this statement runs then $tableRowNumber never gets set. ($cookiePassword != $encryptedPass) { Quote Link to comment https://forums.phpfreaks.com/topic/109502-solved-tryin-a-little-trick/#findComment-561711 Share on other sites More sharing options...
FlyingIsFun1217 Posted June 10, 2008 Author Share Posted June 10, 2008 Ok, let me refine the parts of my problem. Once I add the following, it starts listing the same thing over and over: $pageNumber = $_GET['page']; $parsedNumber = 1; if($pageNumber == '') { $pageNumber = 1; } while((($pageNumber*20)-19)<= $parsedNumber && ($pageNumber*20)>= $parsedNumber) { // Here's where it does stuff that you guys don't need to worry about, since it works } $parsedNumber++; Do not worry about the rest of it; I've tested it, and I can guarantee that it works the way I want it to. FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/109502-solved-tryin-a-little-trick/#findComment-561716 Share on other sites More sharing options...
hansford Posted June 10, 2008 Share Posted June 10, 2008 $parsedNumber++ is outside of the while loop; Quote Link to comment https://forums.phpfreaks.com/topic/109502-solved-tryin-a-little-trick/#findComment-561726 Share on other sites More sharing options...
FlyingIsFun1217 Posted June 10, 2008 Author Share Posted June 10, 2008 Shouldn't it be though? Lets say the user enters "site.php?page=2". In this case, it would never show anything, because it would never cycle up to the number needed to start echoing the results. Basically, it needs to increment every time it does a foreach loop. Thanks! FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/109502-solved-tryin-a-little-trick/#findComment-561982 Share on other sites More sharing options...
hansford Posted June 10, 2008 Share Posted June 10, 2008 The problem with that is: say $pageNumber = 1 and $parsedNumber = 1 neither of them change when they are in the while loop, so the condition is always true, so the loop never ends. while((($pageNumber*20)-19)<= $parsedNumber && ($pageNumber*20)>= $parsedNumber) Quote Link to comment https://forums.phpfreaks.com/topic/109502-solved-tryin-a-little-trick/#findComment-562063 Share on other sites More sharing options...
FlyingIsFun1217 Posted June 10, 2008 Author Share Posted June 10, 2008 It still loops through and returns the first result over and over, so it's obviously not incrementing $parsedNumber... FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/109502-solved-tryin-a-little-trick/#findComment-562242 Share on other sites More sharing options...
hansford Posted June 10, 2008 Share Posted June 10, 2008 you are correct. $parsedNumber never gets incremented so the while loop goes on forever. $parsedNumber++ has to be incremented in the while loop in order to ever exit the while loop. Quote Link to comment https://forums.phpfreaks.com/topic/109502-solved-tryin-a-little-trick/#findComment-562259 Share on other sites More sharing options...
FlyingIsFun1217 Posted June 10, 2008 Author Share Posted June 10, 2008 Apparently, putting it within the while loop did work (must have had it out of the loop before), but I still only get the first result repeated... FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/109502-solved-tryin-a-little-trick/#findComment-562302 Share on other sites More sharing options...
FlyingIsFun1217 Posted June 10, 2008 Author Share Posted June 10, 2008 Duh... it was a while loop, not a for loop. FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/109502-solved-tryin-a-little-trick/#findComment-562322 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.