ggjones
asked on
Pass a single value out of a loop
I need to pass a single value from a loop.
Currently, what happens is that the value that I need gets over-written during the next loop.
If I use "break", the rest of the processing and the next loops get skipped, which needs not to happen..
How can this be done?
regards,
GJ
.
Currently, what happens is that the value that I need gets over-written during the next loop.
If I use "break", the rest of the processing and the next loops get skipped, which needs not to happen..
How can this be done?
regards,
GJ
.
ID|TYPE|URL|LABEL
1|4|www.google.com|google
2|2|www.yahoo.com|yahoo
3|1|www.bing.com|bing
4|2|www.bookface.com|bookface
while(row = mysql_fetch_assoc($result)){
if (row[TYPE] == 1){
$myURL = row[URL];
}
$theLinks .= "\n<div><a href='http://" . row[URL] . "' target='_blank'>" . row[LABEL] . "</a></div>";
}
echo $myURL;
Perhaps you are seeking the continue keyword?
ASKER
... thanks for replying.
From the documentation, I don't think "continue" is the solution.
regards,
GJ
.
From the documentation, I don't think "continue" is the solution.
regards,
GJ
.
OK. Can you clarify what you mean by, "I need to pass a single value from a loop?" You typically don't "pass" anything back from a loop unless your language supports something like a yield keyword (such as Python, C#, or Ruby)--from what I am reading, PHP does not have such a construct.
ASKER
thanks for replying.
If you look at the pseudo code you will see the last line:
echo $myURL;
The value of $myURL should be "www.bing.com", but it isn't, because during the last loop, row[TYPE] does not fulfill the condition "== 1".
.
If you look at the pseudo code you will see the last line:
echo $myURL;
The value of $myURL should be "www.bing.com", but it isn't, because during the last loop, row[TYPE] does not fulfill the condition "== 1".
.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks dsmile.
I initialized $myURL inside the loop, rather than outside. That was the problem.
.
I initialized $myURL inside the loop, rather than outside. That was the problem.
.