Here is the code to two pages.... I am trying to load 'NAME' from functions.php into a $_SESSION array so i can print that chosen name anywhere i need elsewhere on the page test2.php.... can anyone please help????
test2.php
PHP Code:
<?
//ini_set('session.use_trans_sid', false);
session_start();
//require('include/general.php');
require_once('functions.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Testing</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?
if (!$_GET) {
?>
<!-- #################################################################################################### -->
<!-- Here is where we start the first form.....the school drop down menu -->
<form method="GET" action="test2.php">
<select name="schoolchoice">
<?
schoolchoice();
?>
</select>
<input type="submit" />
</form>
<?
}
elseif ($_GET['schoolchoice'])
{
?>
<!-- #################################################################################################### -->
<!-- Here is where we display the school choice static on the teacher choice page -->
<?
$_GET[$_SESSION['schoolcode']];
print "<b>";
print $_SESSION['schoolcode'];
print "</b>";
print "<br>";
?>
<!-- #################################################################################################### -->
<!-- Here is where we start the form for the teacher choice drop down menu..... -->
<form method="GET" action="test2.php">
<select name="teacherchoice">
<?
teacherchoice();
?>
</select>
<input type="submit" />
</form>
<?
}
elseif ($_GET['teacherchoice'])
{
?>
<!-- #################################################################################################### -->
<!-- Here is where we display the school choice and teacher choice static on the student choice page -->
<?
//Attempt to print the school that was selected in the previous drop down
print "<b>";
print $_SESSION['schoolcode'];
print "<br>";
//connect to the db
$conn = ociLogon("K2DEV", "K2DEV", "SBV3");
//bring over teacherchoice variable
$teacherchoice=$_GET['teacherchoice'];
//run new query to print teacherchoice on studentchoice page
$query = "SELECT A.LAST_NAME,A.FIRST_NAME,A.TEACHER_ID,B.NAME,B.SCHOOL
FROM teachers A, Schools B
WHERE B.SCHOOL = A.SCHOOL
AND A.TEACHER_ID = '$teacherchoice'";
// This parses the SQL Statement for use
$statement = OCIParse($conn, $query);
// OCIExecute returns true/false if the query succedded in runnin
// Note that this does *NOT COMMIT* by default, if you are using INSERT/UPDATE/DELETE
if(OCIExecute($statement))
//this starts a flag.....so that it will only display the name one time
{
$flag = '0';
while(OCIFetchInto($statement, $row, OCI_ASSOC))
{
if($flag == 0)
{
printf("<option value='%s'>%s, %s</option>\n", htmlentities($row['TEACHER_ID']), htmlentities($row['LAST_NAME']),htmlentities($row['FIRST_NAME']));
$flag = 1;
print "</b>";
}
}
}
//this ends the flag....name should have only printed once on the student choice page....
?>
<!-- #################################################################################################### -->
<!-- Here is where we start the drop down menu form for the student choice based on the
teacher chosen in the previous drop down menu -->
<form method="GET" action="test2.php">
<select name="studentchoice">
<?
studentchoice();
?>
</select>
<input type="submit" />
</form>
<?
}
elseif ($_GET['studentchoice'])
{
// ##############################################################################
// Here is where we display the teacher choice static on the student choice page
//connect to the db
$conn = ociLogon("K2DEV", "K2DEV", "SBV3");
//bring over teacherchoice variable
$studentchoice=$_GET['studentchoice'];
//run new query to print studentchoice on studentform page
$query = "SELECT A.STUDENT_ID, A.LAST_NAME, A.FIRST_NAME
FROM students A, stu_enr B
WHERE A.STUDENT_ID = B.STUDENT_ID
AND B.STUDENT_ID = '$studentchoice'
";
// This parses the SQL Statement for use
$statement = OCIParse($conn, $query);
// OCIExecute returns true/false if the query succedded in runnin
// Note that this does *NOT COMMIT* by default, if you are using INSERT/UPDATE/DELETE
if(OCIExecute($statement))
//this starts a flag.....so that it will only display the name one time
{
$flag = '0';
while(OCIFetchInto($statement, $row, OCI_ASSOC))
{
if($flag == 0)
{
print "<b>";
printf("<option value='%s'>%s, %s</option>\n", htmlentities($row['STUDENT_ID']), htmlentities($row['LAST_NAME']),htmlentities($row['FIRST_NAME']));
$flag = 1;
print "</b>";
}
}
}
//this ends the flag....name should have only printed once on the student choice page....
// now you do some real stuff, but you haven't said what ...
// maybe you redirect to another page? ;-)
}
else
{
// there's something wrong, so give an error message here...
}
?>
</body>
</html>
functions.php
PHP Code:
<?php
session_start();
//function for schoolchoice
function schoolchoice()
{
$conn = ociLogon("K2DEV", "K2DEV", "SBV3");
$query = "SELECT * FROM schools ORDER by Name";
$statement = OCIParse($conn, $query);
if(OCIExecute($statement))
{
while(OCIFetchInto($statement, $row, OCI_ASSOC))
{
}
}
}
//function for teacherchoice
function teacherchoice()
{
$conn = ociLogon("K2DEV", "K2DEV", "SBV3");
$schoolchoice=$_GET['schoolchoice'];
$query = "SELECT A.LAST_NAME,A.FIRST_NAME,A.TEACHER_ID,B.NAME,B.SCHOOL
FROM teachers A, Schools B
WHERE B.SCHOOL = A.SCHOOL
AND A.SCHOOL = '$schoolchoice'";
}
}
//function for studentchoice
function studentchoice()
{
$conn = ociLogon("K2DEV", "K2DEV", "SBV3");
$teacherchoice=$_GET['teacherchoice'];
$query = "SELECT A.STUDENT_ID, A.LAST_NAME, A.FIRST_NAME
FROM students A, teachers B, stu_enr C
WHERE A.STUDENT_ID = C.STUDENT_ID
AND C.SCHOOL = B.SCHOOL
AND C.HOMEROOM = B.HOMEROOM
AND B.TEACHER_ID = '$teacherchoice'
ORDER BY LAST_NAME ";
'NAME'.... i attempted to already .... i guess i did it the wrong way ... it is function schoolchoice on functions.php and i want to display it above teacher choice "static" on test2.php
Okay, I believe your problem is variable scope. In PHP, variable declared within functions are ONLY available within that function. In order to make it available outside the function, here is what you can do:
PHP Code:
function schoolchoice()
{
$conn = ociLogon("K2DEV", "K2DEV", "SBV3");
$query = "SELECT * FROM schools ORDER by Name";
$statement = OCIParse($conn, $query);
if(OCIExecute($statement))
{
while(OCIFetchInto($statement, $row, OCI_ASSOC))
{
This way the variable that you return is assigned to $somevar, which is then turned into a session variable in the global scope. Alternatively, you can just do $_SESSION['schoolcode'] = teacherchoice();
Please correct me if I'm wrong about this, PHP gurus.
I see that i return the $row['NAME'] - - - how would i print that later above this statement.... i put this code in there and all of the drop downs work still but i need to print that name??
won't work as you wish. if there are x iterations, superglobal $_SESSION['schoolcode'] will be written x times.
you may also note, after returning from the function, you'll get the last $row['NAME'] and not the chosen one.
from your code flow, i spot something weird with your algorithm. you said that you want to get the chosen option in test2.php. anyway, you set the session before the form submitted. how could you fetch the value? even user hasn't decided at all.
about variable scope, your function is syntactically ok. there is nothing wrong if you write like this:
PHP Code:
function foo () {
$_SESSION['bar'] = '';
}
unlike variables declared within functions, $_SESSION together with $_ENV, $_GET,$_POST,and $_COOKIE (or the EGPCS) are superglobals which are available within whole script. When a script containing superglobal (for example $_SESSION), ZE will flag that for fetching from the global symbol table. Fyi, php variables are stored in hash table. For superglobals, the target hashtable is resolved at compile time while other variable lookups are resolved at run time.
That's why, if you're calling your superglobal from function, you're save. Anyway, if you want to call global variable outside the function you need to declare the variable as global (eg: global $foo
about your problem, imho it's (unintentional) design flaw. i'm sure you can handle it and find the appropriate solution.
regards,
__________________
just an ignorant noob with moronic solution...
moronovich --- i appreciate the reply .. The other example didnt work quite like i had planned. I will try the (global $foo) to see if that works out. Thanks again for your help
hi rathersurf,
either global $foo or $_SESSION['foo'] won't work if you don't change the algorithm. please review your code
PHP Code:
//.....deleted
<body>
<?
if (!$_GET) {
?> <!-- ################################################## ################################################## -->
<!-- Here is where we start the first form.....the school drop down menu -->
<form method="GET" action="test2.php">
<select name="schoolchoice">
<?
schoolchoice();
?> </select>
<input type="submit" />
</form>
<?
}
elseif ($_GET['schoolchoice'])
{
?> <!-- ################################################## ################################################## -->
<!-- Here is where we display the school choice static on the teacher choice page -->
<?
print "<b>";
print $_SESSION['schoolcode'];
print "</b>";
print "<br>";
?>
if you modify the script a bit to print the session value after function schoolchoice():
You will get the last value from the database and not the one chosen by user.
If you want to do so, with consequence of bloated session file, you can try this:
-your functions.php
PHP Code:
function schoolchoice()
{
$conn = ociLogon("K2DEV", "K2DEV", "SBV3");
$query = "SELECT * FROM schools ORDER by Name";
$statement = OCIParse($conn, $query);
if(OCIExecute($statement))
{
while(OCIFetchInto($statement, $row, OCI_ASSOC))
{
?>
<!-- ################################################## ################################################## -->
<!-- Here is where we display the school choice static on the teacher choice page -->
<?
print "<b>";
$choice = $_GET['schoolchoice'];
print Selected school: $_SESSION['schoolcode'][$choice];
print "</b>";
print "<br>";
?>
anyway, as i told you, this will lead into bloated session file. Now, it's you to choose.
regards,
ps:
of course, i don't guarantee the code to work..
__________________
just an ignorant noob with moronic solution...