Laravel 动态读取mysql表数据(包含列、值

来源:互联网 发布:114源码 编辑:程序博客网 时间:2024/06/04 01:37


Laravel 动态读取mysql表数据(包含列、值  。难点,出现对象数组且列不确定


1、字段列取出

2、数据取出

3、页面遍历列

4、对象->列  取出 表中值


Model层

<pre style="background-color:#272822;color:#f8f8f2;font-family:'Source Code Pro';font-size:13.5pt;">$results <span style="color:#f92672;">= </span>DB<span style="color:#f92672;">::</span><span style="color:#a6e22e;">select</span>(<span style="color:#e6db74;">"select COLUMN_NAME from information_schema.COLUMNS where table_name = '</span><span style="color:#fd971f;font-style:italic;">$tableName</span><span style="color:#e6db74;">' and table_schema='dmp_line2';"</span>);
$results = DB::select("select COLUMN_NAME from information_schema.COLUMNS where table_name = '$tableName' and table_schema='dmp_line2';");
public function getTableC() //得到表所有列名 { $results = DB::select("select COLUMN_NAME from information_schema.COLUMNS where table_name = 'db_20160420063047753'"); return $results; } public function getTableDate($recode) //得到表数据,含分页。(每页显示$recode条数据 { //使用查询构建器进行简单分页,每页显示3条记录 //$posts = DB::table('20160420063047753')->simplePaginate(3); $posts = DB::table('20160420063047753')->paginate($recode); return $posts; }改写为:<pre name="code" class="php"> public function getTableC($tableName) //根据表名得到表所有列名 { $tableName = "db_".$tableName; //$results = DB::select("select COLUMN_NAME from information_schema.COLUMNS where table_name = '$tableName'");
//加库名: table_schema  
$results = DB::select("select COLUMN_NAME from information_schema.COLUMNS where table_name = '$tableName' and table_schema='dmp_line2';");
return $results; } public function getTableDate($tableName,$recode) //得到表数据,含分页。(每页显示$recode条数据 { //使用查询构建器进行简单分页,每页显示3条记录 //$posts = DB::table('20160420063047753')->simplePaginate(3); $posts = DB::table("$tableName")->paginate($recode); return $posts; }




Control层

$users = new Users();$tableC = $users->getTableC();//var_dump(count($tableC));die();//var_dump($tableC);$recode = 3; //每页显示条数$tableDate = $users->getTableDate($recode); //得到表中数据,并分页,每页3条数据return view('index.index',compact('tableC','tableDate','recode'));

View层

<table id="tt" class="oBoder_lqq" width="100%" border="1" cellspacing="0" cellpadding="0">                                                <tr class="oTitle_lqq">                                                    @foreach ($tableC as $task)                                                            <th scope="col" class="oTD8">{{ $task->COLUMN_NAME }}</th>                                                    @endforeach                                                </tr>                                                <?php                                                    $currentPage = $tableDate->currentPage();                                                    $id = ($currentPage - 1)*$recode + 1;                                                ?>                                                <tr>                                                @foreach ($tableDate as $info)                                                        <td>{{ $id }}</td>                                                        @foreach ($tableC as $value)                                                            <?php $key = $value->COLUMN_NAME; ?>                                                        <?php if($key!='id'){ ?>                                                        <td><p class="DMP_P2">{{ $info->$key }}</p></td>                                                            <?php } ?>                                                        @endforeach                                                    </tr>                                                    <?php $id = $id + 1; ?>                                                @endforeach                                                                                            </table><pre style="background-color:#272822;color:#f8f8f2;font-family:'Source Code Pro';font-size:13.5pt;"><<span style="color:#e3e3ff;font-weight:bold;">div </span><span style="color:#a6e22e;">class=</span><span style="color:#e6db74;font-weight:bold;">"page_list"</span>>    {{$tableDate<span style="color:#f92672;">-></span><span style="color:#a6e22e;">links</span>()}}</<span style="color:#e3e3ff;font-weight:bold;">div</span>>  //分页

======================

难点

表数据,  存在对象->                         (那么解决的话,就可以单独把列也取出来,然后去拼   ->

----------------------------

注:如需分页显示, 跳转时,数据丢失,则需要重新调取单纯的分页显示 模块

在同一方法时需要

$view_page = false;if($input = Input::all()){if(isset($input['page'])){$view_page = true;}}

再次跳转进来时,判断page是否有值,有的话就是分页显示的


//第一次调用数据表中数据//调用表并进行分页,值加载到页面view中$users = new Users();$recode = 3; //每页显示条数$view_pages = $this->view_pages($tabl_name,$recode);$tableC = $view_pages[0];   //表中列$tableDate = $view_pages[1]; // 表中数据$recode = $view_pages[2];   //每页显示条数//将表名、每页显示条数存入session$_SESSION['tableName'] = $tabl_name;$_SESSION['recode'] = $recode;


第二次调用

elseif($view_page){//$view = $this->view_pages();//die();$tabl_name = $_SESSION['tableName'];$recode = $_SESSION['recode'];$view_pages = $this->view_pages($tabl_name,$recode);$tableC = $view_pages[0];   //表中列$tableDate = $view_pages[1]; // 表中数据$recode = $view_pages[2];   //每页显示条数return view('query.index',compact('tableC','tableDate','recode'));


下边是调用的模块

//根据表名、每页显示条数,对表数据进行分页处理public function view_pages($table,$recode) //$table,$recode{//echo "234234";$users = new Users();$tableC = $users->getTableC($table);//$recode = 3; //每页显示条数$tableDate = $users->getTableDate($table,$recode); //得到表中数据,并分页,每页3条数据$new_array = array($tableC,$tableDate,$recode);return $new_array;//return view('query.index',compact('tableC','tableDate','recode'));}


0 0
原创粉丝点击