Php Undefined Offset Error While Printing Table - php

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++) {

Related

How to break a column to fix a php table [duplicate]

This question already has answers here:
PHP: How do you determine every Nth iteration of a loop?
(8 answers)
Closed last year.
I had a little problem, i cannot break the table because I'm looking for a 16 columns in these table to create a unicode table...
The aim is to find where i must break the line of the column as like as there:
<?php
$columnas = 16;
$filas = 16;
$word= 0;
$contador = 0;
print "<table border=\"1\">\n";
print "<caption>ASCII</caption>\n";
print "<tbody>\n";
print "<tr>\n";
for ($j = 1; $j <= $columnas; $j++){
if ($j%2 == 1){
print "<th>Codigo</th>\n";
}elseif ($j%2 == 0){
print "<th>Valor</th>\n";
}
}
print "</tr>";
for ($i = 1; $i <= $filas; $i++){
for($i = 1; $i <= 50000; $i++,$contador){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>" .$i. "</td>\n";
print "<td>".$unicodeChar."</td> \n";
}
print "</tr>\n";
}
print "</tbody>\n";
print "</table>\n";
?>
enter image description here
You used the modulus in the first loop, so use it again in the second
print "</tr>";
print "<tr>";
for($i = 1; $i <= 50000; $i++){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>$i</td><td>$unicodeChar</td>\n";
if ( $i % $columnas/2 ) == 0 ) {
print "</tr><tr>\n";
}
}
print "</tr>\n";
Thank you very much for this help, i have been fixed!!
here is the code that i fixed, with your idea :)
´´´´
<?php
$columnas = 16;
$filas = 16;
$word= 0;
$contador = 0;
print "<table border=\"1\">\n";
print "<caption>ASCII</caption>\n";
print "<tbody>\n";
print "<tr>\n";
for ($j = 1; $j <= $columnas; $j++){
if ($j%2 == 1){
print "<th>Codigo</th>\n";
}elseif ($j%2 == 0){
print "<th>Valor</th>\n";
}
}
print "</tr>";
for ($i = 1; $i <= $filas; $i++){
print "<tr>";
for($i = 1; $i <= 50000; $i++,$contador){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>" .$i. "</td>\n";
print "<td>".$unicodeChar."</td> \n";
if (($i % 8 ) == 0 ){
print "</tr><tr>";
}
}
print "</tr>\n";
}
print "</tbody>\n";
print "</table>\n";
?>
´´´´
Like Riggsfolly said but maybe divide the columns number by 2 before the modulo, because there are 2 TDs / iteration:
if ( $i % ($columnas/2) ) == 0 ) {
print "</tr><tr>\n";
}

two dimension array with assoc and for loop php

I write that code but i got error
$tablica = [
'xd'=>['jabłka', 'wiśnie', 'gruszki'],
'xd'=>['jabłska', 'wisssśnie', 'grussssszki']
];
$n=count($tablica);
echo "<table border=1>";
for($i = 0; $i <count($tablica); ++$i) {
echo "<tr>";
for($x = 0; $x < count($tablica[$i]); $x++){
echo "<td>".$tablica[$i][$x]."</td><br>";
}
echo "</tr>";
}
echo "</table>";
it's simple but when i add assoc for 1st dimension it's broke with that error
Notice: Undefined offset: 0 in C:\wamp64\www\projects\xd\1.php on line 10
Warning: count(): Parameter must be an array or an object that implements Countable in C:\wamp64\www\projects\xd\1.php on line 10
You are adding complexity when there is no need to.
When using associative arrays, foreach is usually a best way to iterate.
You can replace your code with:
$tablica = [
'xd'=>['jabłka', 'wiśnie', 'gruszki'],
'xd'=>['jabłska', 'wisssśnie', 'grussssszki']
];
echo "<table border=1>";
foreach($tablica as $tr) {
echo "<tr>";
foreach($tr as $td) {
echo "<td>".$td."</td>";
}
echo "</tr>";
}
echo "</table>";
PS: you have duplicated key named xd. Then PHP will only get the last one.
If you want to stick with using for() loops, then the problem is that your array has xd and xd1 as the keys, but your trying to access them with $i which is 0 and 1. What you can do is create an array of the keys (I've set $keys using array_keys()) and then index that using $keys[$i]...
$tablica = [
'xd'=>['jabłka', 'wiśnie', 'gruszki'],
'xd1'=>['jabłska', 'wisssśnie', 'grussssszki']
];
$n=count($tablica);
$keys = array_keys($tablica);
echo "<table border=1>";
for($i = 0; $i < $n; ++$i) {
echo "<tr>";
for($x = 0; $x < count($tablica[$keys[$i]]); $x++){
echo "<td>".$tablica[$keys[$i]][$x]."</td><br>";
}
echo "</tr>";
}
echo "</table>";

Problems with arrays: Notice: Undefined offset: 0

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

error in php programming [duplicate]

This question already has answers here:
What causes: "Notice: Uninitialized string offset" to appear? [closed]
(5 answers)
Closed 9 years ago.
I have a text file write.txt:-write.txt
My name
jai hind
stack
I am getting the error in the following code at line 154 and 165 and the error is:-
PHP Notice: Uninitialized string offset: 11 in /home/sakshi/Desktop/syllabify/step2/Syll_script_1.php on line 154
PHP Notice: Undefined offset: -2 in /home/sakshi/Desktop/syllabify/step2/Syll_script_1.php on line 165
code:-
$file1 = fopen("write.txt", "r");
$q=1;
while ( $line = fgets($file1) )
{
$i=0;
$arr = explode( "\n", $line );
$q=$q+1;
if(strcmp($arr[0],"." ) == 0)
{
$i=1;
$q=$q-1;
echo "\nposition of dot in write.txt:";
echo "$i \t $q\n";
break;
}
}
fclose($file1);
if($i==0)
echo $i;
while ( $q!=1 )
{
$file_input=fopen("input.txt","r");
$line = fgets($file_input);
$p = strlen($line);
echo $p;
$a=2;
if ($p == $a)
{
$m = $line[0];
echo "$m\n";
$l=0; $h=0;
$filename=fopen("write.txt","r");
$content=file("write.txt");
for($i=0;$i<$u-1;$i++)
{
$arr=$content[$i];
$length=strlen($arr);
for($j=0;$j<=$length;$j++)
{
if(strcmp($arr[$j],$m ) == 0)// line 154
{
echo "\nYES PRESENT AT LINE NUMBER: ";
$h=$i+1;
echo "$h\n";
$l=$h+1;
}
}
break;
}
/********************************************************/
$arr3=$content[$l-2];// line 165
$ar3=explode(" ",$arr3);
$output_file=fopen("Result.txt","a");
fwrite($output_file,$ar3[0]);
fwrite($output_file," ");
fclose($output_file);
$output_file=fopen("Result.txt","a");
fwrite($output_file,$ar3[1]);
fwrite($output_file," ");
fclose($output_file);
$ar3=str_replace("\n","",$ar3);
$output_file=fopen("Result.txt","a");
fwrite($output_file,$ar3[2]);
fclose($output_file);
/************************************************************/
// put enter at the end of operation
$output_file=fopen("Result.txt","a");
fwrite($output_file,"\n");
fclose($output_file);
/**********************************************************/
}
The problem is in the following line:
for($j = 0; $j <= $length; $j++)
it should be
for($j = 0; $j < $length; $j++)
Without the '=' sign. Because with the equal sign the iteration will exceed the length of the string. Counting start at 0 until strlen()-1. Thus
$j < strlen();
Otherwise you should apply
$j <= strlen() - 1;
Check if $arr is an array before access to an element.

Creating a multiplication "grid"

I am trying to create a simple multiplication grid in PHP
It should be of the format for example for a 2x2 grid:
0 1 2
1 1 2
2 2 4
My issue is getting it to start from 0.
This is my nested for loop so far:
for($i=0;$i<=$_POST['rows'];$i++)
{
echo "<tr>";
for($j=0;$j<=$_POST['columns'];$j++)
{
if($i==0)
{
echo "<td>" . 1*$j . "</td>";
}
else
{
$mult = $i * $j;
echo "<td> $mult </td>";
}
}
echo "</tr>";
}
But it gives the output:
0 1 2
0 1 2
0 2 4
I need the column of 0's to be appropriate.
The way you're getting the top row of 0 1 2 3 is by that special-case on the X-axis. Do a similar special-case for the Y-axis ($j):
if ($i == 0) {
... 1 * $j ...
}
else if ($j == 0) {
... $i * 1 ...
}
else {
... $i * $j ...
}
You not only have $i==0 as special case but also $j==0:
if($i==0)
{
echo "<td>" . 1*$j . "</td>";
}
elseif($j==0)
{
echo "<td>" . $i*1 . "</td>";
}
else
{
$mult = $i * $j;
echo "<td> $mult </td>";
}
I don't understand the way you construct your grid. All you need is a row indicator and the number for the multiplication not a nested loop. Second: Why don't you start with 1 instead of catching the case inside the loop. This would be my variant of the multiplication “grid”
<?php
$rows = $_POST['rows'];
$number = $_POST['columns'];
for( $i=1; $i <= $rows; $i++) {
$mult = $i * $number;
echo "<tr>
<td>" . $i.'*'. $j . "</td>
<td>".$mult."</td>
</tr>";
}
?>
This would to a simple grid (x * y) = result. If you want a complete multiplication table it would be something like this:
<?php
$rows = $_POST['rows'];
$number = $_POST['columns'];
echo "<tr><th></th>";
for( $j=1; $j <= $number; $j++) {
echo "<th>".$j."</th>";
}
echo "</tr>";
for( $i=1; $i <= $rows; $i++) {
echo "<tr>";
echo "<th>".$i."</th>";
for( $j=1; $j <= $number; $j++) {
$mult = $i * $j;
echo "<td>".$mult."</td>";
}
echo "</tr>";
}
?>

Categories