Jump to content

Recommended Posts

Hey,

 

I'm trying to use a boolean marker in a while loop. If any returned result is equal to a value stored in an array then I set want to set my boolean to true and do one thing. If the returned result can't be matched in the array I want to set the boolean to false.

 

I'm trying to achieve checking or unchecking check boxes from a query but the code will only ever check 1 box even if more are eligible to be ticked.

 


while ($subs = mysql_fetch_array ($result3, MYSQL_NUM))


	{
		$subid=$subs[0];
		$issubcatticked=FALSE;
		//$startSelectedSubCats is an array I have with allowed values
		foreach ($startSelectedSubCats as &$value) 
		{
			//echo 'value'.$value[0];
			//echo 'SSvalue'.$subs[0];
			echo $issubcatticked;
			if($value[0]==$subid)
			{

				$issubcatticked=TRUE;
			//	echo 'value'.$value[0];

			}
		}

		if($issubcatticked==TRUE)
		{

				echo "	<tr>

				<input type=\"checkbox\" checked=\"yes\"name=\"subcats_check[]\" value={$subs[0]}/>{$subs[2]}<br />
			</tr>\n";


		}else
		{

			echo "	<tr>

				<input type=\"checkbox\" name=\"subcats_check[]\" value={$subs[0]}/>{$subs[2]}<br />
			</tr>\n";


		}

	}


}

Yeah That the logic I'm going after.

 

Unfortunately I dont have enough experience with PHP syntax to do such a thing.

 

im expecting in the foreach loop in my last post for $value to equal for example 1,5,7,8 each iteration of the loop.

 

But when I echo it it just prints ARRAY.

 

 

My array of checked values is gotten like so

 

 


// getting the values into the array
$startSelectedSubCats[]= getAllSubCategories($video_id);

function getAllSubCategories($video_id)
{
$query = "SELECT sub_cat_id FROM video_cat_ass WHERE vid_id='$video_id'";
$result = mysql_query ($query) or die(mysql_error());
$array2=array();
while($row2 = mysql_fetch_array($result))
{	
	array_push($array2,$row2[0]);
}
return $array2;
//$result

}

 

This is my latest attempt

 



	while ($subs = mysql_fetch_array ($result3, MYSQL_NUM))


	{
		$subid=$subs[0];
		$issubcatticked=FALSE;
		$c=count($startSelectedSubCats);

		foreach ($startSelectedSubCats as &$value) 
		{
			echo 'value'.$value; // this is printing out "ARRAY()"
			echo $issubcatticked;
			if($value==$subid)
			{

				$issubcatticked=TRUE;


			}
		}
		unset($value); 

I like var_dump  :)

 

It returns the following

 

valueArrayarray(4) { [0]=>  string(2) "33" [1]=>  string(2) "32" [2]=>  string(2) "30" [3]=>  string(2) "31" }

valueArrayarray(4) { [0]=> string(2) "33" [1]=> string(2) "32" [2]=> string(2) "30" [3]=> string(2) "31" }

valueArrayarray(4) { [0]=> string(2) "33" [1]=> string(2) "32" [2]=> string(2) "30" [3]=> string(2) "31" }

valueArrayarray(4) { [0]=> string(2) "33" [1]=> string(2) "32" [2]=> string(2) "30" [3]=> string(2) "31" }

 

So it is indeed an array.

 

So I guess now I need to modify my loop somehow to an index for loop?

 

I rembember trying this earlier and it gave me trouble


Parse error: syntax error, unexpected T_INC, expecting ')'

 


for ($i=0;i<$c;i++) 
		{
			echo 'value'.$value;
			//var_dump($value);
			//echo 'SSvalue'.$subs[0];
			echo $issubcatticked;
			if($value[$i]==$subid)
			{

				$issubcatticked=TRUE;
			//	echo 'value'.$value[0];

			}
		}

 

 

I got this to work.

 

I was just being a lemon about creating my array.

 

The first element of $startSelectedSubCats was an array because at the top of my file I defined the array as "$startSelectedSubCats[] = " instead of just "$startSelectedSubCats="

 

I dont know if could help anyone but heres the code anyway

 

while ($subs = mysql_fetch_array ($result3, MYSQL_NUM))

	{
		$istick=FALSE;
	 $amtt=count($startSelectedSubCats);
		for($i=0;$i<$amtt;$i++)
		{

			if($startSelectedSubCats[$i]==$subs[0])
			{

				$istick=true;

			}

		}

		if($istick)
		{

			echo "	<tr>

		<input type=\"checkbox\" checked=\"yes\" name=\"subcats_check[]\" value={$row[0]}/>{$subs[2]}<br />
	</tr>\n";

		}else
		{
			echo "	<tr>

		<input type=\"checkbox\"  name=\"subcats_check[]\" value={$row[0]}/>{$subs[2]}<br />
	</tr>\n";
		}
	//var_dump($startSelectedSubCats);



	}


}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.