douke6424 2013-10-30 01:45
浏览 25
已采纳

Laravel 4:像CodeIgniter一样生成HTML表

So in CodeIgniter, there was a cool feature of creating an HTML table just by passing it an array and it handles the header, etc. Is there ANYTHING out there for Laravel, has anybody been able to use the CI version in Laravel? just asking around

  • 写回答

3条回答 默认 最新

  • dscs63759 2013-10-30 06:44
    关注

    There is nothing like this in Laravel AFAIK but you may create your own, something like (an idea only) this (a php class, not only for Laravel)

    class Table {
    
        protected $table = null;
        protected $header = null;
        protected $attr = null;
        protected $data = null;
    
        public function __construct($data = null, $attr = null, $header = null)
        {
            if(is_null($data)) return;
            $this->data = $data;
            $this->attr = $attr;
            if(is_array($header)) {
                $this->header = $header;
            }
            else {
                if(count($this->data) && $this->is_assoc($this->data[0]) || is_object($this->data[0])) {
                    $headerKeys = is_object($this->data[0]) ? array_keys((array)$this->data[0]) : array_keys($this->data[0]);
                    $this->header = array();
                    foreach ($headerKeys as $value) {
                        $this->header[] = $value;
                    }
                }
            }
            return $this;
        }
    
        public function build()
        {
            $atts = '';
            if(!is_null($this->attr)) {
                foreach ($this->attr as $key => $value) {
                    $atts .= $key . ' = "' . $value . '" ';
                }
            }
            $table = '<table ' . $atts . ' >';
    
            if(!is_null($this->header)) {
                $table .= '<thead><tr>';
                foreach ($this->header as $value) {
                    $table .= '<th>' . ucfirst($value) . '</th>';
                }
                $table .= '</thead></tr>';
            }
    
            $table .= '<tbody>';
            foreach ($this->data as $value) {
                $table .= $this->createRow($value);
            }
            $table .= '</tbody>';
            $table .= '</table>';
            return $this->table = $table;
        }
    
        protected function createRow($array = null)
        {
            if(is_null($array)) return false;
                $row = '<tr>';
                foreach ($array as $value) {
                    $row .= '<td>' . $value . '</td>';
                }
                $row .= '</tr>';
                return $row;
        }
    
        protected function is_assoc($array){
            return is_array($array) && array_diff_key($array, array_keys(array_keys($array)));
        }
    }
    

    Now, you can use it as given below (An Example Here.)

    $data = array(
        array('name' => 'Heera', 'age'=>'35', 'address' =>'Masimpur', 'phone'=>'123456'),
        array('name' => 'Usman', 'age'=>'28', 'address' =>'Kamal Gor', 'phone'=>'654321')
    );
    $attr = array('class'=>'tbl someClass', 'id'=>'myTbl', 'style'=>'width:400px;color:red', 'border'=>'1');
    $t = new Table($data, $attr);
    echo $t->build();
    

    Or, set an header using third argument, like

    $t = new Table($data, $attr, array('Known As', 'Years', 'Location', 'Contact'));
    

    This is just an idea and could be better. Now just integrate this class with Laravel using Laravel's rule. You may extend Html class or use as an individual class by registering it as a service. Take a look at this answer for extending a class in laravel.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?