Results 1 to 17 of 17

Thread: Loop Question

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Loop Question

    Color me stupid here, but is there a loop that doesn't require something to stop it?

    I mean like:

    PHP Code:
      do {
        
    //blah blah, code to break in here
      

    except that requires a while();

    Right now I'm just doing this:

    PHP Code:
      do {
        
    //blah blah, code to break in here
      
    } while(!= 2); 
    Thanks in advance.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Not sure if this right but you can try this:
    PHP Code:
    do {
        
    //blah blah, code to break in here
      
    } while( ; ); 
    or

    PHP Code:
    for( ;; ){
        
    //blah blah, code to break in here

    Take out the space in for() and while() because when I posted without the spaces, it put some other image-linking text.
    Last edited by abdul; Jul 31st, 2002 at 06:05 PM.
    Baaaaaaaaah

  3. #3
    Hyperactive Member Kagey's Avatar
    Join Date
    Sep 2000
    Location
    The Wilderness of New Brunswick
    Posts
    294
    or how about this. it's a never ending thread of infinite loops!
    PHP Code:
    while(true) {
       
    // do some stuff and break it


  4. #4
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    why are you wanting to do this?
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  5. #5

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Because I need to be able to loop through MySQL records and I have to know as I'm working with it if it's the last record. This is a brief part of my code:

    PHP Code:
        if ($post mysql_fetch_array($posts)) {
            do {
                
    //do stuff here
                
                
    if ($post mysql_fetch_array($posts)) {
                    
    //if there's another record after this one
                    //do special stuff
                
    } else {
                    
    //no more records after this one
                    //don't do special stuff
                    
    break;
                }
            } while (
    != 2);
        } 
    That's pretty much what I"m trying to accomplish.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    so why not skip the nonsense and just use:

    PHP Code:
    while ($post mysql_fetch_array($posts)) {


    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  7. #7

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by cpradio
    so why not skip the nonsense and just use:

    PHP Code:
    while ($post mysql_fetch_array($posts)) {


    Because I need to do the fetch_array inside the loop so I can see if there is another one. If I do it this way, then It'll be skipping records:

    PHP Code:
    while ($post mysql_fetch_array($posts)) {
      
    //do stuff here
                
      
    if ($post mysql_fetch_array($posts)) {
        
    //if there's another record after this one
        //do special stuff
      
    } else {
        
    //no more records after this one
        //don't do special stuff
        
    break;
      }

    It'll skip records because it's fetching twice before it prints.

    I suppose I could do this:

    PHP Code:
      $num mysql_num_rows($posts);

      for(
    $i 1$i <= $num$i++) {
        if (
    $post mysql_fetch_array($posts) {
          
    //do stuff here
          
    if ($i != $num) {
            
    //do special stuff
          
    }
        } else {
          echo 
    "No records found!";
          break;
        }
      } 
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    I'm having troubles trying to figure out your goal, but I feel there is a better way then possiblity running an inifinite loop.

    Can you give an example?
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  9. #9

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I'll try...

    What I'm doing this for is that PHP NewsPro I was working on. When it prints the news, it's in a format like this:

    News Item #1
    ------------------
    News Item #2
    ------------------
    News Item #3
    The "------------------" is supposed to be a <hr>, but notice that after Item #3 there's no <hr>. So I want to place the <hr> between each news item, but I don't want to have one at the bottom of the page if there's one after it.

    So I'm looping through the recordset but I have to know if the current $post is the last in the record set so I know whether or not to echo that <hr>

    Does this make any sense whatsoever? I'm not sure I can explain it any better.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Ahh, i see, then you want this:

    PHP Code:
    for ($i=0;$i<mysql_num_rows($posts);$i++) {
      
    $post mysql_fetch_array($posts)
      echo 
    $post;
      if (
    $i != mysql_num_rows($posts)-1)
        echo 
    "<hr />";

    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  11. #11

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Yeah, that's what I was starting to think. Thanks
    My evil laugh has a squeak in it.

    kristopherwilson.com

  12. #12
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Yeah, anytime you start to think an infinite loop is needed, you have to kick yourself in the head b/c I doubt there is any good understandable reason you will have to use one.

    BTW, did you ever receive my PM? You can ignore it now.

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  13. #13

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by cpradio
    Yeah, anytime you start to think an infinite loop is needed, you have to kick yourself in the head b/c I doubt there is any good understandable reason you will have to use one.

    BTW, did you ever receive my PM? You can ignore it now.

    -Matt
    Well, the loop wasn't really infinite if I was breaking it, though.

    I just noticed your PM. Sorry 'bout that.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  14. #14
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Hey, you may want to look at this when it comes to finishing your forum and newspro thingy-ma-jig.
    http://www.vbforums.com/showthread.p...hreadid=189037
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  15. #15

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by cpradio
    Hey, you may want to look at this when it comes to finishing your forum and newspro thingy-ma-jig.
    http://www.vbforums.com/showthread.p...hreadid=189037
    I'm not following?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  16. #16
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Well, assuming your forum will be like vBulletin and you will have vbCode (or something similiar), that function at the very end will probably save you a lot of time.

    I will send you a PM so you can see exactly what I mean. As from just looking at it in that thread and the link, it leaves out a lot of information.

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  17. #17

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by cpradio
    Well, assuming your forum will be like vBulletin and you will have vbCode (or something similiar), that function at the very end will probably save you a lot of time.

    I will send you a PM so you can see exactly what I mean. As from just looking at it in that thread and the link, it leaves out a lot of information.

    -Matt
    I already know how to do tags...
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width