Jump to content


Photo

return variable in functions


  • Please log in to reply
7 replies to this topic

#1 garry27

garry27
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 16 October 2006 - 10:21 PM

hi all,

how do you return a value from inside a function which you can use when you call it? i.e. in the following code, i'm trying to return the value of $account_type to the login function:

                       
if(login($_POST)== 'P')
		{
		$_SESSION['valid_user'] = $email;
		$member_homepage = 'pa_home.php';
		}
		
		if(login($_POST)== 'B')
		{
		$_SESSION['valid_user'] = $email;
		$member_homepage = 'ba_home.php';
		}
                          if(!login($_POST))
		echo 'membership not recognised';

TIA

Garry
 

     

#2 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 16 October 2006 - 10:33 PM

There's no function there so nothing to return from.

<?php
  // Returning a single value
  function Func(){
    $ReturnValue = 'Hello, World!';
    return $ReturnValue;
  }

  // Returning multiple values
  function FuncMany(){
    $Ret = Array();
    $Ret['Val1'] = $Val1;
    $Ret['Val2'] = $Val2;
    return $Ret;
  }
?>

PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#3 garry27

garry27
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 16 October 2006 - 10:59 PM

i've already defined the function login:
           
...
         $sql = "select Member.accType from Member
                  Where Member.userEmail='$email";
	    
         $account_type = $conn->query($sql); 
         return $account_type;
         }


now i'm trying to call the value of $account_type using post:

                 if(login($_POST)== 'P')
		{
		$_SESSION['valid_user'] = $email;
		$member_homepage = 'pa_home.php';
		}
		
		if(login($_POST)== 'B')
		{
		$_SESSION['valid_user'] = $email;
		$member_homepage = 'ba_home.php';
		}
                          if(!login($_POST))
		echo 'membership not recognised';


#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 16 October 2006 - 11:53 PM

Post your login function.

#5 garry27

garry27
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 17 October 2006 - 12:15 AM

i've been stuck on this for well over an hour now. any help would me much appreciated.



function login($email, $pwd)
{
        $conn = db_connect($account_type); 
        $sql = "select * from All_Users where userEmail='$email' and userPassword='$pwd'";     
        $result=$conn->query($sql); 
		   
        if ($result->num_rows >0 ) 
		{
		  $sql = "select Member.accType from Member
                                     Where Member.userEmail='$email'";
		 $account_type = $conn->query($sql);
		 
		 }
        return $account_type ;
       





<?php 

//Initiates php script containing functions all common functions 
session_start(); 
require_once('common.php'); 
$dbconnect = db_connect(); 

//outputs header info to brower with the following title 
echo xhtmlheader('Personal Account Registration Form'); 
  
//outputs the main navigation bar to the browser 
echo topnav_pa(); 




if (isset($_POST['email']) && isset ($_POST['pwd'])) 
{ 

        //if the user has just tried to log in 
        $email = $_POST['email']; 
        $pwd = $_POST['pwd']; 
  
        
        if(login($_POST)== 'B')
		{
		$_SESSION['valid_user'] = $email;
		$member_homepage = 'pa_home.php';
		}
		
		if(login($_POST)== 'P')
		{
		$_SESSION['valid_user'] = $email;
		$member_homepage = 'ba_home.php';
		}
        if(!login($_POST))
		echo 'membership not recognised';
} 
?> 

<!-- main body of page --> 
<div id="main"> 
  <h1>My Pub Crawl Planner</h1> 
  



<?php 
  if (isset($_SESSION['valid_user'])) 
  { 
    echo '<h1> You are logged in as: '.$_SESSION['valid_user'].' </h1><br />'; 
	 echo "<a href=$member_homepage>go to home page</a>";
	echo "<a class='logout' href='logout.php'>Log out</a>";
  } 
  else 
  { 
    if (isset($email)) 
    { 
      // if they've tried and failed to log in 
      echo 'Could not log you in.<br />'; 
    } 
    else 
    { 
      // they have not tried to log in yet or have logged out 
      echo 'You are not logged in.<br />'; 
    } 
  } 
?> 
        
 <p>Member Login </p>
 
 
 <?php 
 
 echo show_loginfrm();
 
 ?>
<!-- main body of page -->
<div id="main">
  <h1>My Pub Crawl Planner</h1>
 




<?php 
  if (isset($_SESSION['valid_user'])) 
  { 
    echo '<h1> You are logged in as: '.$_SESSION['valid_user'].' </h1><br />'; 
	 echo "<a href=$member_homepage>go to home page</a>";
	echo "<a class='logout' href='logout.php'>Log out</a>";
  } 
  else 
  { 
    if (isset($email)) 
    { 
      // if they've tried and failed to log in 
      echo 'Could not log you in.<br />'; 
    } 
    else 
    { 
      // they have not tried to log in yet or have logged out 
      echo 'You are not logged in.<br />'; 
    } 
  } 
?> 
        
 <p>Member Login </p>
 
 
 <?php 
 
 echo show_loginfrm();
 
 ?>


#6 garry27

garry27
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 17 October 2006 - 12:27 AM

i can log on now afer i deleted the stupid variable after db connection on login() but i'm still getting:

Warning: Missing argument 2 for login() in /home/unn_p921847/public_html/common.php on line 38

Warning: Missing argument 2 for login() in /home/unn_p921847/public_html/common.php on line 38

Warning: Missing argument 2 for login() in /home/unn_p921847/public_html/common.php on line 38

:

function login($email, $pwd)              //line 38
{
        $conn = db_connect(); 
        $sql = "select * from All_Users where userEmail='$email' and userPassword='$pwd'";     
        $result=$conn->query($sql); 
		   
        if ($result->num_rows >0 ) 
		{
		  $sql = "select Member.accType from Member
                  Where Member.userEmail='$email'";
		  $account_type = $conn->query($sql);
		 
	     }
         return true; 
}

???

#7 garry27

garry27
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 17 October 2006 - 01:54 AM

why isn't it working dammit?  :(

#8 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 17 October 2006 - 02:47 AM

it's because you've constructed your login function such that it requires you pass it the email and password that the user is attempting to login with.  NOT the array they're found in.  try this:

login($_POST['email'], $_POST['pwd'])

you can change your function back to returning the account_type variable, that wasn't the issue.  read a little bit on functions to learn how parameters and arguments work with custom functions.  a bit of reading on scope would help as well.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users