|
-
Jul 31st, 2002, 05:39 PM
#1
Thread Starter
Stuck in the 80s
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(1 != 2);
Thanks in advance.
-
Jul 31st, 2002, 06:01 PM
#2
PowerPoster
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
-
Jul 31st, 2002, 07:10 PM
#3
Hyperactive Member
or how about this. it's a never ending thread of infinite loops!
PHP Code:
while(true) {
// do some stuff and break it
}
-
Jul 31st, 2002, 07:34 PM
#4
Fanatic Member
why are you wanting to do this?
-
Jul 31st, 2002, 08:21 PM
#5
Thread Starter
Stuck in the 80s
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 (1 != 2);
}
That's pretty much what I"m trying to accomplish.
-
Aug 1st, 2002, 06:59 AM
#6
Fanatic Member
so why not skip the nonsense and just use:
PHP Code:
while ($post = mysql_fetch_array($posts)) {
}
-
Aug 1st, 2002, 11:50 AM
#7
Thread Starter
Stuck in the 80s
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;
}
}
-
Aug 1st, 2002, 12:09 PM
#8
Fanatic Member
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?
-
Aug 1st, 2002, 12:15 PM
#9
Thread Starter
Stuck in the 80s
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.
-
Aug 1st, 2002, 12:58 PM
#10
Fanatic Member
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 />";
}
-
Aug 1st, 2002, 01:09 PM
#11
Thread Starter
Stuck in the 80s
Yeah, that's what I was starting to think. Thanks
-
Aug 1st, 2002, 01:29 PM
#12
Fanatic Member
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
-
Aug 1st, 2002, 01:51 PM
#13
Thread Starter
Stuck in the 80s
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.
-
Aug 1st, 2002, 01:55 PM
#14
Fanatic Member
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
-
Aug 1st, 2002, 01:59 PM
#15
Thread Starter
Stuck in the 80s
I'm not following?
-
Aug 1st, 2002, 02:02 PM
#16
Fanatic Member
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
-
Aug 1st, 2002, 02:03 PM
#17
Thread Starter
Stuck in the 80s
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|