Forum Moderators: coopster
class Books_Widget extends WP_Widget {
function Books_Widget() {
$widget_ops = array( 'classname' => 'books_widget', 'description' => __('A widget to display a list of books and their links to purchase') );
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'books-widget' );
$this->WP_Widget( 'books-widget', __('Books Widget', 'books'), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract($args);
$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
$booktext = apply_filters('books_widget', $instance['booktext'] );
$booktitle = $instance['booktitle'];
$bookimg = $instance['bookimg'];
$url = $instance['url'];
echo $before_widget;
if (!empty( $title ))
echo $before_title . $title . $after_title;
if(!empty($booktext))
echo $instance['booktext'] ? wpautop($booktext) : $booktext;
//foreach
if (!empty( $url)) {
echo '<a href="' . $instance['url'] . '">';
if(!empty($bookimg)) {
echo '<img src="' . $instance['bookimg'] . '"';
if(!empty($booktitle))
echo ' alt="' . $instance['booktitle'] . '"';
else
echo 'alt=""';
echo ' />';
}
echo '<br />Click to Order</a>';
} else {
if(!empty($bookimg))
echo $instance['bookimg'];
}
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['booktext'] = stripslashes( $new_instance['booktext'] );
$instance['booktitle'] = strip_tags( $new_instance['booktitle'] );
$instance['url'] = strip_tags( $new_instance['url'] );
$instance['bookimg'] = strip_tags( $new_instance['bookimg'] );
return $instance;
print_r($instance['title'][$i]);
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => 'Books Authored by Joel', 'booktext' => 'View one of these five powerful and informative books:', 'booktitle' => '', 'bookimg' => '', 'url' => '' ) );
$title = strip_tags($instance['title']);
$booktitle = strip_tags($instance['booktitle']);
$url = strip_tags($instance['url']);
$bookimg = strip_tags($instance['bookimg']);
$booktext = format_to_edit($instance['booktext']);
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'hybrid'); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" /></p>
<p><label for="<?php echo $this->get_field_id( 'booktext' ); ?>"><?php _e('Section description:', 'books'); ?></label>
<textarea class="widefat" rows="3" cols="20" id="<?php echo $this->get_field_id('booktext'); ?>" name="<?php echo $this->get_field_name('booktext'); ?>"><?php echo $booktext; ?></textarea></p>
<?php $n=0;
for($i=0; $i<5; $i++) {
if($n == '1')
$class = ' background-color:#eee; padding:5px 0; margin:5px 0;';
else
$class = '';
$output[$i] = '<span style="display:block;' . $class . '">' . "\n";
$output[$i] .= '<label style="padding-top:2px; width:90px; text-align:right; font-size:0.9em; float:left;" for="' . $this->get_field_id( 'booktitle' ) . '">' . __('Title of Book: ', 'books') . '</label>' . "\n";
$output[$i] .= '<input style="width:200px;" id="' . $this->get_field_id( 'booktitle' ) . '" name="' . $this->get_field_name( 'booktitle' ) . '" value="' . $instance['booktitle'] . '" />' . "\n";
$output[$i] .= '<label style="padding-top:2px; width:90px; text-align:right; font-size:0.9em; float:left; clear:left;" for="' . $this->get_field_id( 'url' ) . '">' . __('URL to Purchase: ', 'books') . '</label>' . "\n";
$output[$i] .= '<input style="width:200px;" id="' . $this->get_field_id( 'url' ) . '" name="' . $this->get_field_name( 'url' ) . '" value="' . $instance['url'] . '" />' . "\n";
$output[$i] .= '<p><label style="padding-top:2px; width:90px; text-align:right; font-size:0.9em; float:left; clear:left;" for="' . $this->get_field_id( 'bookimg' ) . '">' . __('Path to Image: ', 'books') . '</label>' . "\n";
$output[$i] .= '<input style="width:200px;" id="' . $this->get_field_id( 'bookimg' ) . '" name="' . $this->get_field_name( 'bookimg' ) . '" value="' . $instance['bookimg'] . '" /></p>' . "\n";
$n++;
if($n == '2') $n=0;
$output[$i] .= '</span>' . "\n";
echo $output[$i];
}
}
}
[edited by: jatar_k at 5:27 pm (utc) on Sep. 21, 2009]
[edit reason] fixed code tags [/edit]
When I remove the "for" loop, it works just fine - but I only have one set of form fields. I need 5 of the same form fields - but because it's in an array, something's not being seen correctly, and I'm stumped as to how to fix it.
if the output html is fine then on to the beginning of the processing script to see if it is getting the posted data
if it is then you need to step through and see where it drops it
I've found an alternate workaround for the time being, but I've been stumped by this. I don't like to be stumped. So as soon as my calendar clear, I'm pretty sure this'll be a pet project until I figure it out!
but in any case, I *think* the reason it's not working is because the forms are being populated as an array. That's not something I'm used to doing. Usually, I do each form field on it's own, but I'm trying to get better with my PHP skills. Since it's the same 3 fields, populated 5 times, I figured it as cleaner and neater to write to form fields one, and do a for loop to populate them 5 times. I think my issue is understanding how the variable work, really - I understand it when it's a single set of fields, but when they are part of an array, my brain is clouding over on how, exactly, to access that information. For example, for a *single* set of fields, I know exactly how to access the info: $instance['booktitle'], for example, will access the $booktitle. My issue is in the fact that now it's part of an array - so is it now "$instance['booktitle'][$i]" or "$instance['booktitle' . $i]"? I've tried both, and nothing with one, and with the other I managed to pull the *key* into the filed, but not the value.
Anyway - shelved as a pet project I guess. but if anyone has input on multi-field creation with a for loop, and how to access those arrays of values, that would be great - it's just blocking me.
So what I *think* is happening is that I have an array within an array - if that makes sense (multidimensional array?) o if I take out toe for Loop, it works just fine. The $instance is an array that can be used multiple times. However, now the forms are an array in and of themselves - which I think is where I'm getting confused. So now, say I have instance[1] for one sidebar, and instance [2] on a different one. My issue is how to access the "booktitle" because normally, with just *one* set of form fields, it's just "instance['booktitle']" - which would be for the single instance. But now I need to figure out how to grab something like $instance['output[1]=>booktitle'] - so that the array values within the instance are pulled out correctly. (I know that syntax isn't right, BTW LOL)
Does that make any sense at all? Sorry about the whole explaining thing. I'm self-taught in PHP, and this is, I think, something at a level that just might be over my head - but I'm willing to learn!
you could do this
echo '<pre>';
print_r($instance);
echo '</pre>';
that will dump the whole $instance array in a nice readable way, that should show you where the data you need is, if it isn't there then something else is going on