custom functions and db connections...

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

custom functions and db connections...

Post 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.
New Site Coming Soon! Stay tuned :D
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: custom functions and db connections...

Post 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;
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: custom functions and db connections...

Post by Torniquet »

nope that didnt do it :s
New Site Coming Soon! Stay tuned :D
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: custom functions and db connections...

Post by Chris »

Then I would maybe suggest writing everything in classes.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: custom functions and db connections...

Post 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;
}
Post Reply

Return to “Advanced Help and Support”