Undefined offset PHP error, trying to read from file - php

I'm getting an "Undefined offset" PHP error, and don't get what causes it. I'm just a beginner.
Notice: Undefined offset: 3 on line 58
Here's code I'm using:
$file = fopen("portfolio.file", "r") or die("Unable to open a portfolio file.");
$portfolioFull = fread($file,filesize("portfolio.file"));
fclose($file);
$portfolioItems = explode(";", $portfolioFull);
$i = count($portfolioItems);
echo $i;
while ($i >= 0){
$portfolio[$i] = explode("||", $portfolioItems[$i]);
$i = $i - 1;
}
echo $portfolio[1][0];
echo $portfolio[1][1];
echo $portfolio[1][2];
echo $portfolio[2][0];
echo $portfolio[2][1];
echo $portfolio[2][2];
Here's what portfolio file contains:
Item 1 Title
||
Item 1 Description
||
DOWNLOAD PENDING
;
Item 2 Title
||
Item 2 Description
||
DOWNLOAD UNAVAILABLE
;
Test Item
||
Test Description
||
DOWNLOAD AVAILABLE
And here's what debug echo says: https://gyazo.com/2e1a6e90f1a33578b40e5f330e19dd78
Any clues how to fix that?

$i = count($portfolioItems);
echo $i;
$i-- ; //reduce by one should fix the problem
while ($i >= 0){
$portfolio[$i] = explode("||", $portfolioItems[$i]);
$i = $i - 1;
}
Since the index of an array start from zero, the value of the last element will be count($array) -1.
In this case you are using the count value , which will be 1 greater than the last index. Ex: if array has 3 elements , the indexes will be 0 , 1, 2 . Trying to use 3 as an index will give you this error.

Related

How can I fix this error "Undefined offset: " at Regex?

I get this Error for every row in the data. So around 500 times, but every time with an other Undefined offset.
Heres my Code:
$fl_array = preg_grep('/^\d+\s('. $SelectedTime .':)\d+/', explode("\n", $finalLog));
$count = count($fl_array);
for ($x = 0; $x < $count; $x++)
{
echo "$fl_array[$x] \n";
}
as written here - http://php.net/manual/en/function.preg-grep.php: Returns an array indexed using the keys from the input array. so maybe smth wrong with your keys:
if (isset($fl_array[$x])) {
echo "$fl_array[$x] \n";
}

Why does this function generate an uninitialized string offset notice?

I'm having this error when I run my program:
Notice: Uninitialized string offset: 7 in C:\xampp\htdocs\demo\str_rev.php on line 21
What causes that?
<?php
//strrev($arg);
/*$str = "ademola";
echo strrev("$str");
*/
function reverse_String($str){
$i = 0;
while(!empty($str[$i])){
echo $str[$i];
$i++;
}
for($r = $i; $r > -1; $r--){
$reverse = $str[$r];
echo $reverse;
}
}
reverse_String("Ademola");
?>
Output:
Ademola
Notice: Uninitialized string offset: 7 in C:\xampp\htdocs\demo\str_rev.php on line 21
alomedA
The $i++; in your first while loop increments $i to 7 in its last iteration. The condition !empty($str[$i]) is no longer satisfied, so the loop does not execute again, but $i is still 7 when the next loop starts, which is an index beyond the end of the string.
There are various ways to fix this, a simple way is to subtract 1 from the counter when you define your second loop to set $r to the index of the last character in the string.
for($r = $i - 1; $r > -1; $r--){ ...
As mentioned by Don't Panic there are many ways to fix this,
you can use isset as:-
for($r = $i; $r > -1; $r--){
if(isset($str[$r])) {
$reverse = $str[$r];
echo $reverse;
}
}
Or to reverse the string you can simply use the php's built in function (strrev)
echo strrev('Ademola')

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 echo in rows of three?

I am attempting to show data in rows of three like this (notice the number of items will not always be even):
abcd defg hijk
lmno pqrs tuvw
xyz1 2345 6789
1011 1213
I am struggling to get the logic right to do this (this is in a foreach() loop).
I know I have to have some if($i %3 == 0) logic in there.. But I'm a bit stuck.
Can anyone help me out?
$a = array('abcd','defg','hijk','lmno');
for ($i = 0; $i < count($a); $i++) {
if ($i && $i % 3 == 0)
echo '<br />';
echo $a[$i].' ';
}
It's better to use a for loop as:
// run $i for each index in the array.
for($i=0 ; $i<count($arr) ; $i++) {
// if $i is non-zero and is divisible by 3 print a line break.
if ($i && $i % 3 == 0) {
echo "<br />";
}
// print the element at index $i.
echo $arr[$i].' ';
}
Code in action
Pseudo-code since I don't know PHP (and you asked for the logic which tends to be the same across all procedural languages):
perline = 3
i = 0
foreach item in list:
if i > 0 and (i % perline) == 0:
print newline
if (i % perline) != 0:
print space
print item
i = i + 1
This will both output a line separator before elements 3, 6, 9 and so on (first element being 0) and place whatever desired spacing you want before the second and third elements on each line. You can just use a different value for perline to change the number output on each line.

Categories