0

I try to develop a widget which enabels the functionality of creating tables. It is important that the user can determine how many columns and lines he wants. I already managed to generate a user-specific amount of input fields in the backend. My problem is that I am not able to save the content from the input fields. For that I am using a for-loop within the update method.

I hope someone can guide me into the right direction.

Tank you very much!

My Code:

class table_widget extends WP_Widget {

function __construct() {
$widget_ops = array(
  'classname' => 'table_det_widget',
  'description' => 'Tabelle erstellen',
);
parent:: __construct(false, $name = __('Tabelle'), $widget_ops);
}

function form($instance) { /*Ausgabe im Backend*/

echo '<label for="' . $this->get_field_id('wp_table_title') . '">Überschrift: </label>';
echo '<input class="widefat" type="text" value="' . $instance['wp_table_title'] .'" name="' . $this->get_field_name('wp_table_title') . '" id="' . $this->get_field_id('wp_table_title') . '" >';

echo '<label for="' . $this->get_field_id('wp_table_subtitle') . '">Unterüberschrift: </label>';
echo '<input class="widefat" type="text" value="' . $instance['wp_table_subtitle'] .'" name="' . $this->get_field_name('wp_table_subtitle') . '" id="' . $this->get_field_id('wp_table_subtitle') . '" >';

echo '<label for="' . $this->get_field_id('wp_column') . '">Spalten: </label>';
echo '<input class="widefat" type="text" value="' . $instance['wp_column'] .'" name="' . $this->get_field_name('wp_column') . '" id="' . $this->get_field_id('wp_column') . '" >';

echo '<label for="' . $this->get_field_id('wp_line') . '">Zeilen pro Spalte: </label>';
echo '<input class="widefat" type="text" value="' . $instance['wp_line'] .'" name="' . $this->get_field_name('wp_line') . '" id="' . $this->get_field_id('wp_line') . '" >';

if($instance['wp_column'] == '' || $instance['wp_line'] == '') {
    echo 'Bitte beide Felder ausfüllen!';
} else {
    $spalten = $instance['wp_column'];
    $zeilen = $instance['wp_line'];

    for ($i = 1; $i <= $zeilen; $i++) {
        echo "<label style='display: block; margin-top: 10px;'>Zeile {$i}</label>";

        for ($j = 1; $j <= $spalten; $j++) {
            $instSpecLine = $instance["spec_line_{$i}_{$j}"];
            echo "<input class='widefat' type='text' value='{$instSpecLine}' name='{$instSpecLine}' id='{$instSpecLine}' placeholder='Spalte {$j}' >";
        }

        $j = 0;
    }
}
}

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

$instance['wp_table_title'] = $new_instance['wp_table_title'];
$instance['wp_table_subtitle'] = $new_instance['wp_table_subtitle'];

$instance['wp_line'] = $new_instance['wp_line'];
$instance['wp_column'] = $new_instance['wp_column'];

$spalten = $instance['wp_column'];
$zeilen = $instance['wp_line'];

for ($i = 1; $i <= $zeilen; $i++) {
    for ($j = 1; $j <= $spalten; $j++) {

        $instSpecLine = "spec_line_{$i}_{$j}";

        $instance[$instSpecLine] = $new_instance[$instSpecLine];

    }
}   

return $instance;
}

1 Answer 1

0

Please replace below line:

echo "<input class='widefat' type='text' value='{$instSpecLine}' name='{$instSpecLine}' id='{$instSpecLine}' placeholder='Spalte {$j}' >";

With Below code:

echo "<input class='widefat' type='text' value='{$instSpecLine}' name='".$this->get_field_name("spec_line_{$i}_{$j}")."' id='{$instSpecLine}' placeholder='Spalte {$j}' >";

Hope this will help you

1
  • Thank you very much for the quick answer! Can't believe I made this mistake...works like a charm now!
    – F16R
    Commented Dec 29, 2016 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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