Jump to content

Undefined Variable


JaEvans

Recommended Posts

I am getting this error message:

 

Notice: Undefined variable: folders in /hsphere/local/home/digitaldaylight/herorisingthebook.com/modules/mod_nivoslider/tmpl/default.php on line 15

 

The code on line 15 is:

 

modNivoSliderHelper::render($params, $folders, $images);

?>

 

How do I redo the code to get rid of the error message.  Essentially I tried if code and isset but I am not knowledgible enough in php to fix the problem.  And I dont want to change the reporting error sensitivity.  Its best to do a fix of the code rather than ignore it.

Link to comment
Share on other sites

yes,.. fixing your code rather than ignoring the errors they create is usually the best option.

 

but yeah the error means.. well.. you never defined the $folders variable. We would need to see the code before it to really help, but try defining $folders. Also, use code or php tags when posting code please.

Link to comment
Share on other sites

This is all the code on the page:

 

 

<?php

/**

* @package    Nivo-Szaki Slider

* @link        http://szathmari.hu

* @version    1.0

* @copyright  Copyright © 2011 szathmari.hu

* @license    GNU/GPL http://www.gnu.org/copyleft/gpl.html

*/

defined('_JEXEC') or die( 'Restricted access' );

if (!$images)

{

echo JText::_('Images not found');

}

else

modNivoSliderHelper::render($params, $folders, $images);

?>

 

Link to comment
Share on other sites

Hi,

 

The script if for an image slider.

 

Do I need to define just the $folder or all of the $params, $folders, $images?

 

Also, how would I properly use isset or if in order to define them but not statically. 

 

Something like this is not working but not sure why:

 

modNivoSliderHelper::render($params, $folders, $images); if(!isset($params, $folders, $images))        ?>

 

 

 

Link to comment
Share on other sites

You need to define at least $folder, as that is what is giving you an error. Not sure about the rest, as, again, I have no clue how your script works, so I don't really know where the variables are supposed to be coming from.

 

I'm not sure exactly what you mean by using isset to define them not statically. That doesn't really make any sense. Would you mind explaining?

 

Also, the code you posted doesn't really make any sense at all. What are you trying to accomplish there.

 

And again, use code/php tags when posting code

Link to comment
Share on other sites

The code is from a third party template I purchased.  Essentially I dont want to define a specific value because this is for joomla and the value will be defined by my use of the template functions.  What I need is for the code to define itself as true while undefined.  this error message is all over google with multiple answers on how to do it.  The thing is the actual code is different than this as for a different function so I can't just transfer one idea onto this code.

 

here is what one php post says:

 

If you dont want to define the variable first - you can always use a

conditional to act if it doesnt exist:

if(!isset($varname))

{

do something

}

 

This is what I want to do.  I want to use a conditional but I dont know how to write that in relation to the existing code:

 

modNivoSliderHelper::render($params, $folders, $images);

?>

 

I am thinking this code is to direct where to render the images from that the image viewer uses but not 100% sure.

 

Any help in figuring how to make the above undefined code conditionally defined would help. 

Link to comment
Share on other sites

I'm not sure you fully understand what isset() does. All it does is return true or false if the variable has been set/not been set. If the variable hasn't been set (and in your case it obviously hasn't), using isset won't magically make it set some how.

Link to comment
Share on other sites

I do understand that.  what I am trying to do is not define the variables because I did not write the script and do not know how I would define them if I did.  My research shows that this is completely possible but the "if" script and "isset" script need to be done correctly.  I have only seen it done with a single variable, example $param.  but not with multiple variables, example $param, $folder, $images.  I don't know what the string $param, $folder, $image means.  What I do know, evidenced by above quote from other website :

 

If you dont want to define the variable first - you can always use a

conditional to act if it doesnt exist:

if(!isset($varname))

{

do something

}

 

is that the variables do not have to be defined, a conditional can be a work around.  I just dont know how to write the conditional.

 

Another issue is that I have attempted everything on the error reporting side:

 

error_reporting = E_ALL & ~E_NOTICE

 

this code is not working at all and the notice is still showing up on my frontpage.

 

I am a little confused at the answers I am getting.  I have been scanning forums for hours trying to figure this out, though this is the first I have asked my question on, and when other's ask the same question they actually get offered code solutions without them posting any more info than I have.  I may try another forum but I don't want to join a million of them.  I just need help sorting out this issue.  I want an error message off of my front page and I am having a challenge making that happen.  Any help would be great.

Link to comment
Share on other sites

Well like I said, I have no idea how your script, or your objects work at all. I have never used or seen the specific library you are using, so I apologize that I can't offer you a code solution, but I simply don't know what to tell you without more information. On other forums, the other posters may have more experience with your library. Why don't you try posting this question on a forum that seems to get better answers? That seems to be the logical solution, because no one else seems to be able to help, and I'm the only one who's even trying to give it a shot. Perhaps one of the more experienced members will know or have seen this library, and offer a superior solution if they run across this thread, but for right now I simply need more information. Perhaps you could post some code examples you have seen, and try to explain them.

 

As for isset() (link is to manual entry). The quote you posted. Is it from this thread: http://bytes.com/topic/php/answers/12750-php-saying-i-have-undefined-variable

 

what he is referring to in the quote

if(!isset($varname))

{

do something

}

 

is taking a specific action when your variable is undefined, but he never describes the specific action. For your specific problem, you seem to understand that isset() could be used to get around the problem you have, but what you are not considering is the actual action that needs to take place. I will give you an example of common use of isset().

 

Say I have a web page with a form on it, called sampleForm.html. This page submits to a php script that processes the post data, and outputs some information". It is called samplePHP.php.

 

SampleForm.html could look something like

<!DOCTYPE html ....
<html>
<body>
<form name="form1" action="sname=amplePHP.php" method="post">
<input name="textbox1" type="text">
<input name="textbox2" type="text">
<input type="submit" name="submit1">
</form>
</body>
</html>

 

Now, the actual processing that we do normally is unimportant, so in the example, I will do something simple and stupid. However, we must discuss the actual usage of isset(). In this example, I will use it to make sure that someone has actual submitted the form before we do processing. Otherwise, someone could simply navigate to the page itself, and break the script. This is very useful for much more complex scripts, like user logins, or e-commerce.

 

sampePHP.php

<?php

if (isset($_POST['submit1'])){//here we check that the submit button is set. If it is set, then we know that the user submitted the form
//here we do processing. I will show a few more examples of isset.
  if (!isset($_POST['textbox1'])){
    //this means they didnt enter the first text box. Say something
    echo "you didnt enter the anything in the first textbox<br />";
  }
  else if (!isset($_POST['textbox2'])){
    //they didnt enter the second textbox
    echo "you didnt enter anything in the second textbox<br />";
  }
  else {
    //they entered both, so say a message
    echo "You entered something in both! Good job!<br />";
  }
}
else {
//here we handle what happens when the submit button is set.
//this means they navigated to the page, or got to it through some other means
//here we simply say a message
echo "You didnt submit the form!";
}

/***********************
NOTE: Without isset(), out samplePHP page could look something like
echo "You entered something. Cool. You entered " . $_POST['textbox1'] . " and " . $_POST['textbox2'];
***********************/

 

Now, for your example, we need to consider what you are going to do if that variable is not set. Consider the following

 

//we can check if $folders is defined
if (isset($folders)){
  //however, all this tells us is that it is defined.
  modNivoSliderHelper::render($params, $folders, $images);
}
else {
  //this is where the problem lies. what do we do when $folders is not defined
  //maybe we should set it to a default value.
  //but what will that default value be?
  $folders = ????;
  modNivoSliderHelper::render($params, $folders, $images);
  //or maybe we should call a different function?
  // but what function?
  mysteryObject::mysteryStaticFunction($mysteryArguments);
  //or maybe we should do nothing at all?
}

 

The problem I am having helping you is that I don't know what you have to do when $folders is undefined. As I have said numerous times, I have no idea on what this code is supposed to accomplish. You haven't told us what your script actually even does, besides generate that notice. Does the function render an image? Does your library work? Is this script included in a page with a bunch of other function calls, and includes? Is this part of large library that is loaded automatically when you use the library in your other pages? This is why I have been asking for more information. The answer could be one of many many different solutions, and I just don't know enough to give you a definite answer. For this I apologize, but as I said, I have no experience with the library you are using, so I simply don't know anything about it.

 

Another thing to consider. From that thread you keep quoting, you forget to mention the following:

Note that this is not an error but a notice - there's a difference. Errors

will stop execution of your script, while warnings and notices are there to

inform you of possible faulty code. Depending on your error_reporting

setting in php.ini, you may or may not see warnings and notices. Your script

has an undefined variable on your server but your server is just not telling

you, because error_reporting is set to off or warn (not all).

 

It is likely that you are using $end_while in a conditional statement before

you have defined it, leading to the notice. You should set it to a default

value at the beginning of your script, or before you use it.

 

Like this person says, you do not have an error, but a notice. Your script will still run even though PHP is telling you something. Usually, with notices, this simply means something is coded in a non-optimal way. There are no syntax errors that are causing an end to execution, but you have coded in such a way that something unexpected may happen. Like in your example, you are sending an undefined variable to that function. Or in my example, where if we didn't use isset(), the script would give you an unexpected and useless message: "You entered something! Cool! You entered and ". The rammifications of doing this are unknown to me, so this could be something that breaks the script, or be something that doesn't really matter, as that static method you are using is written in such a way to handle null or undefined arguments. I am unsure of this because you haven't actually told me what is happening with your script.

 

However, if your script still works even though you are sending an undefined variable, you could always just suppress the notice since it doesn't really break the script. You can do this by prepending that line with the error control operator (@) like so

@modNivoSliderHelper::render($params, $folders, $images);

 

However, you should note that using this suppression operator is a band-aid solution, and doesn't really fix the core problem. The best way would be to use isset() in such a way that it gracefully handles what happens when the variables are undefined, whether it be providing some default values, or calling a different function.

 

Hope this helps

 

 

 

 

Link to comment
Share on other sites

This response is absolutely fantastic.  Just to reiterate, this is not my script.  The script was written by monstertemplates.com  It is for an image slider.  I guess the key is to figure out if I need to provide some default values, or call a different function.  I am not sure how to write the code for either and I guess what gets done depends on what exactly this script is meant to do.  I will contact monstertemplates and get some answers from them regarding the function of this specific script.

 

Thanks a bunch

 

J

Link to comment
Share on other sites

So I posted the same question on another forum and this is the answer I got:

 

I tried to lookup the documentation for these functions, but couldn't find it (nor could I even locate this module to look at the source).

 

So the best I can do is tell you how to skip that part entirely if the error is going to be trigger.

 

Change this:

PHP Code:

else

modNivoSliderHelper::render($params, $folders, $images);

?> 

 

 

To this:

PHP Code:

elseif(isset($folders))

modNivoSliderHelper::render($params, $folders, $images);

?> 

 

  So this worked and I no longer have the error message bothering me.  This is exactly what I was looking for I just did not know how to write it. It looks like what I was doing wrong is I was placing the if isset after the original code rather than before it.  I know that this does not define the variable but because the error was really a notice and non important at that, All I needed was for it to stop showing up. 

 

Link to comment
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.