Jump to content

Recommended Posts

Hello All,

 

I am still new to coding but since i moved from php4 to php5 i am encountering some minor errors but things i want to fix. Like right now I am getting the error

 

Notice: Undefined variable: canhave in /home/nerodbc/public_html/test/charfun.inc on line 205

 

now this is a function that I am callign from another file.

 

So the main file starts off like this.

 

 
$query = "SELECT * FROM skill_info where skillType = '3'";
$result = mysql_query($query) or die (mysql_error());

echo "<table border='0'>\n";
while ($row = mysql_fetch_array($result))
  {
  extract($row);
  $cost = getskillcost($skillID, $classid, $raceid);
  $chkcost = checkcost($cost, $cid);
   if ($chkcost == 1)
   {
  echo "<tr><td>\n";
  $preq= explode(",",$prereq);
  foreach ($preq as $value)
  {
  if ($value == $n)
  {
   echo "<a href='home.php?action=addskill&skillID=$skillID'>$skillName</a> ($cost)";	 
  }
  if ($value != $n)
   {
   $canhave = checkprereq($skillID, $cid, $value);
     if ($canhave==1)
     {
      echo "<a href='home.php?action=addskill&skillID=$skillID'>$skillName</a> ($cost)";	 
     }
   }
  }
  }
  echo "</td></tr>\n";
  }

 

The function looks like this.

 

function checkprereq($skillID, $cid, $value)
{

  $query = "SELECT * FROM skill1 where cid = '$cid' order by skillID";
  $result = mysql_query($query) or die (mysql_error());
  while ($row = mysql_fetch_array($result))
  {
  $prereq = $row['skillID']; 
  if ($value == $prereq)
  {
  $canhave = 1;	
  }	



  if ($skillID=='27' || $skillID =='28')
  {
  if ($prereq=='18' || $prereq=='19' || $prereq=='22' || $prereq=='23'  || $prereq=='30' || $prereq=='31')
  {
  $canhave = 1;
  }
  else
  {
  $canhave = 0;		
  }
} 	   

return $canhave;
}

 

I have looked this code over and I can't see where there might be an issue. I can't see why it is not defining the variable $canhave.. just a bit confused.

 

any suggestions would be appreciated.

 

Thanks

Bill

Link to comment
https://forums.phpfreaks.com/topic/187662-notice-undefined-variable-php-5/
Share on other sites

	if ($skillID == '27' || $skillID == '28') {
		if ($prereq == '18' || $prereq == '19' || $prereq == '22' || $prereq == '23' || $prereq == '30' || $prereq == '31') {
			$canhave = 1;
		} else {
			$canhave = 0;
		}
	}

That is your problem. If the skillID isn't 27 or 28 then the $canhave doesn't get set. you at the start of your function put

 

$canhave = 0;

and it should all work fine

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.