Current location: Hot Scripts Forums » Programming Languages » PHP » Php Help Please !!!


Php Help Please !!!

Reply
  #1 (permalink)  
Old 03-09-05, 08:47 AM
RatherSurf RatherSurf is offline
Newbie Coder
 
Join Date: Feb 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
$_SESSION help please !!!

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$rowOCI_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$rowOCI_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$rowOCI_ASSOC))
        {
        
        
$_SESSION['schoolcode'] = $row['NAME'];
        
printf("<option value='%s'>%s</option>\n"htmlentities($row['SCHOOL']), htmlentities($row['NAME']));

        
        }
    }
}
//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'";

$statement OCIParse($conn$query);

if(
OCIExecute($statement))
    {
        while(
OCIFetchInto($statement$rowOCI_ASSOC))
        {
            
printf("<option value='%s'>%s, %s</option>\n"htmlentities($row['TEACHER_ID']), htmlentities($row['LAST_NAME']),htmlentities($row['FIRST_NAME']));
        }

    }
}
//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 "
;

$statement OCIParse($conn$query);

if(
OCIExecute($statement))
    {
        while(
OCIFetchInto($statement$rowOCI_ASSOC))
            {
                
printf("<option value='%s'>%s, %s</option>\n"htmlentities($row['STUDENT_ID']), htmlentities($row['LAST_NAME']),htmlentities($row['FIRST_NAME']));
            }
    }
}
?>

Last edited by RatherSurf; 03-09-05 at 09:37 AM.
Reply With Quote
  #2 (permalink)  
Old 03-09-05, 10:58 AM
kjmatthews kjmatthews is offline
Wannabe Coder
 
Join Date: Jun 2004
Location: Boston, MA
Posts: 134
Thanks: 0
Thanked 0 Times in 0 Posts
There are lots of names in this code. Which name is the one you wanted to make a session variable?
Reply With Quote
  #3 (permalink)  
Old 03-09-05, 12:11 PM
RatherSurf RatherSurf is offline
Newbie Coder
 
Join Date: Feb 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
sorry

'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

thanks
Reply With Quote
  #4 (permalink)  
Old 03-09-05, 03:48 PM
kjmatthews kjmatthews is offline
Wannabe Coder
 
Join Date: Jun 2004
Location: Boston, MA
Posts: 134
Thanks: 0
Thanked 0 Times in 0 Posts
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$rowOCI_ASSOC))
        {
        
        
printf("<option value='%s'>%s</option>\n"htmlentities($row['SCHOOL']), htmlentities($row['NAME']));

        
        }
    }
return 
$row['NAME']

Then, in test2.php, you replace the lines:
PHP Code:

<?

   teacherchoice
();
?>
with
PHP Code:

<?

   $somevar 
teacherchoice();
   
$_SESSION['schoolcode'] = $somevar;
?>
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.
Reply With Quote
  #5 (permalink)  
Old 03-10-05, 07:33 AM
RatherSurf RatherSurf is offline
Newbie Coder
 
Join Date: Feb 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
??

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??

<?
$somevar = teacherchoice();
$_SESSION['schoolcode'] = $somevar;
?>
Reply With Quote
  #6 (permalink)  
Old 03-10-05, 09:19 AM
moronovich moronovich is offline
Junior Code Guru
 
Join Date: Oct 2004
Posts: 460
Thanks: 0
Thanked 0 Times in 0 Posts
i'm not a php guru, but i hope the gurus let me tell some notes regarding the code:
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$rowOCI_ASSOC)) 
        { 
         
        
$_SESSION['schoolcode'] = $row['NAME']; 
        
printf("<option value='%s'>%s</option>\n"htmlentities($row['SCHOOL']), htmlentities($row['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...
Reply With Quote
  #7 (permalink)  
Old 03-14-05, 10:13 AM
RatherSurf RatherSurf is offline
Newbie Coder
 
Join Date: Feb 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks

..........
Reply With Quote
  #8 (permalink)  
Old 03-14-05, 10:14 AM
RatherSurf RatherSurf is offline
Newbie Coder
 
Join Date: Feb 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks

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
Reply With Quote
  #9 (permalink)  
Old 03-14-05, 12:08 PM
moronovich moronovich is offline
Junior Code Guru
 
Join Date: Oct 2004
Posts: 460
Thanks: 0
Thanked 0 Times in 0 Posts
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():
PHP Code:

//....deleted

<form method="GET" action="test2.php"> 
    <select name="schoolchoice"> 
        <? 
            schoolchoice
(); 
            print 
'Value stored in session, $_SESSION[\'schoolcode\'] : '.$_SESSION['schoolcode'];
        
?> 
    </select> 
    <input type="submit" /> 
</form>
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$rowOCI_ASSOC)) 
        { 
         
        
$_SESSION['schoolcode'][$row['SCHOOL']] = $row['NAME']; 
        
printf("<option value='%s'>%s</option>\n"htmlentities($row['SCHOOL']), htmlentities($row['NAME'])); 

         
        } 
    } 

your test2.php
PHP Code:

//...deleted

        elseif ($_GET['schoolchoice'])    
         
    {      
     
?> 
<!-- ##################################################  ##################################################   --> 
<!-- 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...

Last edited by moronovich; 03-14-05 at 12:29 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 10:17 AM
Mouseover with Single PHP page rjwebgraphix PHP 7 09-16-04 05:15 PM
php with Apache in windows eDevil PHP 3 08-08-04 12:03 AM
100 Web Templates & 10 PHP Scripts for sale! HostersUK.co.uk General Advertisements 0 01-10-04 12:31 AM


All times are GMT -5. The time now is 05:35 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.