0

So I have a widget Im making which allows users to update 4 switches. Each switch has the same fields, name, description, price, amount so I decided to build the widget using a for loop instead of hard coding each field to avoid repeating myself.

I cant figure out how to save the values tho, because from teh tuts I've looked at for building widgets, the name field has to be $this->get_field_name which produces something like

widget-switches-widget[4][name]

So my form() function looks like

for($i =0; $i<4;$i++){
    $name = $instance[$i]['name'];  
    $description = $instance[$i]['description'];    
    $amount = $instance[$i]['amount'];  
    $price = $instance[$i]['price'];
?>

    <div id="<?php echo $i; ?>" class="switch_form" style="background-color:<?php echo $i%2 == 0 ? '': '#DFDFDF' ;?>; border-radius:4px; padding:5px;">
        <h4>Switch # <?php echo $i+1;?> </h4>
        <p>
            <label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e('Name:', 'Name'); ?></label>
            <input style ="width:90%;" name="<?php echo $this->get_field_name($i); ?>['name']" value="<?php echo $name; ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'description' ); ?>"><?php _e('Description:', 'Description'); ?></label>
            <input style ="width:90%;" name="<?php echo $this->get_field_name($i); ?>['description']" value="<?php echo $description; ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'amount' ); ?>"><?php _e('Amount:', 'Amount'); ?></label>
            <input style ="width:90%;" name="<?php echo $this->get_field_name($i); ?>['amount']" value="<?php echo $amount; ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'price' ); ?>"><?php _e('Price:', 'Price'); ?></label>
            <input style ="width:90%;" name="<?php echo $this->get_field_name($i); ?>['price']" value="<?php echo $price; ?>" />
        </p>
    </div>
<?php
}

and my update() function looks like:

function update( $new_instance, $old_instance )
{
    $instance = $old_instance;

    for($i =0; $i<4;$i++){
        $instance[$i]['name'] = strip_tags( $new_instance[$i]['name'] );
        $instance[$i]['description'] = strip_tags( $new_instance[$i]['description'] );      
        $instance[$i]['amount'] = strip_tags( $new_instance[$i]['lights_fill'] );
        $instance[$i]['price'] = strip_tags( $new_instance[$i]['price'] );
    }
    return $instance;

}

How would I save these values into an array?

0

I am not sure I followed your code correctly, but you could try to set all data into one array:

name="unique_widget_name[<?php echo $this->get_field_name($i); ?>]['name']"

Very similar question, but not identical: How to store widget fields data as an array?

  • isnt that redundant? $this->get_field() is already an array, im passing $i in to build nested arrays for the fields. Basically, im trying to build and save 4 forms, each with teh same fields, in a single widget. – rugbert Apr 22 '13 at 18:49
  • You get just one array to check now: unique_widget_name. I think this is what you want to parse. But maybe I misunderstood your question. – fuxia Apr 22 '13 at 18:50
  • right, so i already have a unique array name, so all teh fields look like: widget-switches-widget[4][0][name], and widget-switches-widget[4][0][price]and widget-switches-widget[4][1][name] but they aren't saving – rugbert Apr 23 '13 at 13:17
  • Did you ever figure this out? Have the same issue. – Ben Sep 9 '18 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.