Undefined offset: 5 - php

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
$table = array ("1", "2", "3", "4", "5");
$count = count($table);
echo "<table border='1'>";
$rows = 5;
for($i=0; $i <= $count; $i = $i + $rows)
{
echo "<tr>";
for($z = 0; $z < $rows; $z++)
{
if ($table[$i + $z] !=0)
echo "<td>This is row {$table[$i + $z]}</td></tr>";
else
echo "<td> </td></tr>";
}
}
echo "</table>"
?>
</body>
</html>
This is the code I've been trying to make work for a while, and while everything else is OK, the problem occurs when I run it. It shows the table as I want it, but under the table it posts:
Notice: Undefined offset: 5 in [file location] on line 22
Notice: Undefined offset: 6 in [file location] on line 22
Notice: Undefined offset: 7 in [file location] on line 22
Notice: Undefined offset: 8 in [file location] on line 22
Notice: Undefined offset: 9 in [file location] on line 22
I know the problem is around the "!=0" value, but no matter what I change it into, it either flushes the whole effort or repeats the same message.

You missed ; in the following statement.
echo "</table>"
You are iterating in steps of 5. $i + $rows and your $table can go at most $table[4].
The error/notice is because of the same.

I've gotten the same error code, but I managed to find the issue. The error message you're getting has to do with arrays. It seems you're referencing to an undefined array[key]:
for($i=0; $i <= $count; $i = $i + $rows). $i = $i + $rows is the culprit. Example: $i = 0 and $rows=5; so 0 + 5 = 5 so array[5] and this doesn't exist in the array. It only goes to 4 with the $table array.
Hope this helps you along and/or someone else.

<?php
if(isset($_POST['submit'])){
$the_file = file($_FILES['the_file']['tmp_name']);
for( $i = 0; $i < count($myfile); ++$i){
echo $myfile[$i] . "<br/>";
echo "Line " . ( $i + 1 ) . " is " .strlen($myfile[$i]) . " characters long<br/>";
}
}
?>
<form method="post" action="myphp.php" enctype='multipart/form-data' >
<input type="file" name="the_file" value="Yes"/>
<input type="submit" name="submit" value="No"/>
</form>

I think who posted this problem has solved it already. Just a little bit change on this line for($i=0; $i <= $count; $i = $i + $rows) can solve the problem.
for($i=0; $i <= $count; $i = $i + $rows) // for($i=0; $i < $count; $i = $i + $rows) use this.

Related

Trying to solve this php code getting error

i am trying to print 2 4 6 8 10 with spaces, but it prints 246810.
for($i = 1; $i<= 5; $i++){
echo $i * 2.' ';
}
i am getting error : "This page isn’t working"
This might help.
for($i = 1; $i<= 5; $i++){
echo ($i * 2).' ';
}
try:
for ($i = 1; $i <= 5; $i++) {
echo ($i * 2) . ' ';
}
the main reason that your code fails is that the 2 is connected with a dot .
php interprets the 2 as an invalid type as it expects a digit next to it. e.i. 2.0
separating the 2 with a space or in the example with a parenthesis (to explicitly tell the logic) will make the code work
Problem:-2.-> php interprets the 2 as an invalid type because it is expecting a digit next to it something like 2.0. when you add space that simply tell it to apply multiplication first and then add space with it.
1.Either add a space before . like below:-
<?php
for($i = 1; $i<= 5; $i++){
echo $i * 2 .' ';
}
Output:-https://eval.in/856709
2.Or put multiplication into bracket like below:-
<?php
for($i = 1; $i<= 5; $i++){
echo ($i * 2).' ';
}
Output:-https://eval.in/856710
Try this,
for($i = 1; $i<= 5; $i++){
echo ($i * 2).' ';
}

undefined offset and syntax error but code is executed properly

ajax:
...
success: function(data){
console.log(data);
}
position.php
Two arrays are transfered ($ids and $indexes), with the equal number of elements.
extract($_POST);
print_r($ids);
print_r($indexes);
for ($i = 0; $i <= count($ids); $i++) {
$stmt = $db->query("UPDATE " . $table . " SET inde = " . $indexes[$i] . " WHERE id = " . $ids[$i]); // this is line 10
}
The code is executed properly, i.e. all table data are updated as expected, but console (after listing the arrays) shows some errors:
Array
(
[0] => 25
[1] => 23
[2] => 18
[3] => 26
[4] => 21
)
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
)
<b>Notice</b>: Undefined offset: 5 in <b>D:\matria\s02\admin\position.php</b> on line <b>10</b><br />
<b>Fatal error</b>: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064... near 'WHERE id =' at line 1 in D:\matria\s02\admin\position.php:10
Any help?
The problem is with your for cycle:
for ($i = 0; $i <= count($ids); $i++) {
count($ids) is 5 and when $i reaches 5, 5 <= 5 is still true. However, indexing starts from 0, therefore your possible indexes are between 0 and 4. Change your for to:
for ($i = 0; $i < count($ids); $i++) {
and then it will not try to do things when $i reaches 5.
EDIT:
The original answer dealt with your effective error, but there are still things to be refactored/improved after the fix:
you have an array called $indexes which on its own indexes stores the index as value as well. If there is no counter-example unmentioned in the question, then this whole array is redundant and you can remove $indexes and you can use $i instead of $indexes[$i]
your $i < count($ids) in the for cycle will calculate the number of $ids on each iteration. It is much more elegant to calculate it only once before the for with something like $myCount = count($ids); and then use $i < $myCount in your for, to calculate things only once
Suggested code:
extract($_POST);
print_r($ids);
$myCount = count($ids);
for ($i = 0; $i < $myCount; $i++) {
$stmt = $db->query("UPDATE " . $table . " SET inde = " . $i . " WHERE id = " . $ids[$i]); // this is line 10
}

I am getting Undefined offset error php

I am trying to split the Content that is fetching from DB a few words next to image and a remaining content in the below paragraph
I am getting undefined offset error : 41
the below is the code... it executes correctly but with notice error as above... Can you please help me to fix it.
<div class="contentfont" style="padding-left:20px; text-align:left;">
<?php
$words = explode(" ",$dcontent);
$cntWords = count($words);
$splitWords = round($cntWords/2);
for ($i=0;$i<=$splitWords;$i++)
{
print $words[$i].' ';
$halfPoint = $i;
}
?>
<p class="contentfont" style="padding-top:10px;">
<?php
for ($i=$halfPoint;$i<=$cntWords;$i++)
{
print $words[$i].' ';// This print statement is causing me this error Notice: Undefined offset: 41 in D:\xampp\htdocs\training\admin-panel\php\sai_devotees_detail.php on line 106
}
?>
</p>
Undefined offset in this case means you are reading past the length of the $words array. Try $cntWords - 1 for ceiling of the loop like this:
for ($i = $halfway; $i <= $cntWords -1; $i++) {
$halfPoint = $i; is also outside the loop. Try this instead:
for ($i = 0; $i <= $splitWords; $i++) {
print $words[$i] . ' ';
$halfPoint = $i;
}
When you make a loop in PHP without brackets, only the first statement after it will be executed. So in this case, $i doesn't exist for you to assign it to $halfPoint. Don't think it's clever to compact everything into one line. It will only cause you problems.

Why i'm getting Undefined offset: 0 in this php code

i wrote this code and i'm getting this error, i tried every this but doesn't seem to work btw i've made a comment for line 55 where i'm gettin error
function calculate_result()
{
$option_number = array('option_a'=>'1','option_b'=>'2','option_c'=>'3','option_d'=>'4');
$answers = array();
$total_questions = $this->quiz_model->return_number_of_questions($this->input->post('quiz_number'));
if($total_questions > 0)
{
for($i=1; $i <= $total_questions; $i++)
{
//line 55 $answers[$i] = $option_number[$this->input->post('question_'.$i)];
}
print_r($answers);
}
else
{
show_404();
}
//print_r($answers);
}
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: controllers/quiz.php
Line Number: 55
Change this...
for($i=1; $i <= $total_questions; $i++)
To this...
for($i=0; $i <= $total_questions; $i++)
On that line there are two indexes, on the left side, there is $i and it is non-zero.
So, problems has to be with right side expression, with $this->input->post('question_'.$i) which probably returns 0.

I am facing a small bump printing number pyramids , still a newbie to php and programming

What i want to print is
1
3 5
7 9 11
With my current code , that is ...
<?php
function Odd($limit='20'){
$c = 1;
while($c <= $limit){
if ($c % 2!=0){
echo $c ;
echo "<br/>";
}
$c++ ;
}
}
Print Odd();
?>
i am getting
1
3
5
7
9
11
Can someone please guide me the right way ?
Aaah ... ok.^^ Now i got it.
Its pretty easy: You need another variable which counts up and one which limits the breakposition. Looks like this:
<?php
function Odd($limit='40'){
$c = 1;
$count = 0;
$break = 1;
while($c <= $limit){
if ($c % 2!=0){
echo $c . " ";
$count++;
if($count === $break) {
echo "<br/>";
$break++;
$count = 0;
}
}
$c++ ;
}
}
Print Odd();
?>
Output till 40:
1
3 5
7 9 11
13 15 17 19
21 23 25 27 29
31 33 35 37 39
Edit: Code for your new request:
<?php
function Odd($limit='40'){
$c = 1;
$count = 0;
$break = 1;
while($c <= $limit){
echo $c . " ";
$count++;
if($count === $break) {
echo "<br/>";
$break++;
$count = 0;
}
$c++ ;
}
}
Print Odd();
?>
So if I understand correctly you want to output something like that:
1
3 5
7 9 11
13 15 17 19
Here is my solution:
function Odd($limit='20'){
$c = 1;$some_array = array();
while($c <= $limit){
if ($c % 2!=0){
$some_array[]=$c;
}
$c++ ;
}
return $some_array;
}
$array = Odd();
$nr =0;
$j=1;
foreach ($array as $key => $value) {
echo $value.' ';$nr++;
if($nr==$j){
echo '<br />';
$nr=0;
$j++;
}
}
Hope this helps!
From your question it Seems you are really new to programming so before writing any program first of all observe the question properly:
For example for the question above it is clear that is an triangle of odd numbers.
now the number of odd numbers on each row is equal to the row
i.e 1st row contains 1 number ,2nd contains 2 and it continues...
Now what we do is take an variable to count the no of rows say $row and the other will be $limit .
<?php
function odd($limit){
$row=1;
$current_number=1;
while($current_number<=$limit){
for($i=1;$i<=$row;$i++){
echo $current_number." ";
$current_number=$current_number+2;//incrementing numbers by 2 if you want to increment by 1 i.e print all numbers replace 2 by 1
}
$row++;
echo "<br/>";//for new line
}
}
To run above function you need to call it and pass the value of $limit.To do it just type anywhere outside of this function.
odd(20);
Watch this running here:

Categories