Jump to content

Recommended Posts

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. :)

 

Link to comment
https://forums.phpfreaks.com/topic/140000-undefined-offset/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/140000-undefined-offset/#findComment-732446
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/140000-undefined-offset/#findComment-732452
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.