Jump to content

[SOLVED] Easy One (I hope)


inspireddesign

Recommended Posts

Hello all,

 

I have the following code that I can't seem to pass the var to the method inside it's class.  Can someone help.  It says it's missing an argument.

 

My problem: I'm trying to echo the value entered when I call the class:  So in this case I'm trying to echo '40' and when I get that to work, it will be the id used to fetch the data from the db.  Thanks!

 

<?

$openclose = new showHide();
$openclose->showHide(40);

?>

 

Here is the class:

<?

class showHide {

private $id;
private $returnResults;

	public function showHide($id){

		global $contacts; // Bring in MySQL DB Link

		echo $this->id; die;

			$query = "SELECT * FROM contacts WHERE contact_id = $this->id";
			$result = mysql_query($query, $contacts)
				or die("ERROR: Unable to fetch field value: \n" . mysql_error());
			$this->returnResults = mysql_fetch_assoc($result);

			echo $this->returnResults['contact_primary'] . ' - ' . $this->returnResults['contact_id']. '<br />' ;

	}


}	


?>

Link to comment
https://forums.phpfreaks.com/topic/169421-solved-easy-one-i-hope/
Share on other sites

okay your trying to echo $this->id but you havn't set it, $id has been passed but its not set to $this->id

 

The reason for the missing an argument is because your not setting it..

when a function name has the same name as the class the function is called during the construction stage

so

  $openclose = new showHide();

is calling the function and you can see your not setting a var!

 

so a simple fix would be

<?php
  $openclose = new showHide(40); //construction
  echo "<br>".$openclose->WhatsTheID(); //another function 

class showHide {
   
   private $id;
   private $returnResults;
   
      public function showHide($id){ //construction //pass the var
      
         global $contacts; // Bring in MySQL DB Link
         //we will use this in other places so we will set it to the private id
         $this->id = $id; //set private var to passed one
         
         echo $this->id;
         return;
         
            $query = "SELECT * FROM contacts WHERE contact_id = $this->id";
            $result = mysql_query($query, $contacts)
               or die("ERROR: Unable to fetch field value: \n" . mysql_error());
            $this->returnResults = mysql_fetch_assoc($result);
            
            echo $this->returnResults['contact_primary'] . ' - ' . $this->returnResults['contact_id']. '<br />' ;
            
      }
      function WhatsTheID()
      {
	return $this->id;
      }


}   


?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.