How to pass values in loop to OOP function?

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();

    }
}

?>


Source: oop

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.