Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Regular Coder che_anj's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    157
    Thanks
    12
    Thanked 3 Times in 3 Posts

    any know how to loop element of form

    I would like to minimize writing a lot of codes just to have those textbox while I think theres a way to shorten it.. Anyone can help how.. this is my code..
    PHP Code:

    <?php
    echo '    <form name="adduser" method="post">';
    function 
    testing()
      {
        for(
    $i 0$i 6$i++)    
        {
            echo 
    '<tr><td><input type="text" name="a['.$i.']"></td></tr>';
        }
        
      }

    echo 
    '<table width="50%" cellpadding="2" cellspacing="0" class="table-outline">
    <tr><td class="detail-title">Add User</td>
    </tr>
    </table>
    <table  width="50%" cellpadding="2" cellspacing="0" class="table-list">'
    ;
      echo 
    testing();

    echo 
    '<tr><td colspan="2" class="table-list-title">
            <input type="submit" name="add" value="Register">
            <input type="reset" name="add" value="Cancel"></td></tr>
        </table></form>'
    ;
    so far its the looping is work, but I want also the Caption should be present like ..

    Username:<input type="text" name="a[0]">
    Name:<input type="text" name="a[1]">
    ....

    and what about If im going to insert the values of the textboxes? HOw could I do it since its on array form...

    mysql_query("insert into users (username,password...)VALUES('$_POST[]')");
    tnx...

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    foreach?

  • #3
    Regular Coder che_anj's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    157
    Thanks
    12
    Thanked 3 Times in 3 Posts
    do u know what to apply the foreach,, i try this

    $text= array("Username","Password","Name","Email");
    foreach($text as $names)
    {
    echo $names;
    }

    but how to apply it together with the looping of textboxes.. Im confuse..

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,009
    Thanks
    2
    Thanked 312 Times in 304 Posts
    In your foreach example, $text is an array, yes?

    From your form, $_POST['a'] will be an array -
    Code:
        [a] => Array
            (
                [0] => 
                [1] => 
                [2] => 
                [3] => 
                [4] => 
                [5] => 
            )
    Using your foreach code -
    PHP Code:
    foreach($_POST['a'] as $names)
    {
    echo 
    $names;

    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    New Coder
    Join Date
    Aug 2007
    Posts
    26
    Thanks
    0
    Thanked 4 Times in 4 Posts
    PHP Code:
    function testing($data_array)
    {
        foreach (
    $data_array as $key => $val)    
        {
            echo 
    "<tr>";
                echo 
    "<td>$val :</td>";
                echo 
    "<td><input type='text' name='$key'></td>";
            echo 
    "</tr>";
        }
        
    }


    $text_array = array('username'=>'Username','password'=>'Password','name'=>'Name','email'=>'Email');

    echo 
    "<form name='adduser' method='post'>";
    echo 
    "<table width='50%' cellpadding='2' cellspacing='0' class='table-outline'>";
    echo 
    "<tr>";
        echo 
    "<td class='detail-title'>Add User</td>";
    echo 
    "</tr>";
    echo 
    "</table>";

    echo 
    "<table  width='50%' cellpadding='2' cellspacing='0' class='table-list'>";
    testing($text_array);
    echo 
    "<tr>";
        echo 
    "<td colspan='2' class='table-list-title'>";
            echo 
    "<input type='submit' name='add' value='Register'>";
            echo 
    "<input type='reset' name='reset' value='Cancel'>";
        echo 
    "</td>";
    echo 
    "</tr>";
    echo 
    "</table>";
    echo 
    "</form>"

  • #6
    Regular Coder che_anj's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    157
    Thanks
    12
    Thanked 3 Times in 3 Posts
    Great! it works .. Thanks a lot Bahamut...

  • #7
    Regular Coder che_anj's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    157
    Thanks
    12
    Thanked 3 Times in 3 Posts
    By the way, What about if im going to include the type of the input..

    example. echo "
    <td><input type='$type' name='$key'></td>";

    because i have a password type of input..

    tnx..

  • #8
    New Coder
    Join Date
    Aug 2007
    Posts
    26
    Thanks
    0
    Thanked 4 Times in 4 Posts
    PHP Code:
    function testing($data_array)
    {
        foreach (
    $data_array as $val)    
        {
            echo 
    "<tr>";
                echo 
    "<td>$val[text] :</td>";
                echo 
    "<td><input type='$val[type]' size='$val[size]' maxlength='$val[max]' name='$val[name]'></td>";
            echo 
    "</tr>";
        }
        
    }


    $text_array = array(
                    
    'username' => array (
                        
    'name'=> 'username',
                        
    'text'=> 'Username',
                        
    'type'=> 'text',
                        
    'size'=> 25,
                        
    'max' => 25
                    
    ),    
                    
    'password' => array (
                        
    'name'=> 'password',
                        
    'text'=> 'Password',
                        
    'type'=> 'password',
                        
    'size'=> 20,
                        
    'max' => 20
                    
    ),    
                    
    'email' => array (
                        
    'name'=> 'email',
                        
    'text'=> 'Email',
                        
    'type'=> 'text',
                        
    'size'=> 30,
                        
    'max' => 30
                    
    )
    );                                

    echo 
    "<form name='adduser' method='post'>";
    echo 
    "<table width='50%' cellpadding='2' cellspacing='0' class='table-outline'>";
    echo 
    "<tr>";
        echo 
    "<td class='detail-title'>Add User</td>";
    echo 
    "</tr>";
    echo 
    "</table>";

    echo 
    "<table  width='50%' cellpadding='2' cellspacing='0' class='table-list'>";
    testing($text_array);
    echo 
    "<tr>";
        echo 
    "<td colspan='2' class='table-list-title'>";
            echo 
    "<input type='submit' name='add' value='Register'>";
            echo 
    "<input type='reset' name='reset' value='Cancel'>";
        echo 
    "</td>";
    echo 
    "</tr>";
    echo 
    "</table>";
    echo 
    "</form>"

  • #9
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Don't echo all of that static HTML, there's no reason to, and it's just more work for you:
    PHP Code:
    <?php
    function testing($data_array)
    {
        foreach (
    $data_array as $val)
        {
            
    ?>
            <tr>
                <td><?=$val['text'];?> :</td>
                <td><input type="<?=$val['type']?>" size="<?=$val['size']?>" maxlength="<?=$val['max']?>" name="<?=$val['name']?>" /></td>
            </tr>
            <?
        
    }
        
    }


    $text_array = array(
                    
    'username' => array (
                        
    'name'=> 'username',
                        
    'text'=> 'Username',
                        
    'type'=> 'text',
                        
    'size'=> 25,
                        
    'max' => 25
                    
    ),    
                    
    'password' => array (
                        
    'name'=> 'password',
                        
    'text'=> 'Password',
                        
    'type'=> 'password',
                        
    'size'=> 20,
                        
    'max' => 20
                    
    ),    
                    
    'email' => array (
                        
    'name'=> 'email',
                        
    'text'=> 'Email',
                        
    'type'=> 'text',
                        
    'size'=> 30,
                        
    'max' => 30
                    
    )
    );                                
    ?>
    <form name="adduser" method="post">
    <table width="50%" cellpadding="2" cellspacing="0" class="table-outline">
        <tr>
            <td class="detail-title">Add User</td>
        </tr>
    </table>

    <table  width="50%" cellpadding="2" cellspacing="0" class="table-list">
        <?=testing($text_array);?>
        <tr>
            <td colspan="2" class="table-list-title">
                <input type="submit" name="add" value="Register" />
                <input type="reset" name="reset" value="Cancel" />
            </td>
        </tr>
    </table>
    </form>
    Note: If you're going to redistribute the code or if you have short tags off you should use <?php echo $var; ?> instead of <?=$var;?>.
    Also, if you're only running the loop once you don't really need to put it in a function.

  • #10
    Regular Coder che_anj's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    157
    Thanks
    12
    Thanked 3 Times in 3 Posts
    Thanks Bahamut, it works well..

    Btw, What about if theres a select box? is it possible to include it in the loop? or make another function?


    tnx

  • #11
    New Coder
    Join Date
    Aug 2007
    Posts
    26
    Thanks
    0
    Thanked 4 Times in 4 Posts
    PHP Code:
    function testing($data_array)
    {
        foreach (
    $data_array as $val)    
        {
            echo 
    "<tr>";
                echo 
    "<td>$val[text] :</td>";
                if (
    $val['type'] == 'select') {
                    echo 
    "<td>";
                        echo 
    "<select size='$val[size]' name='$val[name]'>";
                        foreach (
    $val['options'] as $k => $v)
                            echo 
    "<option value='$k'>$v</option>";
                        echo 
    "</select>";                        
                    echo 
    "</td>";
                } else {
                    echo 
    "<td><input type='$val[type]' size='$val[size]' maxlength='$val[max]' name='$val[name]'></td>";
                }                     
            echo 
    "</tr>";
        }
        
    }


    $text_array = array(
                    
    'username' => array (
                        
    'name'=> 'username',
                        
    'text'=> 'Username',
                        
    'type'=> 'text',
                        
    'size'=> 25,
                        
    'max' => 25
                    
    ),    
                    
    'password' => array (
                        
    'name'=> 'password',
                        
    'text'=> 'Password',
                        
    'type'=> 'password',
                        
    'size'=> 20,
                        
    'max' => 20
                    
    ),    
                    
    'email' => array (
                        
    'name'=> 'email',
                        
    'text'=> 'Email',
                        
    'type'=> 'text',
                        
    'size'=> 30,
                        
    'max' => 30
                    
    ),
                    
    'gender' => array (
                        
    'name'=> 'gender',
                        
    'text'=> 'Gender',
                        
    'type'=> 'select',
                        
    'size'=> 1,
                        
    'options' => array ('M'=>'Male','F'=>'Female')
                    )
    );                                

    echo 
    "<form name='adduser' method='post'>";
    echo 
    "<table width='50%' cellpadding='2' cellspacing='0' class='table-outline'>";
    echo 
    "<tr>";
        echo 
    "<td class='detail-title'>Add User</td>";
    echo 
    "</tr>";
    echo 
    "</table>";

    echo 
    "<table  width='50%' cellpadding='2' cellspacing='0' class='table-list'>";
    testing($text_array);
    echo 
    "<tr>";
        echo 
    "<td colspan='2' class='table-list-title'>";
            echo 
    "<input type='submit' name='add' value='Register'>";
            echo 
    "<input type='reset' name='reset' value='Cancel'>";
        echo 
    "</td>";
    echo 
    "</tr>";
    echo 
    "</table>";
    echo 
    "</form>"

  • #12
    Regular Coder che_anj's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    157
    Thanks
    12
    Thanked 3 Times in 3 Posts
    Brilliant!!! Tnx Bahamut... Ur great!...

  • #13
    Regular Coder che_anj's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    157
    Thanks
    12
    Thanked 3 Times in 3 Posts
    one last question Bahamut pls forgive me...

    Im using function in my select box..

    example..
    Code:
    //select box function
    function html_select($name,$value,$arrayof,$extra='',$selectedvalue='')
    {
    $str = "<select name='$name' class='small'>";
    $str .="<option value='0'>Choose One</option>";
    foreach($arrayof as $key=>$val)
    
    {
        if($key==$selectedvalue)
            $str .="<option value='$key' selected>$val</option>";
        else
            $str .="<option value='$key'>$val</option>";
    
    }
    $str.="</select>";
    
    return $str;
    }
    Code:
    //function for Query Code
    function array_of($table)
    {
    	global $link;
    	switch ($table) 
    	{
    		case'users':
    			$query="select * from users";
    			break;
    			
    		case 'coun':
    			$query="select id,value from countries";
    			break;
    			
    		case 'comp':
    			$query="select * from client";
    			break;
    	}
    
    
    	$result = dbquery($link, $query);
    	
    	while($row=mysql_fetch_array($result)) 
    	{
    		$arrayof[$row[0]]=$row[1];
    	}	
    		return $arrayof;
    }
    Code:
    //calling the function for displaying the select box
    
    '.html_select('countries',0,array_of('coun')).'
    tnx..

  • #14
    New Coder
    Join Date
    Aug 2007
    Posts
    26
    Thanks
    0
    Thanked 4 Times in 4 Posts
    PHP Code:
        foreach ($data_array as $val)    
        {
            echo 
    "<tr>";
                echo 
    "<td>$val[text] :</td>";
                if (
    $val['type'] == 'select') {
                    echo 
    html_select($val['name'], ''$val['options'], ''$val['value']);
                } else {
                    echo 
    "<td><input type='$val[type]' size='$val[size]' maxlength='$val[max]' name='$val[name]'></td>";
                }                     
            echo 
    "</tr>";
        } 

    PHP Code:
        'country' => array (
            
    'name'=> 'country',
            
    'text'=> 'Country',
            
    'type'=> 'select',
            
    'size'=> 1,
            
    'value' => 'USA',
            
    'options' => array_of('coun')
        ) 

  • Users who have thanked Bahamut for this post:

    che_anj (08-21-2007)

  • #15
    Regular Coder che_anj's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    157
    Thanks
    12
    Thanked 3 Times in 3 Posts
    Great! Bahamut ur the best... Thanks a lot...Its a big help...


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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