Zikula: A Flexible Open Source Content Management System
home | forum | contact us

Dizkus

Bottom
dynamic form...
  • Posted: 18.07.2006, 22:31
     
    rowjoe
    rank:
    Helper Helper
    registered:
     February 2004
    Status:
    offline
    last visit:
    26.07.06
    Posts:
    129
    In a module i am working on, I have a form on the admin page to update records in the DB. A part of this form is generated dynamically based on what is in the database for that record.


    Kinda like the phpbb poll form. Where it generates x number of fields depending on how many questions you have in your poll.


    My question is, how do I pass those variables from the form to the update function? I'm working from the example module and the way that is set up, I need to list all the varibles before I can use them. But I obviously cannot do that if I don't know how many variables there will be for a given record.

    Here is the piece of code I'm talking about:

    Code

    function sked_admin_updatestuff($args)
    {

        // Get parameters
        list($sid,
             $s_preview,
             $s_review,
             $link_id1,
             $link_type1,
         $link_lbl1,
         $link_lnk1,
         $link_lang1) = pnVarCleanFromInput('sid',
                                                's_preview',
                                                's_review',
                                                'link_id1',
                                                'link_type1',
                                                'link_lbl2',
                                                'link_lnk1',
                                                'link_lang1');
       
        extract($args);


    Is there a better way I should be doing this? Any advice would be great. thanks. icon_smile








    edited by: rowjoe, Jul 18, 2006 - 05:33 PM
  • Posted: 19.07.2006, 19:30
     
    alarconcepts
    rank:
    Professional Professional
    registered:
     April 2004
    Status:
    offline
    last visit:
    21.01.08
    Posts:
    2723
    In your template form, use inputs with names such as:

    Code

    <input type="text" name="form_data[this]" value="" />
    <input type="text" name="form_data[that]" value="" />
    <input type="text" name="form_data[theother]" value="" />


    That way, in the PHP (update function) you can simply grab a single variable as:

    Code

    sked_admin_updatestuff($args)
    {
        $woo_hoo = pnVarCleanFromInput('form_data');

        // And the rest of the code...

    }


    ...this will grab all the form data in a nice, neat single variable, no matter how many (or few) fields there are.

    --
    Photography | PHP | Other
  • Posted: 19.07.2006, 19:37
     
    cyber_wolf
    rank:
    Helper Helper
    registered:
     June 2002
    Status:
    offline
    last visit:
    09.09.08
    Posts:
    288
    If you don't know how many rows there will be, you can use arrays. In your form, in the input, textarea, select rows; use as your parameter....

    Code

    name="link_id[]"
    name="link_type[]"
    name="link_lbl[]"
    name="link_lnk[]"
    name="link_lang[]"


    Then in your PHP code.....

    Code

    list($link_id,
             $link_type,
             $link_lbl,
             $link_lnk,
             $link_lang) = pnVarCleanFromInput('link_id',
                                               'link_type',
                                               'link_lbl',
                                               'link_lnk',
                                               'link_lang')


    Then use a foreach statement to loop through them.

    --
    cyber_wolf
    www.bkbsolutions.com - My Zikula module development site.
  • Posted: 19.07.2006, 21:23
     
    alarconcepts
    rank:
    Professional Professional
    registered:
     April 2004
    Status:
    offline
    last visit:
    21.01.08
    Posts:
    2723

    cyber_wolf

    If you don't know how many rows there will be, you can use arrays.


    The method I posted is /somewhat/ the same, but uses less code.

    :)


    --
    Photography | PHP | Other
  • Posted: 20.07.2006, 07:13
     
    cyber_wolf
    rank:
    Helper Helper
    registered:
     June 2002
    Status:
    offline
    last visit:
    09.09.08
    Posts:
    288
    We both were writing our methods at the same time; but yes, your way would be more efficient. I would have suggested that method, but I think I read too much into rowjow's question. I thought he was asking how to do 'x' rows with 5 fields per row and my method has been the only way I have come up for that type of a complex form. I've used your way many times myself for simple 1 field per row forms.

    --
    cyber_wolf
    www.bkbsolutions.com - My Zikula module development site.

Extensions Moderation

Main Menu

Extensions Database

Documentation

Development

Login

Donate to Zikula