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.
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.
Comments