schasecraven Posted August 24, 2011 Share Posted August 24, 2011 Firstly, here's the code i'm having trouble with: 'products.php': <?php class product{ public $id,$brand,$partnumber,$price,$per; public function dump(){ echo "dump"; echo "<table><tr><td>$id</td><td>$brand</td><td>$partnumber</td><td>$price</td><td>$per</td></tr></table>"; } public function search($strSearch){ include ("db.php"); $query = "SELECT * FROM products WHERE partnumber=\"$strSearch\";"; $db_conn = mysql_connect($dbserver,$user,$pass); @mysql_select_db($database) or die("unable to connect to database"); $result = mysql_query($query); $num_rows = mysql_num_rows($result); if($num_rows==1){ while ($row = mysql_fetch_assoc($result)) { $id = $row["id"]; $brand = $row["brand"]; $partnumber = $row["partnumber"]; $price = $row["price"]; $per = $row["per"]; //DEBUG #1: echo "<table><tr><td>$id</td><td>$brand</td><td>$partnumber</td><td>$price</td><td>$per</td></tr></table>"; } } if($num_rows>1){ // code selection (disambiguation) here } if($num_rows<1){ // code secondary searchs here } } } ?> findprod.php: <body> <form action="findprod.php" method="GET"> <input type="text" name="searchstr"> <input type="submit" value="Search"> </form> </body> <?php if(isset($_GET['searchstr'])){ $searchStr = $_GET['searchstr']; include ("products.php"); $np = new product(); $np->search($searchStr); $np->dump(); echo $np->brand; } ?> Here's my problem: after typing in a part-number to search for, I'm initializing a new 'product' class, and searching for that partnumber. I know that the query is returning 1 record just like it should. The 'DEBUG#1' line that echo's the table works just fine. The problem is, when I try to call the 'dump' method (from 'findprod.php') the variables are empty. Same thing if I try to access the variables directly. Am I doing something wrong? Perhaps I don't understand classes in PHP correctly, but it's almost as if the methods in the 'product' class aren't sharing the variables between each other. Can anyone show me what I can do to make this work? BTW: I have google'd everything I could think of for the past few days, but I just can't seem to find anything to solve this problem on my own. I've thought of possibly using SESSION variables to accomplish this task, but I'd rather not. I'm also just really curious to know why this isn't working. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 24, 2011 Share Posted August 24, 2011 Your problem is due to variable scope. A variable defined outside a function and one defined inside a function are two completely different variables (unless you do some certain things to tell the application that they are to be the same). For classes there is an easy solution. You define the "properties" (i.e. variables: $id,$brand,$partnumber,$price,$per) at the top of the class. Then inside your functions if you want to set or access the values for those properties you would do so like this $this->id = 1; So in the method (i.e. function) search you would do the following if($num_rows==1){ while ($row = mysql_fetch_assoc($result)) { $this->id = $row["id"]; $this->brand = $row["brand"]; $this->partnumber = $row["partnumber"]; $this->price = $row["price"]; $this->per = $row["per"]; } You need to do that within any method (function) of the class where you want to set/access the properties (variables) of the class. Although a simpler solution would be to simple have a class property called $data that is an array. Then you could just use $this->data = $row; Quote Link to comment Share on other sites More sharing options...
schasecraven Posted August 24, 2011 Author Share Posted August 24, 2011 Thank you very much! That works like a charm! So, basically what's happening is that the class has it's variable set, and each function has it's own variable set. In order to reference a class variable from a function, I have to use '$this' to refer back to the current instance of the class... right? Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 24, 2011 Share Posted August 24, 2011 Thank you very much! That works like a charm! So, basically what's happening is that the class has it's variable set, and each function has it's own variable set. In order to reference a class variable from a function, I have to use '$this' to refer back to the current instance of the class... right? Something like that. I understand how it works, but I'm not always accurate on the terminology. So, I don't want to state something that could be misinterpreted. Take a look at a PHP Class Tutorial (there is one on this site) to get a more thorough explanation. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.