Announcement

Collapse
No announcement yet.

Accessing constructed variables

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

  • Accessing constructed variables

    Hi,

    I have couple of problems with accessing variables and could not find any relevant post on the subject. Appreciate if any one has faced these issues and found some workarounds:

    Background: I have a table with say 10 fields amount_1, amount_2, .... amount_10. and I need to access these fields using a for loop and assign some computed values.

    When I directly try {amount_1}=999; it works, however if I construct a variable for e.g.
    $i=1;
    $tmp='{amount_'.$i.'}=999;';
    and do an
    eval($tmp); -- it does not work. Further if I do an echo $tmp; -- it shows a space in between { braces -- like -- { amount_1}=999;

    This is strange SC is adding a blank space after {

    I have tried many events including OnLoad.

    Second issue is what is the best way to do a total of these fields -- which event.

    Apart from the above 10 amount fields I have a total field. How do I display the total in the form whenever any of these amount are changed / update.


    Regards,
    Chetan

  • #2
    anyone able to get eval work?

    below is my code and it doesn't take effect and no error as well

    $tmp = "{name_first}='abc';";
    eval($tmp);

    Comment


    • #3
      Originally posted by weilies View Post
      anyone able to get eval work?

      below is my code and it doesn't take effect and no error as well
      When there are issues, then eval will not return an error message. Also despite what you want to do I would try to find another solution, like putting the values in an array and process. To allow a program to put php code in a variable to be executed is a security risk and should be avoided at all times imho. See http://php.net/manual/en/function.eval.php.

      in your sample:

      $arr = array($amount_1, $amount2, %amount3);
      foreach($arr as $elem) {
      // do your thing.
      }
      Albert Drent
      aducom software netherlands
      scriptcase partner, reseller, support and (turn-key) development
      www.scriptcase.eu / www.scriptcase.nl

      Comment


      • #4
        hi, i managed to make it worked!
        here is the code

        $myname = "here is my name...";
        eval('{name_first} = \'' . $myname . '\';');

        i tried with many permutation and finally get one working! hurray!

        Thanks Albert

        Comment


        • #5
          hi Albert, i got stuck again... sorry for request further helps

          Example 1: working
          $field = 'name_first';
          $name = 'weilies';

          // code below able to update page's name_first field to 'weilies' once loaded
          eval('{name_first} = \'' . $name . '\';');
          Example 2: working
          $field = 'name_first';
          $name = 'weilies';

          $cmd = '{name_first} = \'' . $name . '\';';

          // code below able to update page's name_first field to 'weilies' once loaded
          eval($cmd);
          Example 3: not working
          $field = 'name_first';
          $name = 'weilies';

          $cmd = '{' . $field . '} = \'' . $name . '\';';

          // code below doesn't update field page
          eval($cmd);
          Dear gurus, i need some helps here. I can't tell what so different for both example 2 and 3, i need the field_name to be dynamically assign via value (example 3). But i have no luck to make it work.

          Comment


          • #6
            I don't know, I don't use eval. Why can't you use my approach of creating an array. It's safe and by far more conveniant imho. See: http://php.net/manual/en/language.types.array.php
            Albert Drent
            aducom software netherlands
            scriptcase partner, reseller, support and (turn-key) development
            www.scriptcase.eu / www.scriptcase.nl

            Comment


            • #7
              Check the generated code, that should make it clear why it doenst work (use view source).

              Comment


              • #8
                This information really useful.
                Bilingual services

                Comment


                • #9
                  Did you manage to solve Example 3?

                  good evenning

                  Did you manage to solve Example 3?

                  Thank You



                  Originally posted by weilies View Post
                  hi Albert, i got stuck again... sorry for request further helps

                  Example 1: working


                  Example 2: working


                  Example 3: not working


                  Dear gurus, i need some helps here. I can't tell what so different for both example 2 and 3, i need the field_name to be dynamically assign via value (example 3). But i have no luck to make it work.

                  Comment

                  Working...
                  X