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?