SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast fLUx1337's Avatar
    Join Date
    Jul 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Whats wrong with this code....? while()

    Hi,

    I'm currently writing a script, which needs to do this...

    Echo all the names on character 1 (a), page 1 > move to page 2, do the same, then keep doing that for 50 pages

    Then when its echo'ed all the names off all the pages starting with "a", move onto "b", do the same as before, echo all the names, for all the pages

    The do the same for c,d,e,f,g....etc..

    This is my code (its edited so it doesnt take forever to test! ):

    PHP Code:
    <?php
    $char 
    97;
    $page 1;
    $no=0;

    while(
    $char <= 100){
    $pageno 50;

        while(
    $page <= $pageno){
        
    $result 50;
                
                while(
    $no <= $result){
                if(
    $no == $result){
                
    $page++;
                break;
                }else{
                
    // heres where the normal code to echo each name would go
                
    echo'Done #'.$no.' for letter '.chr($char).'<br>';
                
    $no++;
                }
                
        if(
    $page == $pageno){
        echo
    'Done All the pages for '.chr($char).', moving onto '.chr($char+1).'<br>';
        
    $char++;
        
    $page=1;
        
                }
            }
        }
    }
    ?>
    Now, whats wrong with it? It does "a" page 1 fine, but never moves on to page 2, etc....

    On the code above, you are meant to get a list of text like this:

    Done #0 for letter a
    Done #1 for letter a
    Done #2 for letter a
    Done #3 for letter a
    ...#4-47...
    Done #48 for letter a
    Done #49 for letter a
    Done #50 for letter a
    Done All the pages for a, moving onto b
    Done #0 for letter b
    Done #1 for letter b
    Done #2 for letter b
    Done #3 for letter b
    ...#4-47...
    Done #48 for letter b
    Done #49 for letter b
    Done #50 for letter b
    Done All the pages for b, moving onto c
    etc...

    But from what I can tell, its not moving to the next page...

    Anyone know why?

    CHEERS in advance!

    fLUx

  2. #2
    SitePoint Enthusiast fLUx1337's Avatar
    Join Date
    Jul 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Spent HOURS and HOURS on this, and I havent moved one step in the right direction so far!

    If anyone know, please post!

    Thanks

  3. #3
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    When $no reaches 50, your code enters an endless loop here:

    PHP Code:
                while($no <= $result){
                if(
    $no == $result){
                
    $page++;
                break;
                } 
    It continually breaks to the while loop outside of that, reenters this while loop, and repeats. $no and $result never change, so it continues infinitely.

    This outputs what you want:
    PHP Code:
    $char 97;
    while (
    $char <= 100) {
            for (
    $i 0$i <= 50$i++) {
                    echo 
    "Done #" $i " for letter " chr($char) . "<br />\n";
            }
            echo 
    "Done all the pages for " chr($char) . ", moving onto " chr($char+1) . "<br />\n";
            
    $char++;


  4. #4
    SitePoint Enthusiast fLUx1337's Avatar
    Join Date
    Jul 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that works perfectly!

    But how do I add the pages?

    i.e. it gets to #50 for letter a, and then goes to the next page to get #51...etc... This is what I've adapted from your code:

    Code PHP:
    $char = 97;
    $page = 1;
    $name=0;
     
    while ($char <= 122) {
    $no_of_pages = pageno('http://blah.com/foo.php?bar=50&letter='.chr($char).''); // function returns a number of pages for that letter
     
    	while ($page <= $no_of_pages) {
     
    	$i = match('http://blah.com/foo.php?bar=50&page='.$page.'&letter='.chr($char).'');
    	$page_names = count($i, COUNT_RECURSIVE);
     
            for ($x = 0; $font <= $page_names; $x++) { 
    				$name++;
                                   //does something
                    echo "Done #" . $i . ", on page ".$page." for letter " . chr($char) . "<br />\n";
            }
    		$page++;
    	}
            echo "Done all the pages for " . chr($char) . ", moving onto " . chr($char+1) . "<br />\n";
            $char++;
    }
    but it seems to be doing the same as last time, only outputing the first page, except this time its missing the letter a, and just starting at b!

    What am I putting in the wrong place??

    Cheers!

    fLUx

  5. #5
    SitePoint Enthusiast fLUx1337's Avatar
    Join Date
    Jul 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Acctully, doesn't matter, sorted it - had some things messed up!

    Cheers Dan!

    fLUx


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •