Jump to content

function problems


Danny620

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.