YII2 - Strange problem with html::checkbox

I have been banging my head on this odd problem for a while not, and figured it time for some more eyeballs on code…

I have two arrays. One that lists all products and one that associates those products with a Session.

I am trying to provide a list of all products with a checkmark to allow the user to associate that product with the session the page is dealing with. ( the sessionid is fixed ). Existing assocations should be displayed already checked…

the problem I am getting is that if any of them are associated, they all get a checkmark…

probably some dumb*** php mistake, but I sure don’t see it…

Thanks

-John




<?= Html::beginForm( '/admin/sessionproducts/save', 'post' ); ?>

	<div class="checkboxgroup">


		<?php

			// loop through all products

			foreach( $products as $product )

			{

				$sessionid = $session['sessionId'];

				$productid = $product['productId'];

				$id = 'Product' . $productid;

				$label = $product['name'];


				// set checkmark to 0, in case of no associations

				$checkmark=0;


				// loop through all associations ( if any ), and set selected if exists..

				foreach( $sessionproducts as $sessionproduct )

				{

					if( $sessionproduct['sessionId'] = $sessionid )

						$checkmark = 1;

					else

						$checkmark = 0;

				}

		?>

				<div>

					<?= Html::checkbox( $id, $checked = $checkmark, 

								$options = ['label' => $label, 'uncheck' => '0'] ); ?>

				</div>

		<?php

					}

		?>


	</div>

	<?= Html::submitButton( 'Update', ['class' => 'btn btn-success']) ?>

	<?= Html::a( 'Cancel', ['admin/sessionproducts'], ['class' => 'btn btn-default'])   ?>

<?= Html::endForm(); ?>




Elaborate please…

Ah, sorry.

You had to write




if( $sessionproduct['sessionId'] == $sessionid ) // comparing



instead of




if( $sessionproduct['sessionId'] = $sessionid ) // populating



:rolleyes: :rolleyes: :rolleyes:

You would think that 30+ years of writing C code would have trained me to always verify comparisons… Guess not… At least not in the wee hours…

"Five minutes of code check by someone else beats five hours with the debugger…"

Thanks

-John

I thought that good IDEs should be able to give warning to this kind of code.

But now I’ve found that my PHPStorm says nothing about it.