如何正确地将 php 变量插入表格单元格


How to properly insert php variables into table cells?

我知道这听起来有点令人困惑,但请允许我进一步解释。

我有一个php函数,可以打印表格的顶部,包括html标签,如下所示:

function print_table_head (){
    echo "<table>
            <thead>
              <tr>
                <th>URL Link</th>
                <th>Date Start</th>
                <th>Date End</th>
                <th>Total Budget</th>
                <th>Daily Budget</th>
                <th>Model Price</th>
                <th>Unit Rate</th>
                <th>Target Info</th>
                <th>Product</th>
                <th>Status</th>
                <th>Total Units</th>
              </tr>
            </thead>
            <tbody>";
  }

我还有一个函数,可以使用 html 表结束标签关闭表,如下所示:

function print_table_bottom(){
    echo "</tbody>
          </table>";
  }

现在,桌子打印得很好,直到这里。我得到了正确的表格标题和每件事。但是我无法打印表体和内容。以下是我打印整个表格(包括其正文和内容)的主要函数:

function print_table ( $status, $model, $bid, $url, $target, $tBudget,$dBudget, $tUnits, $sMonth, $sDay, $sYear, $eMonth, $eDay, $eYear ){
    print_table_head();
    echo "<tr>
            <td><?php $url[0]; ?></td>
            <td><?php $sMonth[0] / $sDay[0] / $sYear[0];  ?></td>
            <td><?php $eMonth[0]; / $eDay[0]; / $eYear[0];  ?></td>
            <td><?php $tBudget[0]; ?></td>
            <td><?php $dBudget[0]; ?></td>
            <td><?php $model[0]; ?></td>
            <td><?php $targeting[0]; ?></td>
            <td><?php $product[0]; ?></td>
            <td><?php $status[0]; ?></td>
            <td><?php $tUnits[0]; ?></td>
          </tr>";
    print_table_bottom();
  }

每次我调用"print_table"函数时,它都会打印带有标题的表格顶部。但它不打印表格行/单元格。我不确定我做错了什么。有人可以为我澄清这一点并告诉我我做错了什么吗?

注意=我正在使用基础-5。不确定这是否重要。

此外,使用的每个变量都是一个数组(它们都有 1 个元素......是的,我知道,我本可以不为每个变量使用数组,但我想这样做。

编辑:

根据评论者的要求,我将提供调用print_table的代码:

<?php
    //if the length of the array "status" is 1.
    if (count($status)==1){
      print_table ( $status, $model, $bid, $url, $target, $tBudget,$dBudget, $tUnits,$sMonth,$sDay,$sYear,$eMonth, $eDay, $eYear );
    }
    // please ignore this function. 
    else
      print_table_with_multiple_rows ( $status, $model, $bid, $url, $target, $tBudget,$dBudget, $tUnits,$sMonth,$sDay,$sYear,$eMonth, $eDay, $eYear );
  ?>

简单选项

最简单的选择是重新格式化数据结构并使用以下代码:

$data = // Structure: array([0] => array('url' => '', 'sMonth' => '', 'eMonth' => '', ...), [1] => array(...));
include 'templates/table.php';

模板/表格.php

<table>
    <thead>
        <tr>
            <th>URL Link</th>
            <th>Date Start</th>
            <th>Date End</th>
            <th>Total Budget</th>
            <th>Daily Budget</th>
            <th>Model Price</th>
            <th>Unit Rate</th>
            <th>Target Info</th>
            <th>Product</th>
            <th>Status</th>
            <th>Total Units</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($data as $row) : ?>
        <td><?php echo $row->url ?></td>
        <td><?php echo $row->sMonth . "/" . $row->sDay . "/" . $row->sYear; ?></td>
        <td><?php echo $row->eMonth . "/" . $eDay . "/" . $eYear;  ?></td>
        <td><?php echo $row->tBudget; ?></td>
        <td><?php echo $row->dBudget; ?></td>
        <td><?php echo $row->model; ?></td>
        <td><?php echo $row->targeting; ?></td>
        <td><?php echo $row->product; ?></td>
        <td><?php echo $row->status; ?></td>
        <td><?php echo $row->tUnits; ?></td>
        <?php endforeach; ?>
    </tbody>
</table>

高级选项(更好)

这可能比其余代码更高级一些,但一般来说,这是一种更合适的编写 (OO)PHP 的方法。但是,它可能需要重组所有代码。

目录结构:

--models
    --informationmodel.php
--controllers
    --abstractcontroller.php
    --informationcontroller.php
--templates
    --information
        --table.php
模型

/信息模型.php

首先,模型类。此模型反映特定类型的数据 - 在本例中,指表中显示的任何类型的信息。此类不应操作数据 - 它只是一个存储所需数据的类。

class InformationModel { //Where "Information" is changed to describe this data type
    private $data;
    public function __construct($data = array()) { //pass the things as an associative array
        $this->data = $data;
    }
    public function __set($key, $value) {
        $this->data[$key] = $value;
    }
    public function setData($data) {
        $this->data = $data;
    }
}

控制器/抽象控制器.php

这是一个抽象的控制器。控制器是存储应用程序逻辑的位置。这应该包括数据操作、模型检索等。每个控制器都使用抽象类来使其访问所有控制器通用的特定函数。

abstract class AbstractController {
    // add more functions for all controllers here
    public function renderTemplate($template, $data) {
        include $template;
    }
}
控制者

/信息控制者.php

这是实际的控制器。在这个控制器中,我们可以定义操作 - 在本例中,我们将定义一个表操作。这意味着我们可以调用此操作来呈现包含我们提供给它的数据的表。这很有用,因为如果我们想改变数据的呈现方式,很容易做到。我们甚至可以使数据以与 HTML 不同的格式呈现,如果我们将其设置为允许您指定它。现在,我们只有一个简单的操作:加载数据,并将其提供给模板。

require_once 'models/informationmodel.php'; // note: autoloading would be preferable
class InformationController extends AbstractController {
    public function tableAction($rawData) {
        $data = new InformationModel($rawData);
        $this->renderTemplate('templates/information/table.php', $data);
    }
}

模板/信息/表格.php

这是一个模板。您可以在此处定义代码的显示方式。如果你想在这里走得更远,看看一个模板引擎,比如Twig(我个人的建议)。这纯粹是为了呈现数据。如您所见,我将模板放入一个文件夹中,用于显示特定类型的信息:数据类型"信息"的表(或您将其更改为的任何内容)。这有助于您保持模板井井有条。

<table>
    <thead>
        <tr>
            <th>URL Link</th>
            <th>Date Start</th>
            <th>Date End</th>
            <th>Total Budget</th>
            <th>Daily Budget</th>
            <th>Model Price</th>
            <th>Unit Rate</th>
            <th>Target Info</th>
            <th>Product</th>
            <th>Status</th>
            <th>Total Units</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($data as $row) : ?>
        <td><?php echo $row->url ?></td>
        <td><?php echo $row->sMonth . "/" . $row->sDay . "/" . $row->sYear; ?></td>
        <td><?php echo $row->eMonth . "/" . $eDay . "/" . $eYear;  ?></td>
        <td><?php echo $row->tBudget; ?></td>
        <td><?php echo $row->dBudget; ?></td>
        <td><?php echo $row->model; ?></td>
        <td><?php echo $row->targeting; ?></td>
        <td><?php echo $row->product; ?></td>
        <td><?php echo $row->status; ?></td>
        <td><?php echo $row->tUnits; ?></td>
        <?php endforeach; ?>
    </tbody>
</table>

如您所见,我更改了数据结构:数据在行中操作,这些行每个只包含每个元素中的一个。使用模板引擎将减少<?php echo $row->status ?>结构的丑陋 - 在 Twig 中,它看起来像:{{ row.status }} .

然后,要实际使用此代码:

无论您使用什么页面

require_once "controllers/informationcontroller.php"; //even better: use autoloading
// $data =  get your data in here somewhere.
$info = new InformationController();
$info->tableAction($data); // prints out a table;

显然,这是很多新信息,但它很有用。如果你花时间学习面向对象的PHP(OOPHP),MVC,模板等等,你将成为一个更好的程序员,你的代码将更容易编写和维护。更多主题: Twig, 自动加载, 依赖注入/控制反转, 命名空间, 路由

现在,模型类

实际上并没有做任何事情,我也没有使用依赖注入(控制器中对模型类有直接依赖关系,这是不受欢迎的),但基本概念在这里。现在,您可以将数据直接传递给控制器,甚至直接传递到模板。

免责声明:我还没有测试过这段代码。但是,无论如何都需要进行一些修改。如果你真的想用PHP编写一个网站,你应该考虑以这种方式构建它。

,你把PHP和html搞砸了。

function print_table ( $status, $model, $bid, $url, $target, $tBudget,$dBudget, $tUnits, $sMonth, $sDay, $sYear, $eMonth, $eDay, $eYear ){
    print_table_head();
    echo "<tr>
            <td>{$url[0]}</td>
            <td>{$sMonth[0]} / {$sDay[0]} / {$sYear[0]}</td>
            <td>{$eMonth[0]} / {$eDay[0]} / {$eYear[0]}</td>
            <td>{$tBudget[0]}</td>
            <td>{$dBudget[0]}</td>
            <td>{$model[0]}</td>
            <td>{$targeting[0]}</td>
            <td>{$product[0]}</td>
            <td>{$status[0]}</td>
            <td>{$tUnits[0]}</td>
          </tr>";
    print_table_bottom();
  }

我个人觉得这更具可读性:

function print_table ( $status, $model, $bid, $url, $target, $tBudget,$dBudget, $tUnits, $sMonth, $sDay, $sYear, $eMonth, $eDay, $eYear ){
    print_table_head();
    ?>
    <tr>
        <td><?= array_shift($url); ?></td>
        <td><?= array_shift($sMonth), "/", array_shift($sDay), "/", array_shift($sYear); ?></td>
        <td><?= array_shift($eMonth), "/", array_shift($eDay), "/", array_shift($eYear); ?></td>
        <td><?= array_shift($tBudget); ?></td>
        <td><?= array_shift($dBudget); ?></td>
        <td><?= array_shift($model); ?></td>
        <td><?= array_shift($targeting); ?></td>
        <td><?= array_shift($product); ?></td>
        <td><?= array_shift($status); ?></td>
        <td><?= array_shift($tUnits); ?></td>
    </tr>
    <?php
    print_table_bottom();
  }

注意:如果它不起作用,可能是因为您的 PHP 不接受<?=。在这种情况下,将所有<?=替换为<?php echo