dub_beat Posted November 16, 2009 Share Posted November 16, 2009 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"; } } } Link to comment https://forums.phpfreaks.com/topic/181784-solved-reset-a-variable-during-a-while-loop/ Share on other sites More sharing options...
MadTechie Posted November 16, 2009 Share Posted November 16, 2009 Something like if($value[0]==$subid && $issubcatticked == false){ Link to comment https://forums.phpfreaks.com/topic/181784-solved-reset-a-variable-during-a-while-loop/#findComment-958717 Share on other sites More sharing options...
dub_beat Posted November 16, 2009 Author Share Posted November 16, 2009 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); Link to comment https://forums.phpfreaks.com/topic/181784-solved-reset-a-variable-during-a-while-loop/#findComment-958728 Share on other sites More sharing options...
MadTechie Posted November 16, 2009 Share Posted November 16, 2009 It would seam that $value is infact an array, do a vardump($value); and see if you can find the data you want and post the results back here (with the info about the expected results) Link to comment https://forums.phpfreaks.com/topic/181784-solved-reset-a-variable-during-a-while-loop/#findComment-958736 Share on other sites More sharing options...
dub_beat Posted November 16, 2009 Author Share Posted November 16, 2009 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]; } } Link to comment https://forums.phpfreaks.com/topic/181784-solved-reset-a-variable-during-a-while-loop/#findComment-958780 Share on other sites More sharing options...
dub_beat Posted November 17, 2009 Author Share Posted November 17, 2009 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); } } Link to comment https://forums.phpfreaks.com/topic/181784-solved-reset-a-variable-during-a-while-loop/#findComment-958798 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.