2

I have following code in my plugin of Wordpress:

wp_localize_script('ffd_js_script', 'myAjax', array(
            'ajaxurl' => admin_url('admin-ajax.php'),
            'idinfo' => $myoptionValue[idinfo],
            'index1' => $myoptionValue[id1],
            'index2' => $myoptionValue[id2]
            )
        );

I want to replace

'index1' => $myoptionValue[id1],
'index2' => $myoptionValue[id2]

with

for($i=1; $i<= $myoptionValue[fieldcount]; $i++)
        {
            $arguments .= ',"index"'.$i.'=>'.$myoptionValue[id.$i];
        }

So that I have

        wp_localize_script('ffd_js_script', 'myAjax', array(
            'ajaxurl' => admin_url('admin-ajax.php'),
            'idinfo' => $myoptionValue[idinfo]
            $arguments
            )
        );

Apparently I'm thinking it might be as simple as this, but it isn't, where is my mistake?

EDIT:

full code:

require( plugin_dir_path( __FILE__ ) . 'menu.php');
require_once( plugin_dir_path( __FILE__ ) . 'getuser.php');

add_action( 'wp_enqueue_scripts', 'ffd_load_scripts' );

function ffd_load_scripts()
{
$myoption =  get_option( 'fixformdata_options' );
$myoptionValue = maybe_unserialize( $myoption );  

$arguments = array();
for($i=1; $i<= $myoptionValue[fieldcount]; $i++)
{
    $arguments['index'.$i] = $myoptionValue['id'.$i];
}

wp_register_script('ffd_js_script', WP_PLUGIN_URL.'/FixFormData/js/ffd_js_script.js', array('jquery'));
wp_localize_script('ffd_js_script', 'myAjax', merge_array(array(
    'ajaxurl' => admin_url('admin-ajax.php'),
    'idinfo' => $myoptionValue['idinfo']),$arguments)
    );

wp_enqueue_script('jquery');
wp_enqueue_script('ffd_js_script', plugin_dir_url(__FILE__) . 'js/ffd_js_script.js');

}

9
  • I strongly suspect micro-optimisation is at work here, $myoptionValue[id2] also looks like it would generate a PHP fatal error, do you mean $myoptionValue[$id2]? To truly condense this down you need to show more of the surrounding code Commented Aug 30, 2014 at 19:29
  • edited it in the op Commented Aug 30, 2014 at 19:40
  • Where are idinfo, id1 and id2 defined? Unless these are constants somewhere this is invalid PHP, I would expect your original code to generate fatal errors Commented Aug 30, 2014 at 19:42
  • They come from a database. It is valid PHP because it is working now. But now I have 2 arguments (index1 and index2) and if there is index115 I hardly can code all this so I want to iterate over it. Commented Aug 30, 2014 at 19:47
  • They aren't defined anywhere though, please show where the values come from, without knowing how they're defined, or what their values are, it's incredibly difficult to refactor your code. Are they constants? are they produced using a DEFINE statement? Even if you think those details aren't relevant, please provide them Commented Aug 30, 2014 at 19:51

1 Answer 1

0

There are 4 valid ways of defining a string, see this question for more details.

However in your case there is a special exception provided by PHP. Most users are unaware of it however, and it does leave ambiguities, as it's no longer possible to tell a string literal, a constant, or a define apart, making it very difficult for other programmers to read your code ( e.g. yourself in 9 months time ).

So your code:

for($i=1; $i<= $myoptionValue[fieldcount]; $i++)
{
    $arguments .= ',"index"'.$i.'=>'.$myoptionValue[id.$i];
}

Is trying to append a string to the end of an array, which doesn't work. Arrays aren't strings, and you can only use the .= and . operators on strings. Also $myoptionValue[id.$i] violates the special case situation as the expression is now ambiguous ( does it mean 'id.$i', 'id'.$i, or "id.$i"? )

To add an item to an array you need to do one of the following:

$arr = array();
$arr['key'] = 'value';
$arr[] = 'value without a key';
array_push( $arr, 'value1', 'value2', 'value3', etc... );

So your loop should be:

for ( $i=1; $i <= $myoptionValue['fieldcount']; $i++ ) {
    $arguments['index'.$i] = $myoptionValue['id'.$i ];
}
2
  • This does not work, I have updated my code in the OP. Commented Aug 30, 2014 at 21:19
  • How does it not work? What did you expect to happen, and what actually happened? Can you verify that $myoptionValue[fieldcount] contains the expected value? Can you update your question with a full debug output of the contents of $myoptionValue? Commented Aug 30, 2014 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.