关于枚举在视图中的使用

浏览:2447 发布日期:2016/05/21 分类:求助交流 关键字: 枚举 视图
初学TP,枚举在数据库中以tinyint(1)存储,在controller中进行转换。根据controller中的$fieldMap数组进行枚举的识别。现在遇到问题如下:
1.由于$fieldMap经常要使用,如果在每个controller中的方法都使用assign,进行传递就太繁琐了。能否让所有的视图都能读取到$fieldMap的值。
2.在视图中根据$fieldMap的KEY,动态的生成<select>的html标签,显示下拉菜单。
controller代码如下:<?php
namespace HOME\Controller;
use Think\Controller;

class testController extends controller{
    
    private $fieldMap=array(    
        'DD'=>array( '是'=>1,'否'=>0 ),
        'FF'=>array( 'A' =>1,'B'=>2,'C'=>3 ),
    );

    
    
    public function createInst(){
        
        $InstAccount=M( 'Test' );    

        $result=$InstAccount->select();
        
        enumConvToView($result,$this->fieldMap);        

        if( !$result ){
            $this->error( $InstAccount->getError() );
        }    
    var_dump($result);
    $this->assign('result',$result);
    $this->display();
   }
}
?>
view creatInst如下:<FORM method="post" action="__URL__/edit" name="instlist">
<table id="inst_list">
    <tr>
        <td><input type="checkbox" class="check" id="checkAll"></td>
        <th>ID</th>
        <th>AA</th>
        <th>BB</th>
        <th>CC</th>
        <th>DD</th>
        <th>EE</th>
        <th>FF</th>
        
    </tr>
    <volist name='result' id='vo'>
    <tr id="{$vo.mgmtID}">
        <td><input type="checkbox" /></td>
        <td><input type="text" name="mgmtID[{$vo.mgmtID}]" value="{$vo.mgmtID}" readonly="true" ></input></td>
        <td><input type="text" name="AA[{$vo.mgmtID}]" value="{$vo.AA}" ></input></td>
        <td><input type="text" name="BB[{$vo.mgmtID}]" value="{$vo.BB}" ></input></td>
        <td><input type="text" name="CC[{$vo.mgmtID}]" value="{$vo.CC}" ></input></td>
        <td><input type="text" name="DD[{$vo.mgmtID}]" value="{$vo.DD}" ></input></td>
        <td><input type="text" name="EE[{$vo.mgmtID}]" value="{$vo.EE}" ></input></td>
        <td><input type="text" name="FF[{$vo.mgmtID}]" value="{$vo.FF}" ></input></td>
        
    </tr>
    </volist>
</table>
</form>
enumConvToView函数如下://枚举转换到视图函数
function enumConvToView(&$data,$fieldMap){    
    $result=array();
    foreach ($fieldMap as $fk => $fv) {
        foreach ($data as &$rowVal) {    //取出数据库查询结果的行数据 
            if (array_key_exists($fk, $rowVal)) {    //判断数据行是否存在fieldMap中的KEY                
                foreach ($fv as $fmk => $fmv) {                    
                    if( intval($rowVal[$fk])===$fmv and strlen($rowVal[$fk])==1 ) //tinyint(1)的返回长度为1,修改后utf8的中文字符长度为3
                    {                                                    
                        $rowVal[$fk]=$fmk;                            
                    }
                    
                }
            }            
            
        }

    }
        
}
tp_test.sql:Target Server Version : 50506
File Encoding         : 65001

Date: 2016-05-21 09:10:54
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `tp_test`
-- ----------------------------
DROP TABLE IF EXISTS `tp_test`;
CREATE TABLE `tp_test` (
  `mgmtID` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `AA` char(16) NOT NULL DEFAULT '',
  `BB` char(16) NOT NULL DEFAULT '',
  `CC` char(16) NOT NULL DEFAULT '',
  `DD` tinyint(1) NOT NULL DEFAULT '0',
  `EE` char(16) NOT NULL DEFAULT '',
  `FF` tinyint(1) NOT NULL DEFAULT '3',
  PRIMARY KEY (`mgmtID`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of tp_test
-- ----------------------------
INSERT INTO `tp_test` VALUES ('2', '222', '3231', '3', '1', '0', '3');
INSERT INTO `tp_test` VALUES ('1', '11', '1213', '3', '1', '0', '3');
最佳答案
评论( 相关
后面还有条评论,点击查看>>