Accessing variables outside of a class and function

user4742637

I am trying to get the value of a function inside of a class.

classes.php

 class luresClass {

  public function lureSelect() {

   global $lureChoice;
    if ($_POST['airtemp'] == 2 && $_POST['watertemp'] == 5) { 
          $lureChoice = 1;
         }
    else {$lureChoice = 0;}

 }   

}

This is the main file (index.php) that needs to access the value of $lurechoice.

if (isset($_POST['submit'])) {

$conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

    $displayLureChoice = new luresClass(); 
    $displayLureChoice->lureSelect();
    $stmt = $conn->prepare("SELECT * FROM ".$tbl."lure WHERE id = ".$lureChoice."");    
    $stmt->execute();

    while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
          echo "Lure Choice: ".$row['type']. "<br />Color: " .$row['color']. "<br /><br />";
    }
}

The user chooses certain items from a form and it is to return the if/else values.

I have tried making $lurechoice a global variable within the function lureSelect() in the classes.php file but doesn't work. I tried making it a public var in the class but that failed as well.

Thanks for the guidance.

Dan

You don't need to use globals you simply set the variable in your class and then use a function pass it back:

Class

class luresClass {

    public $lureChoice;

    public function lureSelect() {

        if ($_POST['airtemp'] == 2 && $_POST['watertemp'] == 5) {

            $this->lureChoice = 1;
        }
        else {
            $this->lureChoice = 0;
        }
    }  

    public function getLureChoice(){
        return $this->lureChoice;
    } 

}

Code

$displayLureChoice = new luresClass(); 
$displayLureChoice->lureSelect();

$lureChoice = $displayLureChoice->getLureChoice();

$stmt = $conn->prepare("SELECT * FROM ".$tbl."lure WHERE id = ".$lureChoice."");    
$stmt->execute();

while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    echo "Lure Choice: ".$row['type']. "<br />Color: " .$row['color']. "<br /><br />";
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Accessing variables outside a function in Python

From Dev

Accessing Variables outside of a function in swift

From Dev

Accessing outside variables inside the 'then' function

From Dev

angular $interval - accessing variables outside of the function

From Dev

Accessing struct variables outside of function in C

From Dev

Accessing php variables outside of a method in a function

From Dev

angular $interval - accessing variables outside of the function

From Dev

PHP assigning date function and accessing class property outside the class problems

From Dev

Accessing session variables outside servlet

From Dev

Accessing outside variables from timeit

From Dev

Accessing outside variables from timeit

From Dev

Accessing class data outside of scope

From Dev

Accessing Class Variables by enum

From Dev

Accessing a variable that is outside an anonymous function

From Dev

Accessing a local variable outside a function

From Dev

Accessing a variable outside a given function

From Dev

Accessing a variable from outside the function?

From Dev

Accessing a static function outside the file

From Dev

Scala accessing variable outside function

From Dev

accessing variable value outside of function

From Dev

Variables declared outside the class

From Dev

Accessing variables that are inside a function

From Dev

Using variables outside a function

From Dev

Global variables in class and outside class

From Dev

Accessing instance variables value outside the method in iOS

From Dev

Accessing a function define within a function from outside

From Dev

Accessing python objects created in function, outside of function

From Dev

Function within Class accessing 'this'

From Dev

Accessing function from class

Related Related

  1. 1

    Accessing variables outside a function in Python

  2. 2

    Accessing Variables outside of a function in swift

  3. 3

    Accessing outside variables inside the 'then' function

  4. 4

    angular $interval - accessing variables outside of the function

  5. 5

    Accessing struct variables outside of function in C

  6. 6

    Accessing php variables outside of a method in a function

  7. 7

    angular $interval - accessing variables outside of the function

  8. 8

    PHP assigning date function and accessing class property outside the class problems

  9. 9

    Accessing session variables outside servlet

  10. 10

    Accessing outside variables from timeit

  11. 11

    Accessing outside variables from timeit

  12. 12

    Accessing class data outside of scope

  13. 13

    Accessing Class Variables by enum

  14. 14

    Accessing a variable that is outside an anonymous function

  15. 15

    Accessing a local variable outside a function

  16. 16

    Accessing a variable outside a given function

  17. 17

    Accessing a variable from outside the function?

  18. 18

    Accessing a static function outside the file

  19. 19

    Scala accessing variable outside function

  20. 20

    accessing variable value outside of function

  21. 21

    Variables declared outside the class

  22. 22

    Accessing variables that are inside a function

  23. 23

    Using variables outside a function

  24. 24

    Global variables in class and outside class

  25. 25

    Accessing instance variables value outside the method in iOS

  26. 26

    Accessing a function define within a function from outside

  27. 27

    Accessing python objects created in function, outside of function

  28. 28

    Function within Class accessing 'this'

  29. 29

    Accessing function from class

HotTag

Archive