$GLOBALS variables

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
SerbanSpire
Posts: 19
Joined: Fri Dec 14, 2012 11:23 am

$GLOBALS variables

Post by SerbanSpire »

I hereby provide you another one of my problems: hehe

I have this code snippet from selected_char_id.php :

Code: Select all

<?php

include 'connect.php';


if(!isset($_SESSION)) { 
	session_start(); }

if(isset($_POST['select_character']))
{
$details = explode(" ", $_POST['select_character']);

echo "<br>";

$query = sprintf("SELECT char_id FROM characters WHERE char_name = '%s' AND char_dynasty = '%s'", $details[1], $details[2]);
$result = mysql_query($query);
list($selected_char_id) = mysql_fetch_row($result);


}
?>
And this code snippet from titles.php:

Code: Select all

<?php
include 'connect.php';
include 'selected_char_id.php';

if(!isset($_SESSION)) { 
	session_start(); }

$query = sprintf("SELECT id FROM users WHERE UPPER(username) = UPPER('%s')",
			mysql_real_escape_string($_SESSION['username']));
$result = mysql_query($query);
list($userID) = mysql_fetch_row($result);


$query = sprintf("SELECT name, type from cities where user_id = ('%d') AND char_id = '%d' order by type desc",$userID, $selected_char_id);
$result = mysql_query($query);

echo "Holdings: <br>";

while($holding = mysql_fetch_assoc($result)){
$name = $holding['name'];
$type = $holding['type'];
echo "<a href = http://localhost/Caught%20in%20the%20Middle/holding_ui.php >Mayor of " . $name . " " . $type . "<br><br></a>";
}

?>
I am perfectly aware that this will never work as $selected_char_id is never declared in titles.php, but how could i make this variable global? I couldn't understand from any source how to do this. I have no idea how to use $GLOBALS.................. $selected_char_id will be used multiple times throught-out my scripts so I really need to make this variable accessible to me. Another idea was to get the $char_name and $char_dynasty through the url using $_GET, but I have no idea how to do that either. Can you please help me?
Don't worry about $_POST['select_character'] as it comes from another script loaded with a select option, and i used explode() because the aforementioned var is composed of multiple columns from the database.
User avatar
MikuzA
Posts: 395
Joined: Thu Aug 08, 2013 8:57 am

Re: $GLOBALS variables

Post by MikuzA »

Hello,

If you declare it inside an included php, it should roll over without any action.

so,
to proof this you can try,

#test.php

Code: Select all

<?PHP
$this_is_a_normal_variable = "I Like turtles!";
?>
#test2.php

Code: Select all

<?PHP
include("test.php");
print $this_is_a_normal_variable;
?>
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
Post Reply

Return to “Beginner Help and Support”