elite_prodigy Posted January 8, 2009 Share Posted January 8, 2009 I'm getting an undefined offset error in this code, if the OOP is bad, forgive me, it's late, and any ideas on how to make it better would also be appreciated. The error: Notice: Undefined offset: 2 in C:\wamp\www\newsite\team\php\classes.php on line 41 Notice: Undefined offset: 2 in C:\wamp\www\newsite\team\php\classes.php on line 41 The code: <?php include 'config.php'; class Page{ function Page(){ $this->id = $_GET['id']; //$this->action = $_GET['action']; } function tabs(){ $tab_loc = array('index.php?id=0', 'index.php?id=1', ); $tab_name = array('A Tab', 'Another Tab', ); $tab_list = ""; for($size = 0; $size <= count($tab_loc); $size++){ if($this->id == $size){ $tab_list .= '<div class="visit_tab_outer"> <div class="visit_tab_inner"> <a href="'.$tab_loc[$size].'">'.$tab_name[$size].'</a> </div> </div>'; continue; } $tab_list .= '<div class="tab_outer"> <div class="tab_inner"> <a href="'.$tab_loc[$size].'">'.$tab_name[$size].'</a> // line 41 </div> </div>'; } echo $tab_list; } } ?> Thanks for all of your help. Quote Link to comment https://forums.phpfreaks.com/topic/140000-undefined-offset/ Share on other sites More sharing options...
premiso Posted January 8, 2009 Share Posted January 8, 2009 Basically $size is not an index in the array. You can avoid this by checking that index of the array with isset before trying to use that: <?php for($size = 0; $size <= count($tab_loc); $size++){ if($this->id == $size && isset($tab_loc[$size])){ $tab_list .= '<div class="visit_tab_outer"> <div class="visit_tab_inner"> <a href="'.$tab_loc[$size].'">'.$tab_name[$size].'</a> </div> </div>'; continue; } if (isset(isset($tab_loc[$size])) { $tab_list .= '<div class="tab_outer"> <div class="tab_inner"> <a href="'.$tab_loc[$size].'">'.$tab_name[$size].'</a> // line 41 </div> </div>'; } } echo $tab_list; } ?> That way it is only echoed out if that size is in the array. Quote Link to comment https://forums.phpfreaks.com/topic/140000-undefined-offset/#findComment-732446 Share on other sites More sharing options...
rhodesa Posted January 8, 2009 Share Posted January 8, 2009 change: for($size = 0; $size <= count($tab_loc); $size++){ to for($size = 0; $size < count($tab_loc); $size++){ Quote Link to comment https://forums.phpfreaks.com/topic/140000-undefined-offset/#findComment-732448 Share on other sites More sharing options...
GingerRobot Posted January 8, 2009 Share Posted January 8, 2009 You want to use the less than operator (<), not the less than or equal to operator (<=) in that for loop. Computers start counting at 0; while there are two elements in that array their indexes are 0 and 1 respectively. Edit: Beaten to it but posted for the explanation Quote Link to comment https://forums.phpfreaks.com/topic/140000-undefined-offset/#findComment-732452 Share on other sites More sharing options...
elite_prodigy Posted January 8, 2009 Author Share Posted January 8, 2009 Thanks, I know computers start counting at zero, I just wasn't making the logic leap that the error was with the loop and not with the actual array itself. Thanks for everything. Quote Link to comment https://forums.phpfreaks.com/topic/140000-undefined-offset/#findComment-732458 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.