【问题标题】:Having trouble with array conversion into list in HTML在 HTML 中将数组转换为列表时遇到问题
【发布时间】:2016-10-09 16:13:34
【问题描述】:

我一直在试图弄清楚为什么我收到一个错误提示

为 foreach() 提供的参数无效

在我的 HTML 文件的第 20 行,我无法弄清楚我已经独立于 HTML 文件运行了我的 PHP 代码,并且 PHP 代码按预期返回了我的对象。一旦我尝试在我的 HTML 文件中调用它来显示它就不起作用,这都是我的文件

PHP:

require 'practice.view.php';

 class Task {

  public $description;

  public $completed = false;

  public function __construct($description){

    $this->description = $description;
  }

  public function complete(){

    $this->completed = true;
  }

  public function iscomplete(){

    return $this->completed;
  }
}




$tasks = [
  new Task('Go to the market'),

  new Task('eat a marshmellow'),

  new Task('Join a soccer league')
];



die(var_dump($tasks));

HTML:

<!doctype html>

<html lang="en">

<head>

<!meta charset="utf-8"->

  <title>Practice</title>

</head>

<body>

  <h1>Tasks List </h1>
    <ul>
      <?php foreach ($tasks as $task) : ?>

          <li>
            <?= $task->description; ?>
          </li>

      <?php endforeach; ?>
   </ul>
</body>
</html>

提前感谢您的任何帮助

【问题讨论】:

  • 如果$tasks 不存在,它就不会工作。您需要在 html 部分中包含 php 文件,否则它根本不知道 $tasks 是什么。还有&lt;?= $task-&gt;description; ?&gt;??请改用&lt;?php echo $task-&gt;description; ?&gt;
  • @icecub 已将 html 文件调用到我的 php 文件中,其中“需要...”是所以我不明白为什么或如何将其称为相反的方式。

标签: php html arrays


【解决方案1】:

文件名.php,

<?php

  class Task {

  public $description;

  public $completed = false;

  public function __construct($description){

    $this->description = $description;
   }

  public function complete(){

    $this->completed = true;
  }

  public function iscomplete(){

    return $this->completed;
  }
}




$tasks = [
  new Task('Go to the market'),

  new Task('eat a marshmellow'),

  new Task('Join a soccer league')
];

?>

HTML,

<!doctype html>

<html lang="en">

<head>

<!meta charset="utf-8"->

  <title>Practice</title>

</head>

<body>

<?php require 'filename.php'; ?>

  <h1>Tasks List </h1>
    <ul>
      <?php foreach ($tasks as $task) : ?>

          <li>
            <?= $task->description; ?>
          </li>

      <?php endforeach; ?>
    </ul>
</body>
</html

您应该使用 require 'filename.php' 将您的类包含在您的 html 文件中;你正在做相反的事情,你将你的 html 包含在类文件中。

【讨论】:

  • 不要将答案用于应该是评论的内容。你没有解决问题。您正在就如何调试它提供建议。
  • 我正在分享我的回答经验。
  • 我已经确保该对象存在并且在 PHP 中正常运行问题在于 HTML 你的答案@ShoyebSheikh 只是重申了我已经解释过的内容
  • 你应该使用 require 'filename.php' 将你的类包含在你的 html 文件中;你正在做相反的事情,你将你的 html 包含在类文件中。
  • @icecub 我为误导而道歉。
【解决方案2】:

我的问题的答案只是将我的“require...”语句放置在我的代码顶部,在这种情况下,它应该位于我的 PHP 文件中代码的底部。感谢所有试图回答这个问题的人

 class Task {

  public $description;

  public $completed = false;

  public function __construct($description){

    $this->description = $description;
  }

  public function complete(){

    $this->completed = true;
  }

  public function iscomplete(){

    return $this->completed;
  }
}




$tasks = [
  new Task('Go to the market'),

  new Task('eat a marshmellow'),

  new Task('Join a soccer league')
];



die(var_dump($tasks));

require 'practice.view.php';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 2021-12-29
    • 2016-07-11
    相关资源
    最近更新 更多