php - 无法显示数据结果codeigniter

标签 php html mysql codeigniter codeigniter-3

我无法显示 undefined variable :平均值所说的数据。这是我的脚本:

我的 Controller :

public function rate_config() {
        $rt = $_POST['hasil_rating'];
        $content["hasil_rating"] = $rt;

        $this->db->insert("tb_rating", $content);
        redirect("rate/index?status=tambah_success","refresh");

        $data['avg'] = $this->db->select_avg("hasil_rating")
                                ->get("tb_rating");
        $this->load->view('rating', $data);
}

和 View :

<form method="post" action="<?php echo base_url('rate/rate_config');?>" enctype="multipart/form-data">
    <input class="rating" name="hasil_rating" data-stars="5" data-step="0.5">
    <div class="form-group" style="margin-top:10px">
        <button type="submit" class="btn btn-success">RATE</button>
    </div>
    <div>
</form>
        <label class="label label-primary"><?=$avg['hasil_rating']?></label>
    </div>
<hr>

我用$avg['hasil_ rating']来显示来自$data['avg']的数据

我应该使用模型吗?我不使用模型,因为它会节省很多时间。先谢谢你了。

最佳答案

在您的代码中:

    public function rate_config() {
    $rt = $_POST['hasil_rating'];
    $content["hasil_rating"] = $rt;

    $this->db->insert("tb_rating", $content);

    /*==================Here====================*/
    redirect("rate/index?status=tambah_success","refresh");

    $data['avg'] = $this->db->select_avg("hasil_rating")
                            ->get("tb_rating");
    $this->load->view('rating', $data);
   }

您在 php 获取 $data['avg'] 之前已重定向。尝试删除该重定向并查看。

关于php - 无法显示数据结果codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49325239/

相关文章:

php - 在字符串中查找第一个大写字符的最快方法

php - 删除条目后更新mysql数据库

php - 多个 mysql_connect 语句相互抵消

html - Bootstrap with ASP.net 不使用全屏

javascript - 当宽度改变(文本改变)时平滑地移动居中的元素(文本)

PHP 登录页面存在哈希密码问题

mysql - 应用分享

php - 单击行以使用其数据预填充表单仅显示第一条记录

javascript - 使用附加变量 jQuery

php - 被连接覆盖的 ID 列