PHP - 避免重复查看数据库数据

时间:2015-06-23 19:42:02

标签: php oop

一个非常理论化的问题 - 如果您的查询几乎没有从同一个表中返回略有不同的数据集,那么人们如何在显示数据库数据时避免代码重复?

我正在绞尽脑汁如何最大限度地减少显示这些记录的工作量 - 目前我有一个基于查询类型的switch构造构建表格。在我看来,这是非常无效的 - 你有什么其他方式可以想到吗?

使用fetchAll(PDO :: FETCH_CLASS)通过PDO获取结果 - 结果被提取到对象中。此对象具有与查询返回的列匹配的私有属性以及返回这些私有属性的方法。

switch($result)
case "a":
 foreach($result as $row)
 {
 echo "<tr><td>".$row->getId()."</td></tr>";
 }
case "b":
 foreach($result as $row)
 {
 echo "<tr><td>".$row->getId()."</td></tr>";
 }
 ...

编辑: 我意识到这里的信息有点模糊和一般,所以我试着稍微扩展它。 场景A:&#39;返回所有记录&#39; 场景B:&#39;返回符合条件的记录,不包括列x,y,z&#39;

两种方案都返回非常相似的数据集,但在HTML表格中显示它们需要大量的代码重复(参见上面的代码示例)。

3 个答案:

答案 0 :(得分:1)

这对你来说可能是一个很好的起点 因为你声明使用PDO::FETCH_CLASS和方法来访问私有属性,所以我没有打算建立MySQL连接,我只有两个对象来测试它。

GenericObject.php - 一个抽象类,允许我获取列可读标题,获取对象属性列表并获取这些属性的关联值。

<?php

abstract class GenericObject extends stdClass{

    public function translateColumn($column){
        return isset($this->_columnsNames[$column]) ?
            $this->_columnsNames[$column] :
            $column;
    }

    public function __get($name){
        /**
         * I used a magic getter for ease of use, but if you have getters
         * like getSomething, that could also work, see note on getColumns()
         */
        return $this->{$name};
    }

    public function getColumns(){
        /**
         * This is only for ease of use, since I only write a magic getter.
         * Alternatively you can use get_class_methods($this) and
         * filter only on methods beginning with get like getSomething,
         * this way you can list all your getters and all your properties
         */
        return array_keys($this->_columnsNames);
    }
}

A.php 继承GenericObject的虚拟示例类

<?php

class A extends GenericObject{
    protected $a = "a";
    protected $b = "b";
    protected $c = "c";

    protected $_columnsNames = array(
        "a" => "Column A",
        "b" => "Column B",
        "c" => "Column C",
    );
}

B.php 继承GenericObject的另一个虚拟示例类,具有与A类不同的属性

<?php

class B extends GenericObject{
    protected $a = "a";
    protected $c = "c";
    protected $d = "d";
    protected $e = "e";
    protected $f = "f";

    protected $_columnsNames = array(
        "a" => "Column A",
        "c" => "Column C",
        "d" => "Column D",
        "e" => "Column E",
        "f" => "Column F",
    );
}

TableHelper.php 我们的表助手,根据对象属性绘制表及其标题

<?php

class TableHelper{
    public function drawTable(array $ar){
        $html = '';
        if(count($ar) > 0){
            foreach($ar as $elem){
                if($html == ''){
                    $tHead = $this->drawHeader($elem);
                }
                $html .= $this->drawLine($elem);
            }
            $html = <<<HTML
                <table>
                    <thead>
                        {$tHead}
                    </thead>
                    <tbody>
                        {$html}
                    </tbody>
                </table>
HTML;
        }
        return $html;
    }

    protected function drawLine(stdClass $obj){
        $row = '';
        $vars = $obj->getColumns();
        foreach($vars as $column){
            $value = $obj->$column;
            $row .= <<<HTML
                <td>{$value}</td>
HTML;
        }
        return <<<HTML
                <tr>{$row}</tr>
HTML;
    }

    protected function drawHeader(stdClass $obj){
        $tHead = '';
        $vars = $obj->getColumns();
        foreach($vars as $column){
            /**
             * This is only a suggestion of a way to translate columns names
             */
            $column = $obj->translateColumn($column);
            $tHead .= <<<HTML
                <th>{$column}</th>
HTML;
        }
        return <<<HTML
                <tr>{$tHead}</tr>
HTML;
    }
}

所有这些的基本用法:

<?php
    $table = new TableHelper();
    echo $table->drawTable(
        array(
            new A(),
            new A(),
            new A(),
        )
    );
?>
<br/><br/>
<?php
    echo $table->drawTable(
        array(
            new B(),
            new B(),
            new B(),
            new B(),
        )
    );
?>

最后并非最不重要的是,它的输出是什么(实际上你可以看到我更像是一个开发人员,而不是我的设计师,显然)。

&#13;
&#13;
table, td, th {
  border: 1px solid;
}
&#13;
<table>
  <thead>
    <tr>
      <th>Column A</th>
      <th>Column B</th>
      <th>Column C</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>a</td>
      <td>b</td>
      <td>c</td>
    </tr>
    <tr>
      <td>a</td>
      <td>b</td>
      <td>c</td>
    </tr>
    <tr>
      <td>a</td>
      <td>b</td>
      <td>c</td>
    </tr>
  </tbody>
</table>
<br/><br/>
<table>
  <thead>
    <tr>
      <th>Column A</th>
      <th>Column C</th>
      <th>Column D</th>
      <th>Column E</th>
      <th>Column F</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>a</td>
      <td>c</td>
      <td>d</td>
      <td>e</td>
      <td>f</td>
    </tr>
    <tr>
      <td>a</td>
      <td>c</td>
      <td>d</td>
      <td>e</td>
      <td>f</td>
    </tr>
    <tr>
      <td>a</td>
      <td>c</td>
      <td>d</td>
      <td>e</td>
      <td>f</td>
    </tr>
    <tr>
      <td>a</td>
      <td>c</td>
      <td>d</td>
      <td>e</td>
      <td>f</td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以在查询中返回相同的数据集,例如:

wrong number of arguments (0 for 1)

Parameters:

{"utf8"=>"✓",
  "authenticity_token"=>"HjiANQUqTQVEqy0yzfLFMlnC8RsTiY5kVlvIUnD5OSIaSYSi4ELSuC95vRMIBA/6W+KvzCWMMXQ==",
"post_id"=>"7",

甚至可以使用UNION将查询结果合并到一个查询中。

SELECT price as value_to_display FROM prices;
SELECT name as value_to_display FROM persons;

答案 2 :(得分:0)

取得结果是有效的,并且在获得结果时基本上使用mysqli:

    while ($data = $results->fetch_assoc()) {
        //Now every record you have is now an array. Do with what you want. 
        echo $data['username']; // This will output the column 'username' from your record.
    }

在我看来,使用foreach有点乱,除非你打算一次显示所有内容而不处理记录中的任何信息。

相关问题