数组问题:注意:未定义的偏移量:0

时间:2018-04-23 05:35:27

标签: php html mysql json

我有这个PHP代码,我试图将HTML表转换为数组,然后进行mysqli查询,但我不知道如何

我有这个变量$aDataTableDetailHTML[][]在第一个[]中,表中有td的tr数,而在第二个[]中有td他们各自tr

的{1}}
<?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));


?>

这是HTML代码

<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>

这是OUTPUT

  

注意:/ opt / lampp / htdocs / ProyectoClasesPHP中的未定义偏移量:0   11-04-2018 / Guardar Factura.php第48行

     

注意:未定义的偏移量:/ opt / lampp / htdocs / ProyectoClasesPHP中的3   11-04-2018 / Guardar Factura.php第48行

     

注意:未定义的偏移量:/ opt / lampp / htdocs / ProyectoClasesPHP中的2   11-04-2018 / Guardar Factura.php第48行

     

注意:/ opt / lampp / htdocs / ProyectoClasesPHP中的未定义偏移量:0   11-04-2018 / Guardar Factura.php第48行

     

注意:未定义的偏移量:/ opt / lampp / htdocs / ProyectoClasesPHP中的3   11-04-2018 / Guardar Factura.php第48行

     

注意:未定义的偏移量:/ opt / lampp / htdocs / ProyectoClasesPHP中的2   11-04-2018 / Guardar Factura.php第48行

{"cadena":"INSERT INTO ItemXVenta (NroVenta, IdArticulo, Cantidad, ValorVenta) VALUES ('', '', '', ''),('', '', '', ''),"}

2 个答案:

答案 0 :(得分:0)

检查错误行中的Isset。 isset()函数用于检查变量是否已设置如果测试变量包含NULL值,则isset()函数返回false。

   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];
                }
            }
    }

答案 1 :(得分:0)

我解决了这个问题,固定代码看起来像这样:

<?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);

?>