Danny620 Posted August 5, 2009 Share Posted August 5, 2009 the function stuff() creates the $passed and puts a 1 in $passed in my other function called levelup() i want it to be in there as well but when i echo out it in my inside my other fucntion nothing apears. <?php //require connection $dbc require_once ('mysqli_connect.php'); function stuff($cost,$dbc){ //function stuff() needs $cost,$dbc //Gets how much the user has of that stuff already //selects table stuff and querys for item,item_x,item_y // Query the database: $q = "SELECT item, item_x, item_y FROM `stuff` WHERE user_id = 1"; $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); $stuff = mysqli_fetch_array ($r, MYSQLI_ASSOC); //End of Hq level //fetch's an array and then puts the item's in vars $item = $stuff['item']; $item_x = $stuff['item_x']; $item_y = $stuff['item_y']; //Get's the users corrent level of the building they wish to update $q = "SELECT level FROM `buildings` WHERE user_id = 1 & building = '$cost'"; $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); $level = mysqli_fetch_array ($r, MYSQLI_ASSOC); $buildinglevel = $level['level']; //testing zone echo "item: $item<br />item_x: $item_x<br />item_y: $item_y<br />"; //end of testing zone //querys the dbc for how much it will cost the user to upgrade there building // Query the database: $q = "SELECT item, item_x, item_y FROM `$cost` WHERE level = '$buildinglevel'"; $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); $buidlingstuff = mysqli_fetch_array ($r, MYSQLI_ASSOC); //End of Hq level //Starts an array and stores the cost of each item it will cost to upgrade $costitem = $buidlingstuff['item']; $costitem_x = $buidlingstuff['item_x']; $costitem_y = $buidlingstuff['item_y']; echo "<br />costitem: $costitem<br />costitem_x: $costitem_x<br />costitem_y: $costitem_y"; // Assume invalid values: $im = $imx = $imy = FALSE; //item if($item >= $costitem){ $im = 1; }else{ echo "Not enough"; } if($item_x >= $costitem_x){ $imx = 1; }else{ echo "Not enough"; } if($item_y >= $costitem_y){ $imy = 1; }else{ echo "Not enough"; } if($im && $imx && $imy){ $passed = 1; }else{ $passed = 0; } echo $passed; } function levelup($dbc,$cost,$passed){ //New function called levelup needs $dbc and $cost //Get HQ level. echo $passed; // Query the database: $q = "SELECT level, points FROM `buildings` WHERE user_id = 1 & building = '$updatebuilding'"; $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); $nowhqlevel = mysqli_fetch_array ($r, MYSQLI_ASSOC); //End of Hq level $hqlevel = $nowhqlevel['level']; $pointsnow = $nowhqlevel['points']; //Get HQ Buildtime. // Query the database: $q = "SELECT time, points FROM `hq` WHERE level = $hqlevel"; $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); $nowbuildtime = mysqli_fetch_array ($r, MYSQLI_ASSOC); //End HQ Buildtime. $points = $nowbuildtime['points']; $buildtime = $nowbuildtime['time']; $builddate = date("Y-m-d H:i:s", time() + (60 * $buildtime)); $userid = 1;//turn it to session when programmed login //Upgrade to the next level. if(isset($_POST['update'])){ if($passed){ echo $passed; // Query the database: $q = "UPDATE construction SET complete = '$builddate', progress = 1 WHERE user_id = $userid LIMIT 1"; $r = @mysqli_query ($dbc, $q); } else { } } //Check to see if any buildings are in progress $q = "SELECT progress, complete FROM `construction` WHERE user_id = $userid & progress = 1"; $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); $progress = mysqli_fetch_array ($r, MYSQLI_ASSOC); //Settings $buildingpro = '0'; if($progress['progress'] == 1){ $msg = "You have 1 building in construction it will be finshished on " .$progress['complete']; //Check if building is complete $q = "SELECT complete FROM `construction` WHERE user_id = $userid & progress = 1"; $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); $check = mysqli_fetch_array ($r, MYSQLI_ASSOC); $date = date("Y-m-d H:i:s"); if($date >= $check['complete']){ $q = "UPDATE construction SET progress = 0 WHERE user_id = $userid & progress = 1 LIMIT 1"; $r = @mysqli_query ($dbc, $q); $upgraded = ++$hqlevel; $addpoints = $pointsnow + $points; echo $addpoints; $q = "UPDATE buildings SET level = '$upgraded', points = '$addpoints' WHERE user_id = $userid & building = 'hq' LIMIT 1"; $r = @mysqli_query ($dbc, $q); $msg = "Building is Complete"; }else{ $buildingpro = "<b>Building in progress</b>"; } } } stuff(hq,$dbc); levelup($dbc,$cost,$passed); ?> Link to comment https://forums.phpfreaks.com/topic/168966-function-problems/ Share on other sites More sharing options...
alexdemers Posted August 5, 2009 Share Posted August 5, 2009 I don't know if your code works but, you're missing the dollar sign on the first parameter of stuff(). Link to comment https://forums.phpfreaks.com/topic/168966-function-problems/#findComment-891463 Share on other sites More sharing options...
Danny620 Posted August 5, 2009 Author Share Posted August 5, 2009 thats in the require file Link to comment https://forums.phpfreaks.com/topic/168966-function-problems/#findComment-891464 Share on other sites More sharing options...
mikesta707 Posted August 5, 2009 Share Posted August 5, 2009 //this: stuff(hq,$dbc); //should be stuff($hq,$dbc); Link to comment https://forums.phpfreaks.com/topic/168966-function-problems/#findComment-891465 Share on other sites More sharing options...
Danny620 Posted August 5, 2009 Author Share Posted August 5, 2009 no thats right i am passing the world hq into that function its right Link to comment https://forums.phpfreaks.com/topic/168966-function-problems/#findComment-891467 Share on other sites More sharing options...
MatthewJ Posted August 5, 2009 Share Posted August 5, 2009 no thats right i am passing the world hq into that function its right If that is supposed to be "word" it would need to be quoted then stuff('hq', $bdc); Link to comment https://forums.phpfreaks.com/topic/168966-function-problems/#findComment-891471 Share on other sites More sharing options...
sasa Posted August 5, 2009 Share Posted August 5, 2009 1. change echo $passed; to return $passed; in the end of your 1st function. 2. change calls of your functions to $passed=stuff(hq,$dbc); levelup($dbc,$cost,$passed); Link to comment https://forums.phpfreaks.com/topic/168966-function-problems/#findComment-891480 Share on other sites More sharing options...
Danny620 Posted August 5, 2009 Author Share Posted August 5, 2009 sorry i dont think i have propley explained in the first stuff() function if that turns true it puts a 1 in $passed else 0 then i want it to be then passsed into the second function called function levelup($dbc,$cost,$passed) as u can see $passed should now have a 1 in or 0 so then in the script for the levelup function it can run or not depending on true or false Link to comment https://forums.phpfreaks.com/topic/168966-function-problems/#findComment-891509 Share on other sites More sharing options...
mikesta707 Posted August 5, 2009 Share Posted August 5, 2009 the problem is, as sasa said, the $passed variable is local to the stuff() function, so your levelup() function can't access it. If you want levelup to have access to the $Passed variable, you must return the value from the stuff function, as sasa said, and use that returned value in the levelup function. so in your stuff function change echo $passed; to return $passed; and when you call the levelup function do something like what sasa posted. hope that helps! Link to comment https://forums.phpfreaks.com/topic/168966-function-problems/#findComment-891522 Share on other sites More sharing options...
watsmyname Posted August 5, 2009 Share Posted August 5, 2009 sorry i dont think i have propley explained in the first stuff() function if that turns true it puts a 1 in $passed else 0 then i want it to be then passsed into the second function called function levelup($dbc,$cost,$passed) as u can see $passed should now have a 1 in or 0 so then in the script for the levelup function it can run or not depending on true or false use return $passed in function stuff instead of echo well your function stuff just returns 1 or 0, so you dont need to call stuff function. Just call levelup function like this levelup($dbc,$cost,stuff('hq',$dbc)); Link to comment https://forums.phpfreaks.com/topic/168966-function-problems/#findComment-891524 Share on other sites More sharing options...
watsmyname Posted August 5, 2009 Share Posted August 5, 2009 well my mistake neglect my last post declare variable $passed before the function, like $passed=""; function stuff($cost,$dbc) { //your codes here } Link to comment https://forums.phpfreaks.com/topic/168966-function-problems/#findComment-891526 Share on other sites More sharing options...
Danny620 Posted August 5, 2009 Author Share Posted August 5, 2009 i cant beacuse stuff desides wether or not its true or not and what i want it to do is pass 1 or 0 to the other function Link to comment https://forums.phpfreaks.com/topic/168966-function-problems/#findComment-891572 Share on other sites More sharing options...
mikesta707 Posted August 5, 2009 Share Posted August 5, 2009 ... read this basic tutorial on php functions. http://www.w3schools.com/PHP/php_functions.asp pay specific attention to the return values, and parameters section Link to comment https://forums.phpfreaks.com/topic/168966-function-problems/#findComment-891573 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.