Page 1 of 1

custom functions and db connections...

Posted: Wed Apr 14, 2010 9:56 pm
by Torniquet
Ok i am currently redesigning how my map works behind the scenes...

and something i have noticed, is that with functions, i am having to include the databse connection script inside the newly created function... aswell as at the start -.- lol

is there any way of getting custom functions to use the single included connection script added at the top of the page?

the same is happening with my $uid variable... i have to reset that inside the function aswell ¬¬

Code: Select all

require "../_scripts/session_check.php";
require "../_scripts/connect_to_db.php";

$uid = $_SESSION['uid'];

$locationRow = mysql_fetch_assoc(mysql_query("SELECT * FROM user_map_location WHERE user_id='$uid'",$dbconec1));

$mapnameinfo = mysql_fetch_assoc(mysql_query("SELECT map_name, map_type, x_size, y_size FROM map_name WHERE map_id='$locationRow[map_id]'",$dbconec1));



function makeHunt(){
	require "../_scripts/connect_to_db.php";
	$uid = $_SESSION['uid'];
	$locationRow = mysql_fetch_assoc(mysql_query("SELECT * FROM user_map_location WHERE user_id='$uid'",$dbconec1));
	$table = "active_info_" . $locationRow['map_id'];
	$huntInfo = mysql_fetch_assoc(mysql_query("SELECT * FROM `$table` WHERE type='2'",$dbconec1));
	
	$length = strlen($huntInfo['x_loc']);
	$xLocN = 1;
	for($x=0; $x<=$length; $x++){
		if(mb_substr($huntInfo['x_loc'],$x,1) != ";"){
				$huntXloc[$n] = mb_substr($huntInfo['x_loc'],$x,1);
				$huntYloc[$n] = mb_substr($huntInfo['y_loc'],$x,1);
				$huntXsec[$n] = mb_substr($huntInfo['x_sec'],$x,1);
				$huntYsec[$n] = mb_substr($huntInfo['y_sec'],$x,1);
				$huntLvl[$n] = mb_substr($huntInfo['level'],$x,1);
			} else {
				$n++;
			}
	}
	
	for($x=1; $x<=$n; $x++){
		if(($huntXloc[$x] == $locationRow['map_x']) && ($huntYloc[$x] == $locationRow['map_y'])){
			print "<div id='MAPsection" . $huntXsec[$x] . $huntYsec[$x] . "'>";
			
			$display = "Hunt<br>Difficulty level: " . $huntLvl[$x];
			print "<a onMouseover=\"ddrivetip('" . $display . "')\" onMouseout=\"hideddrivetip()\" href='hunt.php?map=0'><img src='../_img/map/hunt.png' border='0'></a>
			</div>";
		}
	}
	
}

is abit of a pain tbh and surly it is uneeded.

Re: custom functions and db connections...

Posted: Thu Apr 15, 2010 1:32 pm
by Chris
I've never come accross that problem before, I think it's because I always have all my variables globaled in my PHP settings,

try global your variables after they've been set:

Code: Select all

$uid = $_SESSION['uid'];
global $uid;
 

Re: custom functions and db connections...

Posted: Thu Apr 15, 2010 7:58 pm
by Torniquet
nope that didnt do it :s

Re: custom functions and db connections...

Posted: Fri Apr 16, 2010 9:10 pm
by Chris
Then I would maybe suggest writing everything in classes.

Re: custom functions and db connections...

Posted: Sat Apr 24, 2010 4:09 pm
by hallsofvallhalla
PHP is a pain when it comes to local scope. If global does not work (which is not uncommon) then all you need to do is pass the variables through the function.

$uid = $_SESSION['uid'];

hunt($uid);

function hunt($newuid)
{
$uid = $newuid;
}