mzhou88 Posted February 28, 2007 Share Posted February 28, 2007 $output = array(); while (list($key,$value) = each($lines)) { if (($key >= $startNo) && ($key < ($startNo+$xf_config_postsystem_posts_per_page))) { $output[$key] .= $value; } } return $output; that code gives me an undefined offset error, and i don't know how to fix it... i've tried $output = array(); and i've tried $output = ''; and other similar line before calling the loop. any help appreciated - thanks Link to comment https://forums.phpfreaks.com/topic/40497-solved-undefined-offset-error-in-a-while-loop/ Share on other sites More sharing options...
monk.e.boy Posted February 28, 2007 Share Posted February 28, 2007 $output[$key] .= $value; This is appending the string $value to $output[$key], like this: $output[1] .= 'world'; So if $output[1] does not exist, you get a key error. So check that it exists first: if( array_key_exists($key, $output)) { // it exists, so we can append to it: $output[$key] .= $value; } else { // does not exist, so we create it: $output[$key] = $value; } Take careful note of = and .= in the code. monk.e.boy Link to comment https://forums.phpfreaks.com/topic/40497-solved-undefined-offset-error-in-a-while-loop/#findComment-195930 Share on other sites More sharing options...
mzhou88 Posted February 28, 2007 Author Share Posted February 28, 2007 ahh.. thankyou thankyou.. it works. but it will output all the $output[$key] in this case, how do i limit $key to display only some of it, for example, $key is between 4 and 10 ? thanks =p Link to comment https://forums.phpfreaks.com/topic/40497-solved-undefined-offset-error-in-a-while-loop/#findComment-195948 Share on other sites More sharing options...
mzhou88 Posted February 28, 2007 Author Share Posted February 28, 2007 *bump* edit// oops sry, just read the posting guidelines, shouldnt have done this.. Link to comment https://forums.phpfreaks.com/topic/40497-solved-undefined-offset-error-in-a-while-loop/#findComment-195970 Share on other sites More sharing options...
monk.e.boy Posted February 28, 2007 Share Posted February 28, 2007 ahh.. thankyou thankyou.. it works. but it will output all the $output[$key] in this case, how do i limit $key to display only some of it, for example, $key is between 4 and 10 ? thanks =p eeerm, if( ($key>4) && ($key<10 )) Not rocket science. monk.e.boy Link to comment https://forums.phpfreaks.com/topic/40497-solved-undefined-offset-error-in-a-while-loop/#findComment-196028 Share on other sites More sharing options...
mzhou88 Posted February 28, 2007 Author Share Posted February 28, 2007 sorry i guess i shoulda been specific, what i did earlier was add my piece of code with yours and i end up with warning messages that said for the function array_key_exists() the 2nd parameter shoulda been an array or object. so i was just wondering how you put together the code, cause im noobie thanks =p edit// nvm i was being an idiot, mis typed some punction.. thanks =p Link to comment https://forums.phpfreaks.com/topic/40497-solved-undefined-offset-error-in-a-while-loop/#findComment-196042 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.