Re: using for loop in function

From: Date: Thu, 29 Mar 2007 03:45:23 +0000
Subject: Re: using for loop in function
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-251596@lists.php.net to get a copy of this message
itoctopus wrote:
I don't quite understand why you're naming the checbox, you can easily go for name={$name}[]; -- itoctopus - http://www.itoctopus.com "Jim Lucas" <lists@cmsws.com> wrote in message news:460A02B4.9070904@cmsws.com...
Richard Kurth wrote:
The function below will create a group of checkboxes it will also
check
the checkbox that is stored in the table field that is in $select_value
this
works fine if there is only one value in the variable but if there is
more
stored like 1,2,3,4 it will not work. I am trying to figure out how to
use
the following for loop to add the number to each checkbox and if they
match
mark the checkbox checked $ExplodeIt = explode(",",$select_value,",");
        $Count = count($ExplodeIt);
        for ($i=0; $i < $Count; $i++) {
          }
function checkbox($sqlu,$name,$value1,$value2,$select_value){
      $name_result = safe_query($sqlu);
       while($rowu=mysql_fetch_array($name_result)){
echo "<tr><td colspan=\"2\" align=\"left\"><input type='checkbox'
name='"
. $name . "[" . $rowu[$value1] . "]' value=\"$rowu[$value1]\"";
         if($select_value==$rowu[$value1]) {
           echo " checked>$rowu[$value2]\n</td></tr><BR>";
}else{
              echo ">$rowu[$value2]\n</td></tr><BR>";
      }
} }
I have used in_array as a way of doing what you are looking for. function checkbox($sqlu,$name,$value1,$value2,$select_value){
     $name_result = safe_query($sqlu);
     while ( $rowu = mysql_fetch_array($name_result) ) {
         $checked = '';
         if ( in_array($rowu[$value1], $select_value) ) {
             $checked = ' checked="checked"';
         }
         echo <<<HTML
<tr>
     <td colspan="2" align="left">
         <input
             type="checkbox"
             name="{$name}[{$rowu[$value1]}]"
             value="{$rowu[$value1]}"
             {$checked} />{$rowu[$value2]}
     </td>
</tr> HTML;
     }
} Personally, I would change your SQL query to return only the two columns that you are wanting to display. And also use the mysql_fetch_assoc() SELECT id AS colA, name AS colB FROM myTable; and not this SELECT * FROM myTable; This would then allow me to write your function this way. function checkbox($sqlu,$name,$selected=array()){
     if ( ( $name_result = safe_query($sqlu) ) === false ) {
         die('something went wrong');
     }
     while ( $rowu = mysql_fetch_assoc($name_result) ) {
         $checked = '';
         if ( in_array($rowu['colA'], $selected) ) {
             $checked = ' checked="checked"';
         }
         echo <<<HTML
<tr>
     <td colspan="2" align="left">
         <input    type="checkbox"
             name="{$name}[{$rowu['colA']}]"
             value="{$rowu['colA']}"
             {$checked} />{$rowu['colB']}
     </td>
</tr> HTML;
     }
} Hope this helps
could be that the OP has more then one field/array called $name[]
this way ne can have $name['this'] $name['that'] etc...

« previous php.general (#251596) next »