简体   繁体   中英

can't display data result codeigniter

I can't display the data it said undefined variable : avg . this is my script:

my 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);
}

and the 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>

the $avg['hasil_rating'] is I used to display the data from $data['avg']

should I use model? I don't use model because its gonna save a lot of time. thank you before.

In your Code :

    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);
   }

you have redirected before php getting the $data['avg']. Try removing that redirect and see.

A suggestion for you.

You should be using model to perform database related activities otherwise it is meaningless to use codeigniter framework and when you are using codeigniter try to utilize its maximum functionality.

You should go through the documentation once for more details. (https://www.codeigniter.com/user_guide/)

By going through your code I came to notice that you forgot to fetch the result. result_array() is used for fetching more than one rows in array format.

$data['avg'] = $this->db->select_avg("hasil_rating")->get("tb_rating")->result_array();

for displaying

 echo $avg[0]['hasil_rating'];

If it is a single row then row() should be used which gives you the result as object

$data['avg'] = $this->db->select_avg("hasil_rating")->get("tb_rating")->row();

And for displaying

echo $avg->hasil_rating;

At first, you failed to send data into view, because redirection was execute before sending data.

Secondly, to fetch result from database you should read this:

https://www.codeigniter.com/user_guide/database/results.html?highlight=result

There you can get a clear notion with examples.

So, if you want to fetch a single result row then use,

row() // it returns an object.
row_array // it returns an array

For instance,

$data['avg'] = $this->db->select_avg("hasil_rating")
                            ->get("tb_rating")->row_array();

And delete or uncomment this line.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM