Announcement

Collapse
No announcement yet.

dynamic field

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • dynamic field

    Hello,
    I would like to increment a field name in a foreach
    ex:
    $ x = 0;
    foreach ({sel_ref} = $ ref)
    {
    $ X ++;
    {name $ x} = funct ($ x, a, b);
    }
    in order to have {name1} = 10800;
    {name2} = 108001;
    {...} = ...;
    thanks for your advices, ...

  • #2
    If this code was copied from your application then be aware that variable names are case sensitive in PHP. |So $X++ should be $x++
    I'm not sure however if you can dynamically form names and assign values to them. Might be with an eval .
    Albert Drent
    aducom software netherlands
    scriptcase partner, reseller, support and (turn-key) development
    www.scriptcase.eu / www.scriptcase.nl

    Comment


    • #3
      you are quite right, little mistake on my part when choosing the example,
      everything works except when I want to create an incremented field in a foreach,
      {field $ x} should become {field1}, {field2} to receive a value, because I have
      to create a switch to achieve it, a line in a foreach would be welcome: see example 1,2,3

      Comment


      • #4
        I think you can to create your 14 fields manually and they can be used for your need.

        Comment


        • #5
          that's the purpose, I created 14 fields that look at a similar pivot table (the bottom grid), the form view allows to edit data only to the selected student, the call in my foreach of a { field $ x} does not react, I am certainly doing wrong, here's what I want to do in a foreach see photo

          Comment


          • #6
            I think you can do it this way:.


            switch ($i) {
            case 1:
            {name1} = 108001;
            break;
            case 2:
            {name2} = 108005;
            break;
            case 3:
            {name3} = 108007;
            break;
            case ....
            }

            Comment


            • #7

              thank you,
              that's what I had to resign myself to do,
              I would have hoped to find a way to use an incremented variable instead of a field name, at this time,
              no longer need switch and case, possibility to make a dynamic field comme very useful, but .....,
              I need to study a lot,

              Comment


              • #8
                This post can help you. Try it.
                PHP allows you to use dynamic variable names, called variable variables. You can name a variable with the value stored in another variable. That is, one variable contains the name of another variable. For example, suppose you want to construct a variable named $city with the value Los Angeles. You can use the following statement:

                Comment


                • #9
                  i learn that now , thanks

                  Comment


                  • #10
                    alvagar How does that apply to the scriptcase field variables? Like {myfield} ? I would like to dynamically access the field names using variables.
                    In my case I have a form with over 100 fields on it, and I need to check if data exists in those fields. Rather than code 100 if {myfield} statements I want to read in the names of all the fields directly from that database... (I have that part working) and loop through the array checking each field kind of like above.

                    Any idea how I can accomplish this?

                    Example:
                    $fieldvar = 'myfield';

                    If( {$fieldvar} == ''){Echo $fieldvar." is emtpy";}

                    But this fails as {$fieldvar} isn't the way to do this.

                    Thanks!

                    Comment


                    • #11
                      the real difficulty is to be able to convert the concatenation of the chains into a variable already declared and recognized, the result is always identified as chain of text, it would be necessary to recover the id of the field and to concatenate this id with the increment, for the value of 0 to 100, just use range (0,100) as a loop value, I always manage to reproduce the concatenation, but never to read it as variable, my part statement is correct but the reading is only text: ex:
                      $ var [0] = {x1} (where x1 is the concatenated variable)

                      Comment


                      • #12
                        Hallo nsch2308,
                        maybe the following will guide you into a direction to solve your problem:

                        I created a form with four files, named field1, field2, field3, field4

                        Then created a js method named set_field with the parameters field_name and field_value.
                        It has one line of code:
                        Code:
                           document.getElementsByName(field_name)[0].value = field_value;
                        For testing I created a new button of type ajax:

                        PHP Code:
                        for($x=1;$x<=4;$x++)
                            {
                                
                        sc_ajax_javascript('set_field', array("field".$x,"Inhalt von Feld ".$x));
                            } 
                        If you run the form and press the button, all fields will change the value to 'Inhalt von Feld 1' and so on.

                        I hope this helps ;-)

                        Have a nice day !


                        Gunter Eibl
                        email@GunterEibl.de

                        asdw.de - Scriptcase & Open Source projects (German/English)
                        scriptcase.coach - professional coaching for Scriptcase users (German)
                        GunterEibl.de - About me (German)

                        Comment


                        • #13
                          thank you, this solution will be part of my references, after a lot of testing, I was finally able to create this little routine that corresponds exactly to what I expect and may be able to help "yourguide", thank you all for your advice ,
                          ///
                          $ x = 1;
                          foreach (range (1.14) as $ x)
                          {
                          $ txt = 'sp';
                          $ txt. = $ x;

                          $ val_field = $ _POST [$ txt];
                          echo $ val_field. '</ br>';
                          }
                          ///


                          Comment


                          • #14
                            thank you, this solution will be part of my references, after a lot of testing, I was finally able to create this little routine that corresponds exactly to what I expect and may be able to help "yourguide", thank you all for your advice ,
                            ///
                            $ x = 1;
                            foreach (range (1, 14) as $ x)
                            {
                            $ txt = 'sp';
                            $ txt. = $ x;

                            $ val_field = $ _POST [$ txt];
                            echo $ val_field. '</ br>';
                            }
                            ///
                            Carefully read (range (1,14)

                            Comment


                            • #15
                              nsch2308 , while it isn't directly what I need it does provide me other options on the subject and will help me determine my solution.
                              Thank you for posting your solution!

                              Comment

                              Working...
                              X