Jump to content


Photo

Classes Problem


  • Please log in to reply
3 replies to this topic

#1 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 20 October 2006 - 12:30 AM

I have 2 files in use.

One is index.php, the other is classes.php. Index.php contains all of the website layout and executes the code that is contained in a class (myclass), the class(myclass) is defined in classes.php. Now i have this line of code at the top of index.php:

include('classes.php');

error_reporting(E_ALL);

I then have a function in the class(myclass) called check_priv(); Basically all it does is checks the database and the table users for the priv of certain users. Another function that is already executed (and works) connects and selects the database. Now here is the exact code for function check_priv()...

	function check_priv() {
		
		if (isset($_SESSION['user']) && isset($_SESSION['pass'])) {
		
			$user = $_SESSION['user'];
			$pass = $_SESSION['pass'];	
	
			$query = "SELECT priv FROM users WHERE user='$user' AND pass='$pass'";
		
			$priv = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
						 			
		}						
	} 

But, whenever i try and use $priv in index.php (like if ($priv == 1)) it gives me this error...

Notice: Undefined variable: priv in /home/wintersw/public_html/developer/index.php on line 77


I have tried adding a public variable to the class called $priv, then definining it using that method...

Whats wrong and how do i fix it?

- Cheers, Daniel

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 20 October 2006 - 12:50 AM

first of all, did you make a new object variable for your class?

2nd of all, you have to actually return $priv inside your function.

example:
<?php
   class blah {
      function check_priv() {
         if (isset($_SESSION['user']) && isset($_SESSION['pass'])) {
             $user = $_SESSION['user'];
             $pass = $_SESSION['pass'];	
             $query = "SELECT priv FROM users WHERE user='$user' AND pass='$pass'";
             $priv = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
             return $priv;
         } // end if					
      } // end function check_priv
   } // end class blah

   $blarg = new blah;
  
   $result = $blarg->check_priv();

   if ($result) {
      // do something with result
   } else {
      // user not authorized
   }
?>

that's just based off your current code tho. you should probably put pretty much all of that inside your class in the first place.

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 20 October 2006 - 01:23 AM

How do u mean return it?

- Cheers, Daniel

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 20 October 2006 - 01:27 AM

see my previous post i updated it.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users