Dealing with Undefined offset errors

Hi there

I’m sure the answer is within this forum/site somewhere, I am however totally ignorant of php coding etc and am so confused by what I am reading…

I was supplied some code (graciously) by my Wordpress Theme Support. This threw errors on pages that the slider was not set… I have managed to figure out is due to the variable being not set, so have modifies the code to set the variable $page_slider to null on pages where a slider is not needed. See Below…

I’m wondering if there is a better way to do this?? If so, how??

[php]// 2.2 Displaying custom slider on the right of the logo
// =============================================================================
// Displaying custom slider on the right of the logo
// =============================================================================

function add_custom_slider(){

//Each page ID is equivalent to a slider

$page_slider = array(
2 => null,
5 => ‘MainPage’,
51 => null,
1343 => null,
1364 => null,
1384 => null,
1473 => null,
1508 => null,
1525 => ‘left-bank’,
);

$slider = $page_slider[ get_the_ID() ];

if ( !empty( $slider ) ) :

?>

<?php putRevSlider( $slider ); ?>
<?php endif; } add_action('x_after_view_global__brand', 'add_custom_slider');[/php] Appreciate the assist in advance Rick M NZ

Well, if you use “null” to set a variable, even if it is inside an array, you need to test it before you can use it.
Therefore, this line:
$slider = $page_slider[ get_the_ID() ];
might throw an error if it is a null value. You would need to check it BEFORE you use it or assign it to another
variable. Something loosely like:
if(isset($page_slider[ get_the_ID() ])) $slider = $page_slider[ get_the_ID() ];
Or even better,
if(!isset($page_slider[ get_the_ID() ])) {
echo “no slider page set!”;
} else {
$slider = $page_slider[ get_the_ID() ];
// *** Here you would have the rest of your code…
}
Note that this is just loose code not tested or valid… You need to test the value before you assign it.
In your code, you could skip the assignment of the array and just test it directly. Something like:
[php]
// $slider = $page_slider[ get_the_ID() ];

if ( issetl( $page_slider[ get_the_ID() ] ) ) :

?>

<?php putRevSlider( $page_slider[ get_the_ID() ] ); ?>
<?php endif; } [/php] If you "set" a variable to null, it is not set. If a variable is "set" and emptied using ="" , it is not null but empty. You need to use the isset() function. Here is an explanation from an online post which might help:
isset() From PHP manual – isset(): isset — Determine if a variable is set and is not NULL In other words, it returns true only when the variable is not null. empty() From PHP Manual – empty(): empty — Determine whether a variable is empty In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable. is_null() From PHP Manual – is_null(): is_null — Finds whether a variable is NULL In other words, it returns true only when the variable is null. is_null() is opposite of isset(), except for one difference that isset() can be applied to unknown variables, but is_null() only to declared variables

Hi ErnieAlex

Thank you for your response… I have been round and round in circles trying to figure this out… I have to be honest I tried something like this before and failed…

Or even better, if(!isset($page_slider[ get_the_ID() ])) { echo "no slider page set!"; } else { $slider = $page_slider[ get_the_ID() ]; // *** Here you would have the rest of your code... }

This worked with the addition of a curly bracket instead of the endif.

I also found a somewhat less than desirable solution by using the @ error control operator.

$slider = @$page_slider[ get_the_ID() ];

Not a solution, a means to an end…

SO all I would need to sort out now, is instead of outputting “No Slider Set!” is there a graceful exit method like [php]die()[/php] (not graceful me thinks) that I could use.

Much thanks for helping me out… Your approach to solutions and newbies is great…

Rick M

NZ

Thank you! I do try to help newbies at their level and explain, sometimes over-explain so you can learn.

Anyway, there is a typo in this line:
if ( issetl( $page_slider[ get_the_ID() ] ) ) :

It should be isset not issetl … So, that might have been your problem…

In my opinion, you should never ever never use the @ to suppress errors. You need the errors to fix the code.
Also, you can not do any kind of error-checking if you do not get the errors to start with. So, forget that one!

Also, the “endif” is the same as }, just another version. So, if(cond){ code } is the same as if(cond): code endif;
Just a different way of formatting… Go back to the first format of your code in your first post. Change the
lines 22 and 24 from:
[php] $slider = $page_slider[ get_the_ID() ];

if ( !empty( $slider ) ) :[/php]
to:
[php] if (isset($page_slider[ get_the_ID() ]) $slider = $page_slider[ get_the_id() ];

if ( isset( $slider ) ) :[/php]
And, let us know what happens. If it does not work, then you need to debug it. To do that, you need to look
in the values to see where it is failing. To do that, you need to display the data just before the page gets to the
line#22. Something like this:
[php]echo “

”; print_r($page_slider); die("

");
if (isset($page_slider[ get_the_ID() ]) $slider = $page_slider[ get_the_id() ];

if ( isset( $slider ) ) :
[/php]
What this will do is display your data inside the $page_slider array. That will show what is inside the array. If it
looks correct, you can replace the print_r with an echo of the get_the_ID() to see if the correct ID is showing up.
In other words, you would step thru the data displaying it and viewing what is actually in the data variables or
arrays. In that process you can track down the error and solve the puzzle…
Hope that helps…

Hi Ernie

Thank You.

I will go away and start playing a little. I am now curious as to the options there might be to deal with these errors.

I did catch the issetl error after attempting to run your suggested code… It actually works fine once that has been removed…

I have also commented out the echo “No Slider Set” and the code is still working fine. It now does not print the error over my picture on the page. Again not sure if this is the right way to do things… However at this time I am happy the code works and extra page IDs don’t have to be entered into the code for each new web page created… Just those that will have a slider added…

You have inspired me to learn more about PHP, which I will do in the near future after we have sold one of our current businesses and I have a little extra free time…

I am creating a wordpress website using the X Theme staging.bell-lodge.co.nz . It is not terrific right now, I’m slowly learning how to use WordPress and the Theme… I have some knowledge of HTML and CSS with the original website built directly using them. It was then rebuilt on WordPress but the Theme is no longer supported and I decided to update the site.

If you want to have a peek at the sites the main site is bell-lodge.co.nz

Many Thanks good Sir

Rick M

NZ

Glad we could help you… You site is colorful and eye-catching. Nice! I did not click on much, but, I just played
with the opening page sliding up and down to watch it. Fun… A lot of sites would not work with that type of
“glitter”, but, it works very well for your content. In my humble opinion…

So, Word Press templates that are no longer supported is not really an issue if you learn how to update them for
yourself. Usually, they do require a lot of learning about both the back-end ADMIN panel and the template itself.
But, if you learn the entire system yourself, you do not need support from the author. But, it does take a lot of
studying!

Once you are further along in your PHP studies, you might want to learn PDO. PDO is a better and safer way to
access your database system. Most templates that I have seen do not use this as often as they should. PDO
is a replacement for MySQLi which replaces MySQL… Confusing, right? But, if you want to continue with PHP,
it is one area of study you should look into. (See links below…)

Now, as far as error checking goes, you normally would use a “Try-Catch” system. There is also link to this
below. Basically, using “TRY”, you can have the server test a process and if it does not work correctly, you can
catch the error and handle it in a display. This uses “exceptions” and is fairly easy to learn. And, of course, you
can always post on this site when something does not work well for you. Trapping errors is very important for
websites. I use a hidden DIV that only shows if an error message needs to be displayed. Many ways to do that!

W3Schools is a great learning resource for newbies and can help in finding samples and tutorials for all levels of
programming. Here is a starting point for databases. It shows the current MySQLi and PDO samples starting
with how to create a database and press the green next-chapter to see the next stage. It goes thru most of
all things database…
http://www.w3schools.com/php/php_mysql_create.asp

Error-checking is basically a procedural way to test a function without it “dying” or stopping the page. If there
is an error found in the process, it is caught and you can use the error message as needed or just create your
own version of it. Often, I have a site send an email out to the ADMIN to note that the site encountered an
error that needs to be fixed. In that manner, you can send the data that caused the error so the programmer
can solve it for the next user… Anyway, here is a link explaining it in depth:
http://www.w3schools.com/php/php_exception.asp

Now you have some homework… LOL I will mark this one solved… See you again, Mate, in your next one!
http://www.w3schools.com/php/php_mysql_create.asp

Sponsor our Newsletter | Privacy Policy | Terms of Service