overlordofevil Posted January 8, 2010 Share Posted January 8, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/187662-notice-undefined-variable-php-5/ Share on other sites More sharing options...
JAY6390 Posted January 8, 2010 Share Posted January 8, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/187662-notice-undefined-variable-php-5/#findComment-990740 Share on other sites More sharing options...
overlordofevil Posted January 8, 2010 Author Share Posted January 8, 2010 cool thanks for the suggestion.. I think I got it fixed. Quote Link to comment https://forums.phpfreaks.com/topic/187662-notice-undefined-variable-php-5/#findComment-990747 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.