Problems with arrays: Notice: Undefined offset: 0 - php
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 ('', '', '', ''),('', '', '', ''),"}
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];
}
}
}
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);
?>
Related
How should I display the result using HTML <table> tag in columns of 50 records each(except the last column)?
I've written a program which currently generates 540 different numbers. I want to display these 540 different numbers in a columns having 50 records each(except the last column where the number of records should be less than 50) using HTML <table> tag but I'm not able to do so as there are so many loops and conditions are getting applied. Following is my program : <?php function findFourElements() { $possible_numbers = ''; $sr_no = 0; $row_count = 0; for ($i = 0; $i <= 9; $i++) { for ($j = 0; $j <= 9; $j++) { for ($k = 0; $k <= 9; $k++) { for ($l = 0; $l <= 9; $l++) { $possible_numbers[0] = $i; $possible_numbers[1] = $j; $possible_numbers[2] = $k; $possible_numbers[3] = $l; if((int)$possible_numbers[0] + (int)$possible_numbers[1] + (int)$possible_numbers[2] + (int)$possible_numbers[3] === 14) { $sr_no++; echo $sr_no. ') ' . $possible_numbers[0].$possible_numbers[1].$possible_numbers[2].$possible_numbers[3]. '<br>'; } } } } } } findFourElements(); ?> Please somebody help me out with my logic.
You can use this code: <?php function findFourElements() { $response = []; $possible_numbers = ''; $sr_no = 0; $row_count = 0; for ($i = 0; $i <= 9; $i++) { for ($j = 0; $j <= 9; $j++) { for ($k = 0; $k <= 9; $k++) { for ($l = 0; $l <= 9; $l++) { $possible_numbers[0] = $i; $possible_numbers[1] = $j; $possible_numbers[2] = $k; $possible_numbers[3] = $l; if ((int)$possible_numbers[0] + (int)$possible_numbers[1] + (int)$possible_numbers[2] + (int)$possible_numbers[3] === 14) { $sr_no++; $response[] = $possible_numbers[0] . $possible_numbers[1] . $possible_numbers[2] . $possible_numbers[3]; } } } } } return $response; } $response = findFourElements(); $data = array_chunk($response, 50); $srNo = 1; ?> <table> <tr> <?php foreach ($data as $key => $value) { ?> <td> <table> <tr> <th>Sr. No.</th> <th>Vehicle No.</th> </tr> <?php foreach ($value as $val) { ?> <tr> <td> <?php echo $srNo ?> </td> <td> <?php echo $val ?> </td> </tr> <?php $srNo++; } ?> </table> </td> <?php } ?> </tr> </table> if you don't want to work with return response you can add extra code inside the function.
You should use a while statement. <!DOCTYPE html> <html> <head> ... </head> <body> <?php /** Calculations (# = amount) 540/50 = 10.8 (# of rows) 50*0.8 = 40 (# of records in last row) */ echo "<table>"; $x = 0; $ranNum = 0; echo "<tr>"; while ($x < 540) { /* a round number (1,2,3,4,5,...) %1 = 0. So (Number/50)%1 is only 0 if it's in the multiplication table of 50*/ if ((($x/50)%1) == 0) { echo "</tr>"; echo "<tr>"; } $ranNum = rand(1000,9999); // random number echo "<td id='Track".$x."'>".$ranNum."</td>"; $x++; } echo "</tr>"; echo "</table>"; ?> </body> </html>
Array is bigger then my XML tags? (PHP)
I want to add a child with a specified value which I get from my array. But my problem is that my array is bigger then my amount of XML products... This is why I get this error message: PHP Notice: Undefined offset: 1589 in C:\Users\Jan\PhpstormProjects\censored\test.php on line 63 PHP Fatal error: Uncaught Error: Call to a member function appendChild() on null in C:\Users\Jan\PhpstormProjects\censored\test.php:64 To check that I made two loops which say me that I have 1588 names and 1588 products, both loops say that. Thats logical! $markerProduct = $root->getElementsByTagName("product"); for($i = $markerProduct->length - 1; $i >= 0; $i--){ $productCounter = $productCounter + 1; } $markerTitle = $root->getElementsByTagName("name"); for($i = 0; $i < $markerTitle->length; $i++){ $nameCounter = $nameCounter + 1; } But my array in which I store the specified value for each product is 1589 (0 - 1588) values big... And I don't know. Can anyone help me and tell me the error? $searches = ["Steam", "Uplay", "Xbox", "Nintendo", "PS3", "PS4", "PSN"]; function isContaining($searches, $titleTag, $urlTag, $productTag, $path){ $dom = new DOMDocument('1.0', 'utf-8'); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; $dom->load($path); $root = $dom->documentElement; $markerTitle = $root->getElementsByTagName($titleTag); $markerURL = $root->getElementsByTagName($urlTag); $plat = array(); for($i = $markerTitle->length - 1; $i >= 0 ; $i--){ $title = $markerTitle->item($i)->textContent; $url = $markerURL->item($i)->textContent; $co = count($searches); $productFound = false; for($j = 0; $j < $co; $j++){ if(stripos($title, $searches[$j]) !== false){ if($j > 3){ array_push($plat, "PlayStation"); }else{ array_push($plat, $searches[$j]); } $productFound = true; }elseif(stripos($url, $searches[$j]) !== false){ if($j > 3){ array_push($plat, "PlayStation"); }else{ array_push($plat, $searches[$j]); } $productFound = true; } } if($productFound == false){ array_push($plat, "Nothing"); } } print_r($plat); $c = count($plat); echo $c; for($i = $c - 1; $i >= 0; $i--){ $node = $dom->createElement('plat', $plat[$c]); $dom->getElementsByTagName($productTag)->item($i)->appendChild($node); } $dom->saveXML(); $dom->save('data/gamesplanet2.xml'); } Error is in line 63: $node = $dom->createElement('plat', $plat[$c]); Greetings and Thank You!
I am geting Notice: Undefined offset: 14 in C:\nn\htdocs\myfiles\PHPCode.php on line 18
I am trying to find a string in other string. I am not allowed to use any of str function. Therefore, I wrote this code! But it shows Undefined offset: 14 in C:\nn\htdocs\myfiles\PHPCode.php on line 18. I do not know what's wrong with this line. if ($arr1[$j] == $arr2[$index]) The full code <?php $stringR = strtolower($_POST['name']); $first = strtolower($_POST['First']); $k = 0; $index =0; $firstfeq =0; if (preg_match('/[0-9]/', $stringR)){ echo "ERROR:The String has numbers"."<br>"; exit; } if (( strlen($first) > 8 )){ echo "ERROR: Number of char in each input should be less than 8 char"."<br>"; exit; } $arr1 = str_split($stringR); $arr2 = str_split($first); for ($j = 0 ; $j <= sizeof($arr1) ; $j++) { if ($arr1[$j] == $arr2[$index]){ if ($index <= (sizeof ($arr2))){ $k = 1; $index++; }else { $index = 0; $k =0; } if (($index == sizeof($arr2)) && ( $k == 1)) { $firstfeq++; $index =0; $k = 0; } } } echo "$first appears $firstfeq"."<br>"; ?>
Change this: for ($j = 0 ; $j <= sizeof($arr1) ; $j++) { to this: for ($j = 0 ; $j < sizeof($arr1) ; $j++) { I bet $j is going 1 index above the total limit.
Since Arrays are indexed from 0, you should replace this line for ($j = 0 ; $j <= sizeof($arr1) ; $j++) { with for ($j = 0 ; $j < sizeof($arr1) ; $j++) { and similarly if ($index <= (sizeof ($arr2))){ with if ($index < (sizeof ($arr2))){
Php Undefined Offset Error While Printing Table
I'm tried to call the variable but I get this error Notice: Undefined offset: 6 in /Applications/XAMPP/xamppfiles/htdocs/databasetwo/untitled.php on line 113 This code print a table to Html I want to do it programmatically so the array can print the amount selected. $id = "table" ; // echo "<table id=$id><tr><th>Title</th><th>Price</th><th>Number</th></tr>"; echo "<table id=$id>"; foreach($rows as $row){ echo "<tr>"; for ($i = 0 ; $i <= $amount ; $i++) { echo "<td>{$row[$i]}</td>"; /*echo "<td>{$row[1]}</td>"; echo "<td>{$row[2]}</td>"; echo "<td>{$row[3]}</td>"; echo "<td>{$row[4]}</td>"; echo "<td>{$row[5]}</td>"; echo "<td>{$row[6]}</td>"; */ } echo "</tr>"; } echo "</table>"; If I leave it $row[0], 1, 2 etc. Will Work. The error I'm getting is Notice: Undefined offset: 6 in /Applications/XAMPP/xamppfiles/htdocs/databasetwo/untitled.php on line 113
If you are starting from 0, make sure for ending criteria you add value - 1. for ($i = 0 ; $i <= $amount - 1 ; $i++) { Or remove = in loop like this, for ($i = 0 ; $i < $amount ; $i++) {
How to make php to print xml for this program? [duplicate]
This question already has answers here: How to generate XML file dynamically using PHP? (8 answers) Closed 8 years ago. friends i managed to make following output from a php code 0 0 1 0 2 0 0 0 0 0 0 0 0 3 1 0 0 1 0 2 0 0 0 1 0 0 0 3 2 0 0 0 0 0 0 0 0 2 0 0 0 0 0 3 0 3 0 0 0 1==>3 3==>4 4==>7 7==>2 3==>6 1==>5 <html> <?php // Start XML file, create parent node $dom = new DOMDocument("1.0"); $node = $dom->createElement("markers"); $parnode = $dom->appendChild($node); $result= array(array(0,0,1,0,2,0,0),array(0,0,0,0,0,0,3),array(1,0,0,1,0,2,0), array(0,0,1,0,0,0,3),array(2,0,0,0,0,0,0),array(0,0,2,0,0,0,0), array(0,3,0,3,0,0,0)); echo "<pre>"; for($k = 0; $k < 7; $k++){ for($j = 0; $j < 7; $j++){ echo $result[$k][$j],"\t"; } echo "\n<br>"; } $sum=0; for($i = 0; $i < 7; $i++){ for($j = 0; $j < 7; $j++){ $sum += $result[$i][$j]; } } while( $sum > 0) { $i = 0; while($i < 7){ $j=0; while($j<7) { hm: if($result[$i][$j]!=0) { echo $i+1; print "==>"; $result[$i][$j]=0; $result[$j][$i]=0; $i=$j; $j=0; echo $i+1; print " "; goto hm; } $j++; } $i++; } $sum=0; for($i = 0; $i < 7; $i++){ for($j = 0; $j < 7; $j++){ $sum += $result[$i][$j]; } } } //.................................................................. ?> </html> EXPECTED OUTPUT <markers> <marker from="1" to="3"/> <marker from="3" to="4"/> <marker from="4" to="7"/> <marker from="7" to="2"/> <marker from="3" to="6"/> <marker from="1" to="5"/> </markers>
<?php header('Content-type: text/xml'); /* create a dom document with encoding utf8 */ $domtree = new DOMDocument('1.0', 'UTF-8'); /* create the root element of the xml tree */ $xmlRoot = $domtree->createElement("xml"); /* append it to the document created */ $xmlRoot = $domtree->appendChild($xmlRoot); $currentTrack = $domtree->createElement("track"); $currentTrack = $xmlRoot->appendChild($currentTrack); $result= array(array(0,0,1,0,2,0,0),array(0,0,0,0,0,0,3),array(1,0,0,1,0,2,0), array(0,0,1,0,0,0,3),array(2,0,0,0,0,0,0),array(0,0,2,0,0,0,0), array(0,3,0,3,0,0,0)); /*echo "<pre>"; for($k = 0; $k < 7; $k++){ for($j = 0; $j < 7; $j++){ echo $result[$k][$j],"\t"; } echo "\n<br>"; }*/ $sum=0; for($i = 0; $i < 7; $i++){ for($j = 0; $j < 7; $j++){ $sum += $result[$i][$j]; } } while( $sum > 0) { $i = 0; while($i < 7){ $j=0; while($j<7) { hm: if($result[$i][$j]!=0) { /* you should enclose the following two lines in a cicle */ $currentTrack->appendChild($domtree->createElement('from',$i+1)); $result[$i][$j]=0; $result[$j][$i]=0; $i=$j; /* you should enclose the following two lines in a cicle */ $currentTrack->appendChild($domtree->createElement('to',$j+1)); $j=0; goto hm; } $j++; } $i++; } $sum=0; for($i = 0; $i < 7; $i++){ for($j = 0; $j < 7; $j++){ $sum += $result[$i][$j]; } } } /* get the xml printed */ echo $domtree->saveXML(); //.................................................................. ?> output <xml> <track> <from>1</from> <to>3</to> <from>3</from> <to>4</to> <from>4</from> <to>7</to> <from>7</from> <to>2</to> <from>3</from> <to>6</to> <from>1</from> <to>5</to> </track> </xml>