Jump to content

Recommended Posts

right i have try ed return but i don't understand does return stop the script and does it e.g $passed = 1;

return $passed would this return 1 and would that be accessable outside the function like gobol.

 

what it should do is the script in the stuff()function return a value of 1 or 0 so that it can be used in another function called levelup();

 

<?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 = "hello";
return $passed;
}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/169262-its-killing-me-now/
Share on other sites

return does not act as global. If you pass return a variable it will return the value of that variable. As you are now returning a value from your function you will need to capture this value when you call the function eg

 

$my_var = your_function();

 

$my_var will capture what was returned from your function.

 

So this line

stuff(hq,$dbc);

 

needs to be

$passed = stuff(hq,$dbc);

 

You should have a read up on the return keyword in the manual here

Link to comment
https://forums.phpfreaks.com/topic/169262-its-killing-me-now/#findComment-893186
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.