SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Variable variables

    Hi,

    I have some inputs in my CodeIgniter view file that I would like to set from the DB. Here's an example section that creates the first input in the form:

    PHP Code:
            $user_name = array(
                      
    'name'        => 'article[name]',
                      
    'id'          => 'name',
                      
    'value'       => set_value('article[name]'$article->name),
                      
    'maxlength'   => '255',
                      
    'size'        => '50',
                      
    'style'       => 'width:100%',
                    );

            echo 
    form_label('Name: *'$user_name['name']).br();
            echo 
    form_input($user_name).br();
            echo 
    form_error($user_name['name']).br(); 
    If I wanted to put the above code in a loop, and change the "$user_name" and "name" in each iteration, how can I achieve this? Variable variables i think?

  2. #2
    SitePoint Zealot bronze trophy xMog's Avatar
    Join Date
    Mar 2011
    Posts
    148
    Mentioned
    3 Post(s)
    Tagged
    1 Thread(s)
    Maybe this would work :

    Code:
    function displayForm($dataArr, $label) {
            echo form_label($label . ': *', $dataArr['name']).br(); 
            echo form_input($dataArr).br(); 
            echo form_error($dataArr['name']).br(); 
    }

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    41
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I think you are asking for something like this:
    PHP Code:
    foreach ($users as $user) {
       echo 
    form_label('Name: *'$user['name']).br(); 
       echo 
    form_input($user).br(); 
       echo 
    form_error($user['name']).br(); 

    Where $users would be assigned to an array of values similar to what you have assigned to $user_name on your initial post.

  4. #4
    SitePoint Zealot bronze trophy xMog's Avatar
    Join Date
    Mar 2011
    Posts
    148
    Mentioned
    3 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by parallelist View Post
    I think you are asking for something like this:
    PHP Code:
    foreach ($users as $user) {
       echo 
    form_label('Name: *'$user['name']).br(); 
       echo 
    form_input($user).br(); 
       echo 
    form_error($user['name']).br(); 

    Where $users would be assigned to an array of values similar to what you have assigned to $user_name on your initial post.
    There's the label "Name:" that will always be the same in your "foreach loop". I think that he wants to change the array and that label? It's not clear if the OP wants to change the field completely or just have a bunch of fields named "name"?

    Anyway, you could still do it with a method like:

    Code:
    function displayFormField($dataArr) {
            if (empty($dataArr['size'])) {
                  $dataArr['size'] = '50';
            }
    
            ...
    
            echo form_label($dataArr['label'] . ': *', $dataArr['name']).br(); 
            echo form_input($dataArr).br(); 
            echo form_error($dataArr['name']).br(); 
    }
    
    
    $allArrData[0] = array( 
                      'name'        => 'article[name]', 
                      'label'    => 'Name:',
                      'id'          => 'name', 
                      'value'       => set_value('article[name]', $article->name), 
                      'maxlength'   => '255', 
                      'size'        => '50', 
                      'style'       => 'width:100%', 
                    ); 
    
    $allArrData[1] = array( 
                      'name'        => 'article[name2]', 
                      'label'    => 'Name2:',
                      'id'          => 'name2', 
                      'value'       => set_value('article[name2]', $article->name2), 
                      'maxlength'   => '255', 
                      'size'        => '50', 
                      'style'       => 'width:100%', 
                    ); 
    
    
    foreach ($allArrdata as $arrData) {
       displayFormField($arrData);
    }


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
  •