2
votes

i got this php code, i'm trying to convert a HTML table to a array and then make a mysqli query, but i don't know how

I have this variable $aDataTableDetailHTML[][] where in the first [] there is the number of tr with td in the table, and in the second [] there are the td of their respective tr

<?php
$htmlContent = file_get_contents("FrmClientes.php");

    $DOM = new DOMDocument();
    @$DOM->loadHTML($htmlContent);

    $Header = $DOM->getElementsByTagName('th');
    $Detail = $DOM->getElementsByTagName('td');

//#Get header name of the table
    foreach($Header as $NodeHeader) 
    {
            $aDataTableHeaderHTML[] = trim($NodeHeader->textContent);
    }
    //print_r($aDataTableHeaderHTML); die();

    //#Get row data/detail table without header name as key
    $i = 0;
    $j = 0;
    foreach($Detail as $sNodeDetail) 
    {
            $aDataTableDetailHTML[$j][] = trim($sNodeDetail->textContent);
            $i = $i + 1;
            $j = $i % count($aDataTableHeaderHTML) == 0 ? $j + 1 : $j;
    }
    //print_r($aDataTableDetailHTML); die();
//print_r($aDataTableDetailHTML[0][3]); die();

    //#Get row data/detail table with header name as key and outer array index as row number
    for($i = 0; $i < count($aDataTableDetailHTML); $i++)
    {
            for($j = 0; $j < count($aDataTableHeaderHTML); $j++)
            {
                    $aTempData[$i][$aDataTableHeaderHTML[$j]] = $aDataTableDetailHTML[$i][$j];
            }
    }
    $aDataTableDetailHTML = $aTempData; unset($aTempData);
    //print_r($aDataTableDetailHTML); die();

    //print_r($aDataTableDetailHTML[0][0]); die();

    $CON = mysqli_connect("localhost","root","","BDfactura") or die ("error");

    $cadena = "INSERT INTO ItemXVenta (NroVenta, IdArticulo, Cantidad, ValorVenta) VALUES ";

    for($k=0; $k < count($aDataTableDetailHTML); $k++)
    {
        $cadena.="('".null."', '".$aDataTableDetailHTML[$k][0]."', '".$aDataTableDetailHTML[$k][3]."', '".$aDataTableDetailHTML[$k][2]."'),";
    }

    echo json_encode(array('cadena' => $cadena));


?>

Here is the HTML code

<div class="table-responsive" id="adicionados">          
<table class="table" id="tabla">
<thead>
  <tr>
    <th>ID Articulo</th>
    <th>Descripcion</th>
    <th>Valor Venta</th>
    <th>Cantidad</th>
    <th>Subtotal</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td id="Sid">ID</td>
    <td id="Sdescripcion">Descripcion</td>
    <td id="Svlrventa">VLR</td>
    <td id="Scantidad">CANTIDAD</td>
    <td id="Ssubtotal">TOTAL</td>
  </tr>
  <tr>
    <td id="Sid">dw</td>
    <td id="Sdescripcion">ads</td>
    <td id="Svlrventa">ads</td>
    <td id="Scantidad">da</td>
    <td id="Ssubtotal">ad</td>
  </tr>

</tbody>
</table>
</div>

Here is the OUTPUT

Notice: Undefined offset: 0 in /opt/lampp/htdocs/ProyectoClasesPHP 11-04-2018/Guardar Factura.php on line 48

Notice: Undefined offset: 3 in /opt/lampp/htdocs/ProyectoClasesPHP 11-04-2018/Guardar Factura.php on line 48

Notice: Undefined offset: 2 in /opt/lampp/htdocs/ProyectoClasesPHP 11-04-2018/Guardar Factura.php on line 48

Notice: Undefined offset: 0 in /opt/lampp/htdocs/ProyectoClasesPHP 11-04-2018/Guardar Factura.php on line 48

Notice: Undefined offset: 3 in /opt/lampp/htdocs/ProyectoClasesPHP 11-04-2018/Guardar Factura.php on line 48

Notice: Undefined offset: 2 in /opt/lampp/htdocs/ProyectoClasesPHP 11-04-2018/Guardar Factura.php on line 48

{"cadena":"INSERT INTO ItemXVenta (NroVenta, IdArticulo, Cantidad, ValorVenta) VALUES ('', '', '', ''),('', '', '', ''),"}
2
if you want to remove the error, check isset()Siva Ganesh
Do a var_dump() on the variable and check what it actually contains.Magnus Eriksson
@SivaGanesh, How?Gammath.rar
I just post my answerSiva Ganesh

2 Answers

0
votes

Check Isset in your error line. The isset () function is used to check whether a variable is set or not The isset() function return false if testing variable contains a NULL value.

   for($i = 0; $i < count($aDataTableDetailHTML); $i++)
    {
            for($j = 0; $j < count($aDataTableHeaderHTML); $j++)
            {
                if(isset($aDataTableDetailHTML[$i][$j])){
                    $aTempData[$i][$aDataTableHeaderHTML[$j]] = $aDataTableDetailHTML[$i][$j];
                }
            }
    }
0
votes

I solved the problem, the fixed code looks like this:

<?php
$htmlContent = file_get_contents("FrmClientes.php");

    $DOM = new DOMDocument();
    @$DOM->loadHTML($htmlContent);

    $Header = $DOM->getElementsByTagName('th');
    $Detail = $DOM->getElementsByTagName('td');

//#Get header name of the table
    foreach($Header as $NodeHeader) 
    {
            $aDataTableHeaderHTML[] = trim($NodeHeader->textContent);
    }
    //print_r($aDataTableHeaderHTML); die();

    //#Get row data/detail table without header name as key
    $i = 0;
    $j = 0;
    foreach($Detail as $sNodeDetail) 
    {
            $aDataTableDetailHTML[$j][] = trim($sNodeDetail->textContent);
            $i = $i + 1;
            $j = $i % count($aDataTableHeaderHTML) == 0 ? $j + 1 : $j;
    }
    //print_r($aDataTableDetailHTML); die();
//print_r($aDataTableDetailHTML[0][3]); die();

    //#Get row data/detail table with header name as key and outer array index as row number
    for($i = 0; $i < count($aDataTableDetailHTML); $i++)
    {
            for($j = 0; $j < count($aDataTableHeaderHTML); $j++)
            {
                    $aTempData[$i][$aDataTableHeaderHTML[$j]] = $aDataTableDetailHTML[$i][$j];
            }
    }
    $aDataTableDetailHTML = $aTempData; unset($aTempData);
    //print_r($aDataTableDetailHTML); die();

    //print_r($aDataTableDetailHTML[0][0]); die();

    $CON = mysqli_connect("localhost","root","","BDfactura") or die ("error");

    $cadena = "INSERT INTO ItemXVenta (NroVenta, IdArticulo, Cantidad, ValorVenta) VALUES ";

    for($k=0; $k < count($aDataTableDetailHTML); $k++)
    {
        $cadena.="('".null."', '".$aDataTableDetailHTML[$k]["ID Articulo"]."', '".$aDataTableDetailHTML[$k]["Cantidad"]."', '".$aDataTableDetailHTML[$k]["Valor Venta"]."'),";
    }

    echo json_encode(array('cadena' => $cadena));

//var_dump($aDataTableDetailHTML);

?>