Creating data array from a php table

Asked

Viewed 81 times

-2

How to save data from a table so that each row follows the column date. Example: Line1 col1 = descricao1 and line1 col2= valor1 and col2linha0= data1 saving > descricao1 + valor1 + data1 How to create an array?

//todos os dados recebidos dos inputs
//recebendo datas de cada coluna
    $dats1 = $_POST['dats1'];
    $dats2 = $_POST['dats2'];
    $dats3 = $_POST['dats3'];
    $dats4 = $_POST['dats4'];
    $dats5 = $_POST['dats5'];
    $dats6 = $_POST['dats6'];
    $dats7 = $_POST['dats7'];
    $dats8 = $_POST['dats8'];

    //Todo esquema para linha 1
    $a1 = $_POST['a1'];
    $a2 = $_POST['a2'];
    $a3 = $_POST['a3'];
    $a4 = $_POST['a4'];
    $a5 = $_POST['a5'];
    $a6 = $_POST['a6'];
    $a7 = $_POST['a7'];
    $a8 = $_POST['a8'];
    $desc1 = $_POST['desc1'];

        //Todo esquema para linha 2
    $b1 = $_POST['b1'];
    $b2 = $_POST['b2'];
    $b3 = $_POST['b3'];
    $b4 = $_POST['b4'];
    $b5 = $_POST['b5'];
    $b6 = $_POST['b6'];
    $b7 = $_POST['b7'];
    $b8 = $_POST['b8'];
    $desc2 = $_POST['desc2'];
        //Todo esquema para linha 3
    $c1 = $_POST['c1'];
    $c2 = $_POST['c2'];
    $c3 = $_POST['c3'];
    $c4 = $_POST['c4'];
    $c5 = $_POST['c5'];
    $c6 = $_POST['c6'];
    $c7 = $_POST['c7'];
    $c8 = $_POST['c8'];
    $desc3 = $_POST['desc3'];
        //Todo esquema para linha 4
    $d1 = $_POST['d1'];
    $d2 = $_POST['d2'];
    $d3 = $_POST['d3'];
    $d4 = $_POST['d4'];
    $d5 = $_POST['d5'];
    $d6 = $_POST['d6'];
    $d7 = $_POST['d7'];
    $d8 = $_POST['d8'];
    $desc4 = $_POST['desc4'];
        //Todo esquema para linha 5
    $e1 = $_POST['e1'];
    $e2 = $_POST['e2'];
    $e3 = $_POST['e3'];
    $e4 = $_POST['e4'];
    $e5 = $_POST['e5'];
    $e6 = $_POST['e6'];
    $e7 = $_POST['e7'];
    $e8 = $_POST['e8'];
    $desc5 = $_POST['desc5'];
        //Todo esquema para linha 6
    $f1 = $_POST['f1'];
    $f2 = $_POST['f2'];
    $f3 = $_POST['f3'];
    $f4 = $_POST['f4'];
    $f5 = $_POST['f5'];
    $f6 = $_POST['f6'];
    $f7 = $_POST['f7'];
    $f8 = $_POST['f8'];
    $desc6 = $_POST['desc6'];
        //Todo esquema para linha 7
    $g1 = $_POST['g1'];
    $g2 = $_POST['g2'];
    $g3 = $_POST['g3'];
    $g4 = $_POST['g4'];
    $g5 = $_POST['g5'];
    $g6 = $_POST['g6'];
    $g7 = $_POST['g7'];
    $g8 = $_POST['g8'];
    $desc7 = $_POST['desc7'];
        //Todo esquema para linha 8
    $h1 = $_POST['h1'];
    $h2 = $_POST['h2'];
    $h3 = $_POST['h3'];
    $h4 = $_POST['h4'];
    $h5 = $_POST['h5'];
    $h6 = $_POST['h6'];
    $h7 = $_POST['h7'];
    $h8 = $_POST['h8'];
    $desc8 = $_POST['desc8'];

    //Todo esquema para linha 9
    $i1 = $_POST['i1'];
    $i2 = $_POST['i2'];
    $i3 = $_POST['i3'];
    $i4 = $_POST['i4'];
    $i5 = $_POST['i5'];
    $i6 = $_POST['i6'];
    $i7 = $_POST['i7'];
    $i8 = $_POST['i8'];
    $desc9 = $_POST['desc9'];

    //Todo esquema para linha 10
    $j1 = $_POST['j1'];
    $j2 = $_POST['j2'];
    $j3 = $_POST['j3'];
    $j4 = $_POST['j4'];
    $j5 = $_POST['j5'];
    $j6 = $_POST['j6'];
    $j7 = $_POST['j7'];
    $j8 = $_POST['j8'];
    $desc10 = $_POST['desc10'];

    //Todo esquema para linha 11
    $k1 = $_POST['k1'];
    $k2 = $_POST['k2'];
    $k3 = $_POST['k3'];
    $k4 = $_POST['k4'];
    $k5 = $_POST['k5'];
    $k6 = $_POST['k6'];
    $k7 = $_POST['k7'];
    $k8 = $_POST['k8'];
    $desc11 = $_POST['desc11'];

    //Todo esquema para linha 12
    $l1 = $_POST['l1'];
    $l2 = $_POST['l2'];
    $l3 = $_POST['l3'];
    $l4 = $_POST['l4'];
    $l5 = $_POST['l5'];
    $l6 = $_POST['l6'];
    $l7 = $_POST['l7'];
    $l8 = $_POST['l8'];
    $desc12 = $_POST['desc12'];

Any ideas for me to improve this code?

  • By your question, I can not understand what you are wanting. Please edit the question, being more specific. Otherwise it shall be closed.

  • I think now will be better understanding @Wallace Maxters just edited.

  • @Ezer post your form where all this data comes from.

  • as it did not fit all the code I posted in jsfiddle

  • You need to clarify what the problem is?

  • @Jorge B. the problem has been solved

Show 1 more comment

1 answer

1


Change that:

//todos os dados recebidos dos inputs
//recebendo datas de cada coluna
$dats1 = $_POST['dats1'];
$dats2 = $_POST['dats2'];
$dats3 = $_POST['dats3'];
$dats4 = $_POST['dats4'];
$dats5 = $_POST['dats5'];
$dats6 = $_POST['dats6'];
$dats7 = $_POST['dats7'];
$dats8 = $_POST['dats8'];

//Todo esquema para linha 1
$a1 = $_POST['a1'];
$a2 = $_POST['a2'];
$a3 = $_POST['a3'];
$a4 = $_POST['a4'];
$a5 = $_POST['a5'];
$a6 = $_POST['a6'];
$a7 = $_POST['a7'];
$a8 = $_POST['a8'];
$desc1 = $_POST['desc1'];

//Todo esquema para linha 2
$b1 = $_POST['b1'];
$b2 = $_POST['b2'];
$b3 = $_POST['b3'];
$b4 = $_POST['b4'];
$b5 = $_POST['b5'];
$b6 = $_POST['b6'];
$b7 = $_POST['b7'];
$b8 = $_POST['b8'];
$desc2 = $_POST['desc2'];

//Todo esquema para linha 3
$c1 = $_POST['c1'];
$c2 = $_POST['c2'];
$c3 = $_POST['c3'];
$c4 = $_POST['c4'];
$c5 = $_POST['c5'];
$c6 = $_POST['c6'];
$c7 = $_POST['c7'];
$c8 = $_POST['c8'];
$desc3 = $_POST['desc3'];

//Todo esquema para linha 4
$d1 = $_POST['d1'];
$d2 = $_POST['d2'];
$d3 = $_POST['d3'];
$d4 = $_POST['d4'];
$d5 = $_POST['d5'];
$d6 = $_POST['d6'];
$d7 = $_POST['d7'];
$d8 = $_POST['d8'];
$desc4 = $_POST['desc4'];

//Todo esquema para linha 5
$e1 = $_POST['e1'];
$e2 = $_POST['e2'];
$e3 = $_POST['e3'];
$e4 = $_POST['e4'];
$e5 = $_POST['e5'];
$e6 = $_POST['e6'];
$e7 = $_POST['e7'];
$e8 = $_POST['e8'];
$desc5 = $_POST['desc5'];

//Todo esquema para linha 6
$f1 = $_POST['f1'];
$f2 = $_POST['f2'];
$f3 = $_POST['f3'];
$f4 = $_POST['f4'];
$f5 = $_POST['f5'];
$f6 = $_POST['f6'];
$f7 = $_POST['f7'];
$f8 = $_POST['f8'];    
$desc6 = $_POST['desc6'];

//Todo esquema para linha 7
$g1 = $_POST['g1'];
$g2 = $_POST['g2'];
$g3 = $_POST['g3'];
$g4 = $_POST['g4'];
$g5 = $_POST['g5'];
$g6 = $_POST['g6'];
$g7 = $_POST['g7'];
$g8 = $_POST['g8'];
$desc7 = $_POST['desc7'];

//Todo esquema para linha 8
$h1 = $_POST['h1'];
$h2 = $_POST['h2'];
$h3 = $_POST['h3'];
$h4 = $_POST['h4'];
$h5 = $_POST['h5'];
$h6 = $_POST['h6'];
$h7 = $_POST['h7'];
$h8 = $_POST['h8'];
$desc8 = $_POST['desc8'];

//Todo esquema para linha 9
$i1 = $_POST['i1'];
$i2 = $_POST['i2'];
$i3 = $_POST['i3'];
$i4 = $_POST['i4'];
$i5 = $_POST['i5'];
$i6 = $_POST['i6'];
$i7 = $_POST['i7'];
$i8 = $_POST['i8'];
$desc9 = $_POST['desc9'];

//Todo esquema para linha 10
$j1 = $_POST['j1'];
$j2 = $_POST['j2'];
$j3 = $_POST['j3'];
$j4 = $_POST['j4'];
$j5 = $_POST['j5'];
$j6 = $_POST['j6'];
$j7 = $_POST['j7'];
$j8 = $_POST['j8'];
$desc10 = $_POST['desc10'];

//Todo esquema para linha 11
$k1 = $_POST['k1'];
$k2 = $_POST['k2'];
$k3 = $_POST['k3'];
$k4 = $_POST['k4'];
$k5 = $_POST['k5'];
$k6 = $_POST['k6'];
$k7 = $_POST['k7'];
$k8 = $_POST['k8'];
$desc11 = $_POST['desc11'];

//Todo esquema para linha 12
$l1 = $_POST['l1'];
$l2 = $_POST['l2'];
$l3 = $_POST['l3'];
$l4 = $_POST['l4'];
$l5 = $_POST['l5'];
$l6 = $_POST['l6'];
$l7 = $_POST['l7'];
$l8 = $_POST['l8'];
$desc12 = $_POST['desc12'];

To:

for ($i = 1; $i < 8; $i++) {


    $dats[$i] = $_POST['dats'.$i];
    $a[$i] = $_POST['a'.$i];
    $b[$i] = $_POST['b'.$i];
    $c[$i] = $_POST['c'.$i];
    $d[$i] = $_POST['d'.$i];
    $e[$i] = $_POST['e'.$i];
    $f[$i] = $_POST['f'.$i];
    $g[$i] = $_POST['g'.$i];
    $h[$i] = $_POST['h'.$i];
    $i[$i] = $_POST['i'.$i];
    $j[$i] = $_POST['j'.$i];
    $k[$i] = $_POST['k'.$i];
    $l[$i] = $_POST['l'.$i];  
}

$desc1 = $_POST['desc1'];
$desc2 = $_POST['desc2'];
$desc3 = $_POST['desc3'];
$desc4 = $_POST['desc4'];
$desc5 = $_POST['desc5'];
$desc6 = $_POST['desc6'];
$desc7 = $_POST['desc7'];
$desc8 = $_POST['desc8'];
$desc9 = $_POST['desc9'];
$desc10 = $_POST['desc10'];
$desc11 = $_POST['desc11'];
$desc12 = $_POST['desc12'];

This way will already reduce the lines. And do not forget that this way is with array so when inserting the variables to the array for example:

$a1 para $a[1]; 
  • hum understand will help enough thanks for the tip

  • What a good friend, I’m glad to know you will help. Thanks guy hug and good luck on your project there.

  • and also for ($x = 1; $x< 8; $x++) ģ $desc[$x] = $_POST['desc'. $x]; } Valew my brhoter at the tip of more than 1000 lines have now reduced rs.

  • Because of this error Cannot use a scalar value as an array @Viniam

Browser other questions tagged

You are not signed in. Login or sign up in order to post.