Data tidak mau dipanggil dari database selalu error stdClass: Result()

Izin bertanya, jadi saya mau nyoba panggil data dari sample database. Disini saya coba pakai foreach untuk row->result, setelah dipanggil data tidak mau muncul. Padahal dalam class index sudah saya masukan semua data dalam row seperti gambar dibawah, namun saat saya tes tetap tidak mau muncul. Mohon bantuannya gan, terimakasih.

Error pada stdClass: Result()Yang diharapkan muncul data ini


<!-- Content -->
<section class="content-area p-y-1">
    <div class="container-fluid">
        <h4><?= ucfirst($page) ?> Span</h4>
        <div class="pull-right ">
            <a href="<?= site_url('span') ?>">
                <button type="button" class="btn btn-warning pull-left m-r-0-5 label-left waves-effect waves-light">
                    <span class="btn-label"><i class="ti-arrow-left"></i></span>
                    Back
                </button>
            </a>
        </div>
        <ol class="breadcrumb no-bg m-b-1">
            <li class="breadcrumb-item"><a href="<?= site_url() ?>">Dashboard</a></li>
            <li class="breadcrumb-item active">New Span</li>
        </ol>
    </div>

    <!-- Main Content -->
    <div class="container-fluid">
        <div class="card card-block">
            <div class="row">
                <div class="col-md-12">
                    <div class="pull-right ">
                        <a href="<?= site_url('span') ?>">Cancel</a>
                    </div>
                    <h5>Input the new span below</h5>
                    <text>Can input once or major</text>
                    <br>
                    <div class="box-body">
                        <form action="<?= site_url('span/process') ?>" method="post">
                            <div class="row">
                                <div class="col-md-4"><br>
                                    <div class="form-group">
                                        <label><strong>Span Name</strong></label>
                                        <input type="text" class="form-control" value="<?= $row->span_name ?>" name="span_name" placeholder="Fill the Span name">
                                    </div>
                                    <div class="form-group">
                                        <label><strong>Description</strong></label>
                                        <textarea style="resize:none;height:57px;" class="form-control" name="description" placeholder="Fill the Description"><?= $row->description ?></textarea>
                                    </div>
                                    <div class="form-group">
                                        <button type="submit" name="<?= $page ?>" class="btn btn-success btn-flat">
                                            <i class="fa fa-paper-plane"></i> Save</button>
                                        <button type="reset" class="btn btn-flat">Reset</button>
                                    </div>
                                </div>
                                <div class="col-md-8">
                                    <table class="table table-striped table-bordered dataTable display nowrap" style="width:100%" id="table">
                                        <thead>
                                            <tr>
                                                <th>Span Name</th>
                                                <th>Description</th>
                                                <th>Edit</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <!-- <?php $no = 1;
                                                foreach($row->result() as $key => $data) { ?>
                                                <tr>
                                                    <td><?=$data->span_name?></td>
                                                    <td><?=$data->description?></td>
                                                    <td width="50px" align="center">
                                                        <a href="<?=site_url('span/edit/'.$data->span_id)?>" class="btn btn-primary btn-xs">
                                                            <i class="fa fa-pencil"></i>
                                                        </a>
                                                        <?php if($this->fungsi->user_login()->level == 1) { ?>
                                                        <a href="<?=site_url('span/del/'.$data->span_id)?>" onclick="return confirm('Apakah anda yakin?')" class="btn btn-danger btn-xs">
                                                            <i class="fa fa-trash"></i>
                                                        </a>
                                                        <?php } ?>
                                                    </td>
                                                </tr>
                                            <?php }?> -->
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th>Span Name</th>
                                                <th>Description</th>
                                                <th>Edit</th>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div>
                            </div>

                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

Ini View


<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Span extends CI_Controller {

    function __construct() {

        parent::__construct();
        check_not_login();
        $this->load->model('span_centralregion');

    }

    public function index() {


        $data['row'] = $this->span_centralregion->get();

        $span = new stdClass();
        $span->span_id = null;
        $span->span_name = null;
        $span->description = null;

        $data = array(
            'page' => 'add',
            'row' => $span,
        );
        $this->template->load('template', 'support_manager/span/span_data', $data);
    }

    public function edit($id) {

        $query = $this->span_centralregion->get($id);
        if($query->num_rows() > 0) {
            $span = $query->row();
            $data = array(
                'page' => 'edit',
                'row' => $span,
            );
            $this->template->load('template', 'support_manager/span/span_data', $data);
        } else {
            echo "<script>alert('Data tidak ditemukan');";
            echo "window.location='".site_url('user')."';</script>";
        }
    }

    public function process() {

        $post = $this->input->post(null, TRUE);
        if(isset($_POST['add'])) {
            $this->span_centralregion->add($post);
        }
        if(isset($_POST['edit'])) {
            $this->span_centralregion->edit($post);
        }

        if($this->db->affected_rows() > 0) {
            echo "<script>alert('Data berhasil disimpan');</script>";
        }
        echo "<script>window.location='".site_url('span')."'</script>";
    }

    public function del($id) {

        $this->span_centralregion->del($id);

        if($this->db->affected_rows() > 0) {
            echo "<script>alert('Data berhasil dihapus');</script>";
        }
        echo "<script>window.location='".site_url('span')."';</script>";
    }
}

Ini Controller


<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Span_centralregion extends CI_Model {

    public function get($id = null) {

        $this->db->from('dm_span');
        if($id != null) {
            $this->db->where('span_id', $id);
        }
        $query = $this->db->get();
        return $query;
    }

    public function add($post) {

        $params = [
            'span_name' => $post['span_name'],
            'description' => $post['description'],
        ];

        $this->db->insert('dm_span', $params);
    }

    public function edit($post) {

        $params = [
            'span_name' => $post['span_name'],
            'description' => $post['description'],
        ];

        $this->db->where('span_id', $post['id']);
        $this->db->update('dm_span', $params);
    }

    public function del($id) {

    $this->db->where('span_id', $id);
    $this->db->delete('dm_span');
    }

}

Ini Model

Ekpetasi

avatar vesaldiy
@vesaldiy

6 Kontribusi 0 Poin

Diperbarui 2 tahun yang lalu

Tanggapan

Coba potongan kodenya jangan di screenshoot, tapi dilampirkan pakai tag kode. Agar bisa di-copas

Sudah diganti gan..

1 Jawaban:

Jawaban Terpilih

<div>Kenapa bisa error? Sepertinya karena kamu melakukan assign ke $data di method indexnya, Jadi pada baris ini :</div><pre>$data['row'] = $this-&gt;span_centralregion-&gt;get();</pre><div>Kamu sudah set nilai variabel $data. Kemudian kamu set-ulang nilai $data-nya pada bagian ini :</div><pre>$data = array( 'page' =&gt; 'add', 'row' =&gt; $span, );</pre><div><br>makanya, ketika dikases $row-&gt;result() di view, dia menimbulkan error, karena nilai variabel $row -nya sudah berubah.</div><div><br>Solusinya coba method index di controllernya di ubah jadi sperti ini :</div><pre>public function index() {

$span = new stdClass();
$span-&amp;gt;span_id = null;
$span-&amp;gt;span_name = null;
$span-&amp;gt;description = null;

$data = array(
    &#039;page&#039; =&amp;gt; &#039;add&#039;,
    &#039;row&#039; =&amp;gt; $span,
    &#039;row_data&#039; =&amp;gt; $this-&amp;gt;span_centralregion-&amp;gt;get(),
);

$this-&amp;gt;template-&amp;gt;load(&#039;template&#039;, &#039;support_manager/span/span_data&#039;, $data);

}</pre><div><br>Kemudian untuk view-nya seperti ini :</div><pre>&lt;?php $no = 1; foreach($row_data-&gt;result() as $key =&gt; $data) { ?&gt; &lt;tr&gt; &lt;td&gt;&lt;?=$data-&gt;span_name?&gt;&lt;/td&gt; &lt;td&gt;&lt;?=$data-&gt;description?&gt;&lt;/td&gt; &lt;td width="50px" align="center"&gt; &lt;a href="&lt;?=site_url('span/edit/'.$data-&gt;span_id)?&gt;" class="btn btn-primary btn-xs"&gt; &lt;i class="fa fa-pencil"&gt;&lt;/i&gt; &lt;/a&gt; &lt;?php if($this-&gt;fungsi-&gt;user_login()-&gt;level == 1) { ?&gt; &lt;a href="&lt;?=site_url('span/del/'.$data-&gt;span_id)?&gt;" onclick="return confirm('Apakah anda yakin?')" class="btn btn-danger btn-xs"&gt; &lt;i class="fa fa-trash"&gt;&lt;/i&gt; &lt;/a&gt; &lt;?php } ?&gt; &lt;/td&gt; &lt;/tr&gt; &lt;?php }?&gt;</pre><div><br>Semoga berhasil.</div>

avatar ahanafi
@ahanafi

815 Kontribusi 554 Poin

Dipost 2 tahun yang lalu

Tanggapan

Terimakasih, barusan saya coba dan berhasil. Ternyata dugaan saya benar di sekitar index errornya tapi karena tidak sadar ada variabel yang telah diatur dua kali. Sekali lagi terimakasih pak..

Sama-sama, senang bisa membantu!

Login untuk ikut Jawaban