Forum Moderators: coopster

Message Too Old, No Replies

PHP complicated array for forms

forms are pretty, but it's not retaining any values

         

doodlebee

8:48 pm on Sep 20, 2009 (gmt 0)

10+ Year Member



I was wondering if someone could help me with the below code. I've got the forms looking great, but pretty doesn't mean functional. Right now, any input I put in the form fields isn't retained. I cannot figure out what I'm doing wrong - been trying for over a week and I'm totally stumped on this. Could anyone give me a nudge in the right direction?

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]

jatar_k

3:20 pm on Sep 21, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



any input I put in the form fields isn't retained

not sure what that means, retained by what?

not posted?
not showing when you error out?
not showing in the form at all?

doodlebee

3:44 pm on Sep 21, 2009 (gmt 0)

10+ Year Member



No, the form is showing, but any input I enter in the form fields doesn't do anything. It's supposed to place the values in the database, and then return the form with the values in the fields. It comes back empty every time. So basically, any input I place in the form fields isn't retained - doesn't get written to the database and the forms come back empty.

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.

jatar_k

5:05 pm on Sep 21, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



have you viewed the source of the generated page in the browser? that usually can provide a clue, especially if it all works without the for loop

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

doodlebee

5:17 pm on Sep 21, 2009 (gmt 0)

10+ Year Member



Yeah, that's where I'm having issues. I've already done all that - but have come up empty. I was just hoping someone could be an extra set of eyes and see what I might be missing.

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.

jatar_k

6:06 pm on Sep 21, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you would access it exactly the same way, using $instance['booktitle'], but what do you do between outputs normally to change what's in $instance?

doodlebee

6:13 pm on Sep 21, 2009 (gmt 0)

10+ Year Member



Well, if you couldn't tell before (and you probably could LOL) this is for a wordpress widget. The $instance basically allows for another instance of the widget within WordPress. That way, if you use the widget in a sidebar, you'll be able to use it again in another instance on a different sidebar (it used to be that when you used 1 widget in a sidebar, then that was done - that's all you got. Not so much anymore).

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!

jatar_k

6:26 pm on Sep 21, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hard to say, I get the explanation but I don't have access to all the data so you mainly need to look at what you are working with to figure out how to access it

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