Question

I am trying to take multiple input values (user will add as many row as needed via javascript)

echo '<td class="cat_id"><input type="number" name="cat_id[]"></td>';
echo '<td class="ln_ft"><input type="number" name="ln_ft[]"></td>';
echo '<td class="price"><input type="number" name="price[]"></td>';

echo '<td class="cat_id"><input type="number" name="cat_id[]"></td>';
echo '<td class="ln_ft"><input type="number" name="ln_ft[]"></td>';
echo '<td class="price"><input type="number" name="price[]"></td>';

I am trying to loop through and insert the rows.

if(isset($_POST)){

    $qty = count($_POST['cat_id']);

    for($i = 0; $i < $qty; $i++){
        echo '<pre>';
        foreach($_POST as $item){
            $test = $item[$i];
            var_dump($test);
        }
        echo '</pre>';
    }
}

I'll be doing a wordpress insert

$wpdb->insert( 'pricelist', 
                                            array(
                                                'cat_id' => $cat_id,
                                                'ln_ft' => $ln_ft,
                                                'price' => $price
                                            ), 
                                            array(
                                                '%d',
                                                '%d',
                                                '%d'
                                            )
                                        );

I cannot figure how to organize and separate the info I need for each row ($cat_id, $ln_ft, $price)

Was it helpful?

Solution

if(isset($_POST)){
    $qty = count($_POST['cat_id']);
    for($i = 0; $i < $qty; $i++){
        $wpdb->insert( 'pricelist', 
            array(
                'cat_id' => $_POST['cat_id'][$i],
                'ln_ft' => $_POST['ln_ft'][$i],
                'price' => $_POST['price'][$i]
            ), 
            array(
                '%d',
                '%d',
                '%d'
            )
        );
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top