SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2001
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A function giving the name of a checkbox as an array

    I've tried having a function whose parameter is the name of a checkbox or any other input field. However, it is with the checkbox I'm having a problem because more than 1 item can be selected simulataneously.

    Is there anyway I can make the name of the checkbox an array as an argument of the function?

    PHP Code:
    // what I want is to be able to define $name_of_checkbox as array[]
    // and for the checkbox to actually act as an array
    function ($name_of_checkbox)
    {
       echo 
    "<input type=\"checkbox\" name=\"$name_of_checkbox\" value=\"boom\">";} 
    Appreciated.
    Bardius

  2. #2
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you be more specific about what you're trying to do? If you pass an array to a function, it will take it as an array.

    So you can do:
    PHP Code:
    function something($array) {
        foreach(
    $array as $v)
           echo 
    '<input type="checkbox" name="' $v '" value ="' $v '">';
    }
     
    $test = array('test1','test2','test3');
     
    something($test); // should output 3 checkboxes 
    hope this helps

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2001
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't think I'll find that bit of code useful.

    What I'm trying to achieve is really simple. I want to create a function where the one argument is the name of the input field. So for example if I want to create a list of checkboxes and would like the checkboxes to be named 'checkbox[]', I'd like the argument to act as such so that the checkboxes outputted would be part of the checkbox array.
    Here's the heavily stripped down version of my function currently:
    PHP Code:
    function While_Loop($INPUT_TYPE$NAME$ID_COL$ID_NAME$FROM$WHERE$DIVISIBLE_BY$VARIABLE_EQUAL_TO)
    {
        
    $QUERY "SELECT $ID_COL AS id, $ID_NAME AS name FROM $FROM";
        if(
    $WHERE)
        {
            
    $QUERY $QUERY " WHERE $WHERE";
        }
        else
        {
            
    $QUERY $QUERY " ORDER BY $ID_COL DESC";            
        }
        
    $RESULT = @mysql_query($QUERY);
        if(@
    mysql_num_rows($RESULT) == 0)
        {
            echo 
    "<font class='body'>None found!</font><P>";
        }
        else
        {
            
    $i=1;
            
            if(
    $INPUT_TYPE == "radio" OR $INPUT_TYPE == "checkbox")
            { 
                echo 
    "<table cellpadding='0' cellspacing='0'><tr>";
                
    $NUM divisible_by($i,$DIVISIBLE_BY);
                while(
    $ROW mysql_fetch_array($RESULT))
                {
                    if(
    $INPUT_TYPE == "radio")
                    {
                        echo 
    "<td><input type=\"$INPUT_TYPE\" name=\"$NAME\" value=\"$ROW[id]\"";
                    }
                    else(
    $INPUT_TYPE == "checkbox")
                    {
                        
    // DON'T KNOW WHAT TO DO HERE!!!
                        //echo "<td><input type=\"$INPUT_TYPE\" name=\"$name\" value=\"$ROW[id]\"";            
                    
    }
        
                    if(
    $ROW[id] == $VARIABLE_EQUAL_TO)
                    {
                        echo 
    " CHECKED";
                    }
                    echo 
    "><font class='body'>$ROW[name]</font></td>";
                    if(
    $NUM == 1)
                    {
                        echo 
    "</tr><tr>";
                    }    
                    
    $i++;
                }   
                echo 
    "</tr></table>";
            }
            elseif(
    $INPUT_TYPE == "menu")
            {
                echo 
    "<SELECT NAME='$NAME' SIZE='1'>";
                while(
    $ROW mysql_fetch_array($RESULT))
                {
                    echo 
    "<OPTION VALUE=\"$ROW[id]\"";
                    if(
    $ROW[id] == $VARIABLE_EQUAL_TO)
                    {
                        echo 
    " SELECTED";
                    }
                echo 
    ">$ROW[name]</option>";
                }                           
            }
        }

    So if the below attained say 3 rows from the DB, then I would wish the checkboxes be named game[0], game[1] and game[2].
    PHP Code:
    echo $FORM->While_Loop("checkbox""game""ID_Game""Game_Name""tbl_games""$where_clause""1"""); 
    Appreciated.
    Bardius

  4. #4
    SitePoint Zealot headsnet's Avatar
    Join Date
    Feb 2001
    Location
    London, UK
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // DON'T KNOW WHAT TO DO HERE!!!
    //echo "<td><input type=\"$INPUT_TYPE\" name=\"$name\" value=\"$ROW[id]\""; 
    You've already got a counter variable $i that is incremented with each loop, so surely you can just use this in the name attribute. E.g. name=\"$name[$i]\" etc

    PHP Code:
    // DON'T KNOW WHAT TO DO HERE!!!
    echo "<td><input type=\"$INPUT_TYPE\" name=\"$name[$i]\" value=\"$ROW[id]\""
    Or to start the array keys from zero simply use name=\"$name[$i-1]\" etc

    ...unless I've totally got the wrong end of the stick this is the simplest way to solve your problem.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •