Wednesday, 28 August 2013

Foreach two array

Foreach two array

I was playing around with some code that I got here and what this code
does is auto generate new input for User ID and Location once the settings
is saved instead of having all input field already available (20 the
least). The code below output User ID, User ID and so on then Location
location and so on. What I am having problem is how to make it output User
ID, Location then User Id, Location... I know that it is only possible by
by combining the them in one foreach (not sure if that is really the
case).
public function form( $instance ) {
$user_id = isset ( $instance['user_id'] ) ? $instance['user_id'] :
array();
$user_id_num = count( $user_id );
$user_id[ $user_id_num + 1 ] = '';
$user_id_html = array();
$user_id_counter = 0;
foreach ( $user_id as $name => $value )
{
$user_id_html[] = sprintf(
'User ID<br/><input type="text" name="%1$s[%2$s]" value="%3$s"
class="widefat">',
$this->get_field_name( 'user_id' ),
$user_id_counter,
esc_attr( $value )
);
$user_id_counter += 1;
}
$user_loc = isset ( $instance['user_loc'] ) ? $instance['user_loc'] :
array();
$user_loc_num = count( $user_loc );
$user_loc[ $user_loc_num + 1 ] = '';
$user_loc_html = array();
$user_loc_counter = 0;
foreach ( $user_loc as $name => $value )
{
$user_loc_html[] = sprintf(
'Location<br/> <input type="text" name="%1$s[%2$s]"
value="%3$s" class="widefat">',
$this->get_field_name( 'user_loc' ),
$user_loc_counter,
esc_attr( $value )
);
$user_loc_counter += 1;
}
Now my question is.. How will I combine this code so that the arrays
doesn't work independently so when I enter new User ID and did not enter
Location, It will auto generate new input field for new User ID and
Location. Sorry Newbie here. :D

No comments:

Post a Comment