discuz中写一个表单,数据存入到数据库中,再从数据库读出来显示在列表中

要做到如下的一个效果:

152258_PRRF_2261111.png

创建的文件有:

./funds.php

./template/PHPChina/funds/funds_index.htm

./source/module/funds/funds_index.php

./source/class/table/table_app_funds.php

具体步骤如下:

1、在数据库中建一个表,用于存储表单中填入的数据。我这里建了一个pre_app_funds表。

152429_ElV0_2261111.png

2、写入口文件,在根目录下建一个funds.php的入口文件

153301_LYeh_2261111.png

入口文件主要是一些初始化、引入核心文件、路由定向的一些常规操作

3、写一个funds_index.htm文件,在template/PHPChina/funds/funds_index.htm。其中PHPChina是我用的模板,如果是用的默认的模板,写在default中即可。

在htm文件中记得引入<!--{template common/header}-->和<!--{template common/footer}-->

154057_J8zi_2261111.png

样式我就不截图了,效果就是第一个图的样子。再附上显示系统时间的代码:

154202_e7X1_2261111.png

4、写一个php文件,用于获取表单中写入的数据,并调用存入数据库的方法

<?php
if(!defined('IN_DISCUZ')){
    exit('Access Denied');
}
if(empty($_GET['mod'])){
    $_GET['mod'] = 'index';
}
if($_GET['action'] == 'index'){
    if($_G['uid'] == 0){
        showmessage('请先登录','member.php?mod=logging&action=login',array(),array('alert'=>'error','msgtype'=>2));
    }
    include template('funds/funds_index');
}elseif($_GET['action'] = 'save_funds'){      
        $add_funds = array();
        //$add_funds[app_id]为自增,不用写
        $add_funds[app_name] = $_POST['app_name'];
        $add_funds[app_date] = $_POST['app_date'];
        $add_funds[app_reason] = $_POST['app_reason'];
        $add_funds[app_money] = $_POST['app_money'];
        $result = C::t('app_funds')->add_funds($add_funds);
        if($result){
            showmessage('申请成功,请等待审核','funds.php?mod=index&action=index',array(),array('alert'=>'right','msgtype'=>2));
        }else{
            showmessage('申请失败','funds.php?mod=index&action=index',array(),array('alert'=>'error','msgtype'=>2));
        }
    }

其中,154812_UWtN_2261111.png这行代码的意思是

C::t('app_funds')是数据库中的pre_app_funds表调用后面的add_funds方法,参数为上面获取到的数组$add_funds,数组里是表单中写入的数据。

5、写表调用的方法add_funds。建文件table_app_funds.php。

<?php
if(!defined('IN_DISCUZ')){
    exit('Access Denied');
}

class table_app_funds extends discuz_table
{
    public function __construct(){
        $this->_table = 'app_funds';
        $this->_pk = 'app_id';
        parent::__construct();
    }

    public function add_funds($add_funds){       
        $result = DB::insert($this->_table,$add_funds);
        return $result;
    }
}

到此步骤已经写完,可以看到数据库中已经有这些数据了,但是表单中输入中文,在表里是乱码,这个问题我还没找到解决办法。

155300_mavH_2261111.png

 

补充:终于解决了中文乱码问题!!之前网上搜了很多方法都不适合,我也很奇怪,在我的数据库里,有的表中文乱码,而有的却可以。就在刚刚我新建了一个其他的表,终于找到方法了,原来如此简单

打开数据库,对着表右键-设计表

141253_hBhv_2261111.png

选中有中文的那一列,下面的字符集和排序规则填成图中的选项即可,两个都要填才生效。

以上是数据在数据库的存储,以下是读取数据:

需要添加或修改的文件有:

./source/module/funds/funds_index.php

./source/class/table/table_app_funds.php

./template/PHPChina/funds.funds_list.htm

1、在funds_index.php增加一个action==funds_list的判断段落

<?php
if(!defined('IN_DISCUZ')){
    exit('Access Denied');
}
if(empty($_GET['mod'])){
    $_GET['mod'] = 'index';
}
if($_GET['action'] == 'index'){
    if($_G['uid'] == 0){
        showmessage('请先登录','member.php?mod=logging&action=login',array(),array('alert'=>'error','msgtype'=>2));
    }
    include template('funds/funds_index');
}elseif($_GET['action'] == 'save_funds'){    
        $add_funds = array();
        //$add_funds[app_id]为自增,不用写
        $add_funds[app_name] = $_POST['app_name'];
        $add_funds[app_date] = $_POST['app_date'];
        $add_funds[app_reason] = $_POST['app_reason'];
        $add_funds[app_money] = $_POST['app_money'];
        $result = C::t('app_funds')->add_funds($add_funds);
        if($result){
            showmessage('申请成功,请等待审核','funds.php?mod=index&action=index',array(),array('alert'=>'right','msgtype'=>2));
        }else{
            showmessage('申请失败','funds.php?mod=index&action=index',array(),array('alert'=>'error','msgtype'=>2));
        }
}elseif($_GET['action'] == 'funds_list'){
    $list = array();
    $list = C::t('app_funds')->funds_list();

    $page = empty($_GET['page'])?1:intval($_GET['page']);
    if($page<1) $page=1;

    //分页
    $perpage = 5;
    $start = ($page-1)*$perpage;
    //获得一个简单的分页,只有上一页和下一页,这个不需要count()数据表中的所有记录
    $multi = simplepage(count($list), $perpage, $page, 'funds.php?mod=index');
    //数据准备完毕,引入相应的模板,准备输出
    include_once template("funds/funds_list");
}

(困惑点一)分页那里没有成功,不知道哪里出了错,还望大神们指导!

$list是个数组,$list = C::t('app_funds')->funds_list();这句是C::t(数据表)调用后面funds_list函数

2、接下来写这个函数,在table_app_funds.php中

<?php
if(!defined('IN_DISCUZ')){
    exit('Access Denied');
}

class table_app_funds extends discuz_table
{
    public function __construct(){
        $this->_table = 'app_funds';
        $this->_pk = 'app_id';
        parent::__construct();
    }

    public function add_funds($add_funds){       
        $result = DB::insert($this->_table,$add_funds);
        return $result;
    }

    public function funds_list(){
        $list = DB::fetch_all("SELECT * FROM %t ORDER BY %s DESC",array($this->_table,$app_date));
        return $list;
    }

}

前面重复的我也写上了,这里只需要看最后一个函数funds_list。fetch_all是取所有的数据,%t,%s是后面那个参数的占位符,(困惑点二)这里按照时间降序排列,但是没生效,效果和下面是一样的,不知道为什么

092512_j7CY_2261111.png

取出的数据存储到list中,并返回到funds_index.php,数据取出来了要显示在htm中,所有下一步是写htm

3、增加funds_list.htm

<!--{template common/header}-->
<div id="ct" class="wp cl">
        <div class="bm">
        <!--{if $list}--> 
            <!--{loop $list $funds}-->
               <a><!--{$funds[app_name]}--></a>于<!--{$funds[app_date]}-->申请<!--{$funds[app_money]}-->元资金,理由是<!--{$funds[app_reason]}--><hr>
        
            <!--{/loop}-->
        $multi
        <!--{else}-->
            <p class="emp">暂时没有记录...</p>
        <!--{/if}-->
        </div>      
</div>
<!--{template common/footer}-->

引入头部脚部就不用说了。如果$list中存储了数据,为真,就loop循环$list数组,$funds为每一项。那个$multi是分页用的

最后结果:

093155_zoYU_2261111.png

大功告成!(两个困惑点还未解决)

转载于:https://my.oschina.net/zhangxuman/blog/1548609

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值