Re: having checkboxes checked in a loop? how?
| From: | php at server dot mediawaveonline dot com | Date: | Thu, 14 Sep 2000 17:11:32 +0000 |
| Subject: | Re: having checkboxes checked in a loop? how? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-16812@lists.php.net to get a copy of this message | ||
There a re a few errors logically in the exert of your code.
for ($i = 0; $i < count($array2); $i += $n_cols)
{
echo "<tr>\n";
for ($j = 0; $j < $n_cols; $j++)
{
for($index = 0; $index < count($my_comm_ids); $index++)
{
if($arr_comm_ids[$i+$j] == $my_comm_ids[$index])
$checker = "checked";
else
$checker = "";
}
if( !$arr_comm_ids[$i+$j])
break;
echo "<td><input type='checkbox' name='l_comm[]'
value='" . $arr_comm_ids[$i+$j] . "' $checker></td>";
echo "<td>" . $array2[$i+$j] . "</td>\n";
}
echo "</tr>\n";
}
Its very hard to read your code the way its formated, that means its hard
for you to debug.
there were no start <td>'s thats odd. NS wont view it like it was. You
also had the break; in the middle of a <td></td> that will in some cases
cause a <td> to be echo'd and not a </td>, NS wont view that page either.
there is also the case of where I was to put the <tr> I wasnt sure so I
slaped it in, I think I put it in a bad place too though.
readability wise you also often use
for($x = 0; $x <= (count($x) - 1); $x++)
best use
for($x = 0; $x < count($x); $x++)
easier to read. less math involved too, so slightly faster.
If there is any problem with the checked not working it has to be in one
of the condition clauses, I know that sound redundant.
for ($i = 0; $i < count($array2); $i += $n_cols)
is this looping enuogh, this will loop (count($array2) - 1) % $n_cols
for ($j = 0; $j < $n_cols; $j++)
is this looping enough, this will loop ($n_cols - 1) * ( (count($array2) -
1) % $n_cols)
for($index = 0; $index < count($my_comm_ids); $index++)
is this looping enough, this will loop
(count($my_comm_ids) - 1) * ($n_cols - 1) * ( (count($array2) - 1) %
$n_cols)
that last loop is going to loop ALOT ! is that what you want?
with the information provided its hard to determine whats wrong. hopefully
this helps *some*
Chris Lee
On Thu, 14 Sep 2000, cisman raage wrote:
> Hi
>
> I am having checkboxes in two columns and I want to
> have some of them selected initially. The checkboxes
> are to be checked on the basis of some data from the
> database. I am using the following loop to print the
> checkboxes and keep checked some of them. The problem
> is just one checkbox get checked when I want to get
> three or four or more checked. Can someone tell me
> how?
>
> I can do this when I print the checkboxes in one
> column but I want two or three column format and
> checked checkboxes. Any ideas????. the code is below
>
> cisman m m
>
>
>
> $count = count($array2);
> for ($i = 0; $i <= $count-1; $i=$i+$n_cols){
>
> for ($j=0; $j <= ($n_cols-1); $j++ ){
>
> for($index = 0; $index < count($my_comm_ids);
> $index++){
> if($arr_comm_ids[$i+$j] == $my_comm_ids[$index]){
> $checker = "CHECKED";
> }else{
> $checker = "";
> }
> }
>
> echo " <TD>\n";
>
>
>
> if(empty($arr_comm_ids[$i+$j])){
> break;
> }
>
> echo "<input type=\"checkbox\" name=\"l_comm[]\"
> value=\"".$arr_comm_ids[$i+$j]."\" $checker>";
> echo " </TD>\n";
> echo " <TD>".$array2[$i+$j]."</TD>\n";
> }
> echo " </TR>\n";
> }
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Mail - Free email you can access from anywhere!
> http://mail.yahoo.com/
>
>