Link to home
Start Free TrialLog in
Avatar of ggjones
ggjonesFlag for Afghanistan

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

.



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;

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

Perhaps you are seeking the continue keyword?
Avatar of ggjones

ASKER

... thanks for replying.

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.
Avatar of ggjones

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".


.
ASKER CERTIFIED SOLUTION
Avatar of dsmile
dsmile
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ggjones

ASKER

Thanks dsmile.

I initialized $myURL inside the loop, rather than outside. That was the problem.

.