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

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.

Related

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.

Notice: Undefined offset: 27 Why these error generated in PHP?

I used below code but that gives me an Error like "Notice : Undefined offset: 27" . Why these error generated or what these error means ? please any one help me.
//Code ... final_display2 is found some array.
for($p=0; $p<sizeof($final_display2); $p=$p+2){
$b = $p+1;
$len1 = strlen($final_display2[$p])+strlen($final_display2[$b]); //these line gives error 116
if($len1 < 3200){
array_push($final_display,$final_display2[$p].$final_display2[$b]); //and these one also 118 }else{
array_push($final_display,$final_display2[$p]);
array_push($final_display,$final_display2[$b]);
}
}
ERROR!!
Your error occurs when $p==sizeof($final_display2)-1, so $b==sizeof($final_display2), which will result in a Undefined Offset.
ie. if sizeof($final_display2)==27, when $p==26, then $b==27 and $final_display2[$b]/$final_display2[27] does not exist.
Try using if(isset($final_display2[$b])) ->
for($p=0; $p<sizeof($final_display2); $p=$p+2){
$b = $p+1;
if(isset($final_display2[$b])){ //only run if $final_display2[$b] exists
$len1 = strlen($final_display2[$p])+strlen($final_display2[$b]); //these line gives error 116
if($len1 < 3200){
array_push($final_display,$final_display2[$p].$final_display2[$b]); //and these one also 118 }else{
array_push($final_display,$final_display2[$p]);
array_push($final_display,$final_display2[$b]);
}
}
}
Just use $p instead of $b since your looping trough all items in $final_display2, but your adding 1 to $b it's an offset! (Also change in your for loop this $p=$p+2 to $p++)
Little example:
You have 5 items in $final_display2 so the loop is like this:
for($p=0; $p<5; $p++)
//So if your at the last element ($p = 4) but your adding 1 to $b and use it you have a offset
Edit:
I think your looking for something like this:
foreach($final_display2 as $k => $v) {
if( isset($final_display2[$k+1]) ) {
if($k == 0 || $k % 2 == 0) {
$len1 = strlen($final_display2[$k])+strlen($final_display2[$k+1]);
if($len1 < 3200){
array_push($final_display,$final_display2[$k].$final_display2[$k+1]);
array_push($final_display,$final_display2[$k]);
array_push($final_display,$final_display2[$k+1]);
}
}
}

php undefined offset 150 error in for loop

I keep getting an undefined offset 150 error, but i am not sure what it means or what i should do to debug it. Do to the error line i believe that it has something to do with my for loop.
// Get Datafile
$MyData = file("./tmp/test.txt");
// Get Column Headers
$ColHeads = explode(" ", $MyData[1]);
unset($MyData[1]);
$LastHeader = "";
for ($i = 0;$i <= count($ColHeads);$i++) {
if ($ColHeads[$i] == $LastHeader) { //<---this is the line that errors
$ColHeads[$i] = $ColHeads[$i] . "1";
}
$LastHeader = $ColHeads[$i];
}
Would anyone have any ideas as to where I am going wrong?
and the error is:
Undefined offset: 150
I am sorry if this is vague. I am not familiar with php and not sure where to start on debugging this... any help would be much appreciated! Thank you!
Change your for loop:
for ($i = 0;$i < count($ColHeads);$i++) {
The problem is that on the last iteration of the loop, when $i == count($ColHeads), that's going to set $i too high for the number of items in $ColHeads.
You started off right by setting $i = 0. If $ColHeads has 5 items in it, those items have indexes from 0 to 4. Your for loop, as it is, goes up to 5 - and there is no $Colheads[5], so the error is thrown.
Array index starts from zero. And ends at Length-1. So it should be:
for ($i = 0;$i < count($ColHeads);$i++) {

Warning: Illegal string offset in PHP 5.4 [duplicate]

This question already has answers here:
Illegal string offset Warning PHP
(17 answers)
Closed 9 years ago.
I upgraded to PHP 5.4 today and I am receiving some strange warnings:
Warning: Illegal string offset 'quote1' in file.php on line 110
Warning: Illegal string offset 'quote1_title' in file.php on line 111
Those lines are this part of the code:
for($i = 0; $i < 3; $i++) {
$tmp_url = $meta['quote'. ($i+1)];
$tmp_title = $meta['quote' . ($i+1) .'_title'];
if(!empty($tmp_url) || !empty($tmp_title)) {
$quotes[$src_cnt] = array();
$quotes[$src_cnt]['url'] = $tmp_url;
$quotes[$src_cnt]['title'] = $tmp_title;
$src_cnt++;
}
}
So the $tmp_url and $tmp_title line.
Why am I receiving this odd warning and what is the solution?
Update:
This code is being used as a Wordpress plugin. $meta includes:
$meta = get_post_meta($post->ID,'_quote_source',TRUE);
So I am suspecting that whenever the quotes fields are empty, this warning appears. Is there any way that I can fix this for when the fields are empty?
You need to make sure, that $meta is actually of type array. The warning explicitly tells you, that $meta seems to be a stringĀ and not an array
Illegal string offset
^^^^^^
To avoid this error you may also check for the needed fields
for($i = 0; $i < 3; $i++) {
if ( !is_array($meta) || !array_key_exists('quote'. ($i+1), $meta) ){
continue;
}
// your code
}
If $meta is null whenever there's no data to process:
if( !is_null($meta) ){
for($i = 0; $i < 3; $i++) {
// ...
}
}
You should be able to do more checks if necessary. That depends on what that get_post_meta() function is designed to return.

Undefined offset: 5

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

Categories