PHP Break and continue method are not working

Hello Guys,

I have loop and using php break and continue methods but those are not working. Can you tell me how to use break and continue methods in yii? Here is my loop

if($this->enforceBookOwnerRadio == 1 ) {

; // Drop down to rest of processing 


goto FinishedValidating; ---this is not working too 

} elseif($this->enforceBookOwnerRadio == 3) {

continue; //Skip to the next Book 

} elseif($this->enforceBookOwnerRadio == 4) {

$abortReason="User Chose: Abort Book not owned by this User";

break; //Break out of the for loop. Use the $$abortReason variable to display an information message.

} elseif ($this->enforceBookOwnerRadio == 2) {

$this->enforceBookOwner=0; // Don’t check owner for any more Books. Fall through to rest of processing.

} else { // If you get here then the program has a bug.

$programError="Program error, wrong user enforceBookOwner";

break;

}

Would be nice if you can post your complete loop… and enclose the code with the code button <>… or use the code directives [ code] and [ /code] (without space), so the code is more readable

This loop is inside for loop

here is my complete loop

for ($book=$this->startRange;$book <= $this->endRange; $book++) {

&#036;connection = Yii::app()-&gt;db;





&#036;sql=&quot;SELECT id,User_id,Lab_id from BookRecords where tn='&#036;book'&quot;;


&#036;command=&#036;connection-&gt;createCommand(&#036;sql);


&#036;row = &#036;command-&gt;queryRow();





&#036;abortReason=&quot;&quot;;


&#036;programError=&quot;&quot;;


if(&#036;row[User_id] &#33;= Yii::app()-&gt;user-&gt;id) { 


	if(&#036;this-&gt;enforceBookOwnerRadio == 1 ) {


	; // Drop down to rest of processing


		goto FinishedValidating; ---this is not working too





	} elseif(&#036;this-&gt;enforceBookOwnerRadio == 3) {


		continue; //Skip to the next Book





	} elseif(&#036;this-&gt;enforceBookOwnerRadio == 4) {


		&#036;abortReason=&quot;User Chose: Abort Book not owned by this User&quot;;


		break; //Break out of the for loop. Use the &#036;&#036;abortReason variable to display an information message.





	} elseif (&#036;this-&gt;enforceBookOwnerRadio == 2) {


		&#036;this-&gt;enforceBookOwner=0; // Don't check owner for any more Books. Fall through to rest of processing.





	} else { // If you get here then the program has a bug.


		&#036;programError=&quot;Program error, wrong user enforceBookOwner&quot;;


		break;


	} 


}

// BEGIN inseting Reocrds into Table:

} //End For Loop

These are just if… else… statements… this is not a loop!

Can you post your complete code WITH the loop.

Reposting his code in code =)




		for ( $book=$this->startRange; $book <= $this->endRange; $book++ )

		{

			$connection = Yii::app()->db;

			

			$sql="SELECT id,User_id,Lab_id from BookRecords where tn='$book'";

			$command=$connection->createCommand($sql);

			$row = $command->queryRow();

			$abortReason="";

			$programError="";

			if( $row[User_id] != Yii::app()->user->id )

			{

				if($this->enforceBookOwnerRadio == 1 )

				{

					; // Drop down to rest of processing

					goto 'FinishedValidating'; //---this is not working too

				}

				elseif($this->enforceBookOwnerRadio == 3)

				{

					continue; //Skip to the next Book

				}

				elseif($this->enforceBookOwnerRadio == 4)

				{

					$abortReason="User Chose: Abort Book not owned by this User";

					break; //Break out of the for loop. Use the $$abortReason variable to display an information message.

				}

				elseif ($this->enforceBookOwnerRadio == 2)

				{

					$this->enforceBookOwner=0; // Don't check owner for any more Books. Fall through to rest of processing.

				}

				else

				{ // If you get here then the program has a bug.

					$programError="Program error, wrong user enforceBookOwner";

					break;

				}

			}

			// BEGIN inseting Reocrds into Table:

		} //End For Loop

THanks Sobolev. Do you have any idea how to solve this issue?

Sorry but I still don’t understand this code…

As I see this… the variable $this->enforceBookOwnerRadio is always the same… it does not change value… .so why then a loop…

Hello mdomba,

$this->enforceBookOwnerRadio has four values like

$this->enforceBookOwnerRadio == 1, $this->enforceBookOwnerRadio == 2, $this->enforceBookOwnerRadio == 3 and $this->enforceBookOwnerRadio == 4

this is view file

<?php echo CHtml::activeRadioButtonList($form,'enforceBookOwnerRadio ',array(1=>“Yes to All Books”,2=>“Yes for this Book”,3=>“No for this Book”,4=>“No to All Books”)); ?>

do you see the values?

I m still stuck. Can you help me?

Can you explain me what you need to be done… what is the expected …

If that variable has always the same value… .than why are you using a loop… say for example that the value is 3… than you would just "continue" in a loop

suppose I want to enter these id 5713799980-9999, it will check if this user has own this id or not, then user will choose one of four option(<?php echo CHtml::activeRadioButtonList($form,'enforceBookOwnerRadio ',array(1=>“Yes to All Books”,2=>“Yes for this Book”,3=>“No for this Book”,4=>“No to All Books”)); ?>)

. if user check $this->enforceBookOwnerRadio ==1 then it will insert all records(5713799980-9999) to database, if user check $this->enforceBookOwnerRadio ==2, then it will insert only 5713799980 number, if $this->enforceBookOwnerRadio ==3 then Skip to the next number, and if $this->enforceBookOwnerRadio ==4 then back to original page.

I don’t seee your input form so I don’t know how all this looks

What is startRange and endRange (for loops) in your example?

And enforceBookOwner is for every book selected or for all together?

I don’t think you are using goto properly. The goto’s target point should not be in quotations. Also, make sure that you are at least running PHP 5.3. The goto control structure does not work in earlier versions.

I m Running PHP 5.2.6.

Then the goto operator will not work in your code. It is only available for 5.3 and above. Check the Note in the manual here.

I install php 5.3. If I use "goto FinishedValidating;". Yii can not except this. any idea?

Where are you putting the goto target point?




FinishedValidating:



Is it in a model, view, or controller function? In other words, what is the context of the goto and the target point FinishedValidating?

It is in a model. Target point is

goto FinishedValidating;

There are 2 separate lines of code for a goto statement to work. First is the goto operator followed by the target point:




goto FinishedValidating;



Second is the target point:




FinishedValidating:



The second part is where the code goes to when the goto operator is used. Where are you putting the target point? I don’t see it in the code you posted.

I use FinishedValidating: out of my for loop.

for loop {


}

FinishedValidating:

It should work. Goto works when I use it. There may be something else wrong with your code. Be sure and post any errors you receive. Other than that, I’m out of ideas.