Jump to content
bschultz

Function with Required File and Variables

Recommended Posts

I have a php file with a bunch of variables declared.  I now need a function to put data on a separate page that pulls variables from the required page.

So, on the required page, I have a variable set like this:  $qb1 = "John Doe";

The function on the new script is called like this:  getplayer($qb1);

I've tried this without the dollar sign in the function like this: getplayer(qb1); and then adding the $dollar sign in the rest of the function code...but it never populates the sql query correctly.  I'm guessing this is a case for variable variables or something similar, but I don't know how to get the variable value from the required page.  Any ideas?  Thanks.

Here's the code:

	<?php
	function getplayer($position)
{ // start function
	//$new_position = "$" . $position;
$new_position = $position;
	
$file = "their.php";
require "$file";
//echo $qb1;    this displays the name John Doe...as $qb1 is declared in the required file
	
$db_select_their = "bschultz_their";
	$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "db_name";
	// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
	$sql = "SELECT * FROM (select number as position_number, name as position_name, pronunciation as position_pronunciation, pos as position_pos, height as position_height, weight as position_weight, year as position_year, city as position_city, state as position_state, notes as position_notes FROM $db_select_their WHERE name = '$new_position') as position";
	//$sql = "SELECT * FROM bschultz_their WHERE name = $new_position";
   
echo $sql; //exit;        
/*
this displays this text...   SELECT * FROM (select number as position_number, name as position_name, pronunciation as position_pronunciation, pos as position_pos, height as position_height, weight as position_weight, year as position_year, city as  position_city, state as position_state, notes as position_notes FROM bschultz_their WHERE name = '') as position

The name is empty
*/
	$result = $conn->query($sql);
	if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "#: " . $position_number . " - Name: " . $position_name . "";
    }
} else {
    echo "0 results";
}
	} // end function
	
getplayer("${$qb1}");
$conn->close();
?>
Edited by bschultz

Share this post


Link to post
Share on other sites

You are overthinking it using variable variables.

if you have ...

$qb1 = 'John'

then ${$qb1) is looking for tha contents of $John (which doesn't exist and hnce the blank name). You need to turn your error reporting on.

Use

getplayer($qb1);

 

Share this post


Link to post
Share on other sites

I've tried that...the echo of the sql shows the name is blank

Share this post


Link to post
Share on other sites

I got it working...left the dollar sign out of the function call getplayer('qb1'); and then an if / elseif statement inside the function:

if ($position == 'qb1') { $new_position = $qb1; }
elseif ($position == 'qb2') { $new_position = $qb2; }

Thanks....

Edited by bschultz

Share this post


Link to post
Share on other sites

Alternatively, you could use an associative array for storing the player names. So your their.php file might look something like this:

<?php
$players = array(
	'qb1' => 'John Doe',
	'qb2' => 'Sam Smith',
);
?>

 

Then your function and function call could be

<?php
function getplayer($position) {
	//GET PLAYER INFORMATION
	$file = "their.php";
	require $file;
	
	//IF PLAYER FOUND, GET NAME
	$player = false;  //set default value in case player isn't found
	if(isset($players[$position])) {
		$player = $players[$position];
	}
	
	//RETURN RESULT
	return $player;
}


$player = getplayer('qb1');
var_dump($player);
?>

 

Share this post


Link to post
Share on other sites

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.