-3

我正在尝试创建一个 php 脚本,该脚本从表单中获取输入值,然后在提交表单时输出推荐的旅行路线。我将表单方法设置为 POST。问题是我得到一个空白输出。

我不确定问题是我的代码还是表单 POST 方法。


class Truck {
    // constructor
    public function __construct($truck_name, $max_weight) {
        $this->truck_name = $truck_name;
        $this->max_weight = $max_weight;
    }   
    public function print_truck() {
        if (isset($_POST['submit'])) 
            {
               $truck_1 = new Truck($truck_name, $max_weight);
                  $this->truck_name = $_POST['truck_name'];
                  $this->max_weight = $_POST['max_weight'];
            }       
        echo $this->truck_name;
    }
} 

class Location {
    // constructor
    public function __construct($location_name, $location_weight) {
        $this->location_name = $location_name;
        $this->location_weight = $location_weight;
    }

    public function trip_1() {
        if (isset($_POST['submit'])) {
    $location_2 = new Location($location_name, $location_weight);
        $this->location_name = $_POST['location_2'];
        $this->location_weight = $_POST['package_2'];

    $location_3 = new Location($location_name, $location_weight);
        $this->location_name = $_POST['location_3'];
        $this->location_weight = $_POST['package_3'];
    }

    echo "Trip #1 \n" . $this->location_2 . ", " . $this->location_3 . "\n";
    }

    public function trip_2() {
    if (isset($_POST['submit'])) {
    $location_1 = new Location($location_name, $location_weight);
        $this->location_name = $_POST['location_1'];
        $this->location_weight = $_POST['package_1'];
    }
    echo "Trip #2 \n" . $this->location_1;
    }
}

# Here's the output HTML:
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
  <title>Deliveries</title>
</head>

<body class="container">
  <form method="post" action="">
    <input type="text" name="truck_name" Placeholder="Truck Name" value="" required>
  <input type="number" name="max_weight" Placeholder="Max weight in lbs" value="" required>
  <h4>Delivery Locations</h4>
  <input type="text" name="location_1" Placeholder="Location 1" value="" required>
  <input type="number" name="package_1" Placeholder="Package weight in lbs" value="" required>  
  <input type="text" name="location_1" Placeholder="Location 2" value="">
  <input type="number" name="package_2" Placeholder="Package weight in lbs" value="" required>
  <input type="text" name="location_1" Placeholder="Location 3" value="" required>
  <input type="number" name="package_3" Placeholder="Package weight in lbs" value="" required>
    <input type="submit" name="submit" value="Get Trips">
    <input type="reset" name="reset" value="Reset">
  </form>

<?php
if (isset($_POST['submit'])) 
  { ?>
    <h2><?php echo "Trip Routes"; ?></h2>
<?php
   $truck_1->print_truck();   
   trip_1();
   trip_2();
  }
?>
</body>
</html>

这是预期的结果:

行程 1:地点 2、地点 3

行程 2:位置 1

4

2 回答 2

0

正如我在评论中提到的(然后被删除)并被指出 - 上面的代码并没有多大意义。我很欣赏您正在学习 PHP,但类对象实例化和赋值的方式不正确。在class constructor课堂上被调用method是背对背的。应该使用所需的class任何参数进行实例化,然后methods调用它。一个类可以作为另一个类的参数给出——这就像我在下面所做的那样,我创建了一个名为的新类Trip并分配了一个 and 的实例。trucklocation可能错过了这一点,但希望它有所帮助.

<?php

    class Truck {
        private $truck_name;
        private $truck_weight;

        public function __construct($name=false, $weight=false) {
            $this->truck_name = $name;
            $this->max_weight = $weight;
        }
        public function get_name(){
            return $this->truck_name;
        }
        public function get_max_weight(){
            return $this->max_weight;
        }
    }


    class Location {
        private $location_name;
        private $location_weight;

        public function __construct($name=false, $weight=false) {
            $this->location_name = $name;
            $this->location_weight = $weight;
        }
        public function get_location(){
            return $this->location_name;
        }
        public function get_location_weight(){
            return $this->location_weight;
        }
    }

    class Trip{
        private $truck;
        private $locations;

        public function __construct( Truck $truck, $locations=array() ){
            $this->truck=$truck;
            $this->locations=$locations;
        }
        public function display(){
            if( !empty( $this->locations ) ){

                $dots=str_repeat('-',25);

                printf(
                    "<h2>Trip Routes</h2>
                    <pre>%s\nTruck: %s\nMax-Weight: %s\n%s\n",
                    $dots,
                    $this->truck->get_name(),
                    $this->truck->get_max_weight(),
                    $dots
                );

                $total_locations_weight = 0;

                foreach( $this->locations as $index => $location ){
                    $total_locations_weight += floatval( $location->get_location_weight() );
                    printf( 
                        "[Drop: %d]\nLocation: %s\nLocation-Weight: %s\n\n",
                        $index+1,
                        $location->get_location(),
                        $location->get_location_weight()
                    );
                }
                printf("%s</pre>",$dots);

                /* Is there too much weight for the truck? */
                if( $total_locations_weight > $this->truck->get_max_weight() ){
                    printf(
                        "<pre><h1 style='color:red;margin:0;'>Maximum Weight Exceded</h1><div>Total weight is: %s</div><div>Limit exceded by: %s:</div>\n%s</pre>",
                        $total_locations_weight,
                        $total_locations_weight - $this->truck->get_max_weight(),
                        $dots
                    );
                }
            }
        }
    }

?>

<!DOCTYPE HTML>
<html lang="en">
    <head>
      <title>Deliveries</title>
    </head>
    <body class="container">
      <form method="post" action="">

        <input type="text" name="truck_name" placeholder="Truck Name" value="" required>
        <input type="number" name="max_weight" placeholder="Max weight in lbs" value="" required>

        <h4>Delivery Locations</h4>

        <input type="text" name="location_1" placeholder="Location 1" value="" required>
        <input type="number" name="package_1" placeholder="Package weight in lbs" value="" required>  

        <input type="text" name="location_2" placeholder="Location 2" value="">
        <input type="number" name="package_2" placeholder="Package weight in lbs" value="" required>

        <input type="text" name="location_3" placeholder="Location 3" value="" required>
        <input type="number" name="package_3" placeholder="Package weight in lbs" value="" required>


        <input type="submit" name="submit" value="Get Trips">
        <input type="reset" name="reset" value="Reset">
      </form>

    <?php


        if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['submit'] ) ){

            $truck_name = filter_input( INPUT_POST, 'truck_name', FILTER_SANITIZE_STRING );
            $max_weight = filter_input( INPUT_POST, 'max_weight', FILTER_SANITIZE_STRING );

            $location_1 = filter_input( INPUT_POST, 'location_1', FILTER_SANITIZE_STRING );
            $location_2 = filter_input( INPUT_POST, 'location_2', FILTER_SANITIZE_STRING );
            $location_3 = filter_input( INPUT_POST, 'location_3', FILTER_SANITIZE_STRING );

            $package_1 = filter_input( INPUT_POST, 'package_1', FILTER_SANITIZE_STRING );
            $package_2 = filter_input( INPUT_POST, 'package_2', FILTER_SANITIZE_STRING );
            $package_3 = filter_input( INPUT_POST, 'package_3', FILTER_SANITIZE_STRING );







            $truck=new Truck( $truck_name, $max_weight );

            $loc_1=new Location( $location_1, $package_1 );
            $loc_2=new Location( $location_2, $package_2 );
            $loc_3=new Location( $location_3, $package_3 );

            $trip=new Trip( $truck, array( $loc_1, $loc_2, $loc_3 ) );
            $trip->display();
        }
    ?>
    </body>
</html>

典型的输出可能是:

Trip Routes
-------------------------
Truck: The Crushinator
Max-Weight: 2500
-------------------------
[Drop: 1]
Location: London
Location-Weight: 1500

[Drop: 2]
Location: Glasgow
Location-Weight: 1500

[Drop: 3]
Location: Edinburgh
Location-Weight: 550

-------------------------
Maximum Weight Exceded
Total weight is: 3550
Limit exceded by: 1050:
-------------------------
于 2019-04-14T08:17:40.727 回答
0

在尝试调用方法之前,您不会实例化任何类。您需要实例化该类,例如 $truck = new truck(); 然后从那里调用你的方法。

于 2019-04-14T07:58:40.687 回答