Reputation: 2466
I'm facing difficulty in passing data within foreach loop into oop function. The data are passed via Ajax to a php page. WHere the data is expected to be passed to a function in a class from the foreach loop.
For data without loop this is how I pass to OOP function and then get them inserted into database.
echo $car_name=$data["car_name"];
echo $car_maker=$data["car_maker"];
echo $car_type=$data["car_type"];
echo $passanger=$data["passanger"];
$car=new car($car_name,$car_maker,$car_type,"Red",$passanger);
$car->addCar();
public function __construct($param1,$param2,$param3,$param4,$param5,$param6)
{
$this->name=$param1;
$this->maker=$param2;
$this->type=$param3;
$this->colour=$param4;
$this->passanger=$param5;
$this->rate=$param6;
connection::addConnection();
}
public function addCar()
{
$sql="INSERT INTO car(car_name,car_maker,car_type,car_colour,num_passanger)VALUES('{$this->name}','{$this->maker}', '{$this->type}','{$this->colour}','{$this->passanger}')";
$stmt =connection::$pdo->prepare($sql);
$stmt->execute();
echo "Data inserted!";
//$this->rentalRate();
}
But for those in the loop.
$rate=$data["rate"];
foreach($rate as $key=>$value)
{
echo $key."->";
echo $value;
$car->rentalRate($value);// how do I pass this value into the function rentalRate()?
}
//how to insert the loop value in here?
public function rentalRate()
{
echo $this->rate;
$sql="INSERT INTO rental(day_1,day_3,day_7,day_15,day_30)VALUES('{$this->rate}','{$this->rate}', '{$this->rate}','{$this->rate}','{$this->rate}')";
$stmt =connection::$pdo->prepare($sql);
$stmt->execute();
}
My Full OOP function is as below:
<?php
interface db
{
public static function addConnection();
}
interface rental
{
public function addCar();
public function rentalRate();
}
abstract class report
{
public function generateReport()
{
echo "All done.Now generate report.";
}
}
class connection implements db
{
public static $servername = "localhost";
public static $username = "root";
public static $password = "";
public static $dbname = "carrental";
public static $port="3306";
public static $pdo;
public static function addConnection()
{
try
{
self::$pdo = new PDO("mysql:host=localhost;port=3306;dbname=carrental", self::$username, self::$password);
self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e)
{
echo 'ERROR: ' . $e->getMessage();
}
self::$pdo->query("use carrental");
}
}
class car extends report implements rental
{
public $name;
public $maker;
public $type;
public $colour;
public $passanger;
public $rate;
public function __construct($param1,$param2,$param3,$param4,$param5,$param6)
{
$this->name=$param1;
$this->maker=$param2;
$this->type=$param3;
$this->colour=$param4;
$this->passanger=$param5;
$this->rate=$param6;
connection::addConnection();
}
public function addCar()
{
$sql="INSERT INTO car(car_name,car_maker,car_type,car_colour,num_passanger)VALUES('{$this->name}','{$this->maker}', '{$this->type}','{$this->colour}','{$this->passanger}')";
$stmt =connection::$pdo->prepare($sql);
$stmt->execute();
echo "Data inserted!";
//$this->rentalRate();
}
public function rentalRate()
{
echo "Rate inserted!";
$this->generateReport();
echo $this->rate;
$sql="INSERT INTO rental(day_1,day_3,day_7,day_15,day_30)VALUES('{$this->rate}','{$this->rate}', '{$this->rate}','{$this->rate}','{$this->rate}')";
$stmt =connection::$pdo->prepare($sql);
$stmt->execute();
}
}
?>
Upvotes: 1
Views: 81
Reputation: 23840
Since car::$rate
is public, one way would be to just change its value:
$car->rate = $value;
$car->rentalRate();
However, I consider this a rather dirty implementation, and suggest actual argument passing instead.
I assume you've heard of default values for function arguments, such as
function whatever($myvar = 'default') {}
Now, sadly you can't use things like $this->rate
as default values (save for HHVM), but you can use a "special" value to indicate that no parameter has been passed, such as NULL
, for example:
public function rentalRate($rate = NULL)
{
$rate = $rate == NULL ? $this->rate : $rate;
echo "Rate inserted!";
$this->generateReport();
echo $rate;
$sql="INSERT INTO rental(day_1,day_3,day_7,day_15,day_30)VALUES('{$rate}','{$rate}', '{$rate}','{$rate}','{$rate}')";
$stmt =connection::$pdo->prepare($sql);
$stmt->execute();
}
If rentalRate
is now called with an argument, it will use that as rate, and if not, it will use $this->rate
instead.
Also, if you don't understand this line:
$rate = $rate == NULL ? $this->rate : $rate;
That's just lazy people's way of writing
if($rate == NULL)
{
$rate = $this->rate;
}
See: Ternary operator.
Upvotes: 1