Postingan lainnya
Error setelah menambahkan content yaitu " Array to string conversion"
Capture.PNGBerikut gambaran diatas form konten yang menyebabkan error ketika memakai tombol add+
Dan dibawah adalah gambar errornya
Capture1.PNG
Berikut codingannya :
Model
<?php
class Report_model extends CI_Model
{
private $_table = 'report';
public function get()
{
$query = $this->db->get($this->_table);
return $query->result();
}
public function get_published($limit = null, $offset = null)
{
if (!$limit && $offset) {
$query = $this->db
->get_where($this->_table, ['draft' => 'FALSE']);
} else {
$query = $this->db
->get_where($this->_table, ['draft' => 'FALSE'], $limit, $offset);
}
return $query->result();
}
public function find_by_slug($slug)
{
if (!$slug) {
return;
}
$query = $this->db->get_where($this->_table, ['slug' => $slug]);
return $query->row();
}
public function find($id)
{
if (!$id) {
return;
}
$query = $this->db->get_where($this->_table, array('id' => $id));
return $query->row();
}
public function insert($report)
{
return $this->db->insert($this->_table, $report);
}
public function update($report)
{
if (!isset($report['id'])) {
return;
}
return $this->db->update($this->_table, $report, ['id' => $report['id']]);
}
public function delete($id)
{
if (!$id) {
return;
}
return $this->db->delete($this->_table, ['id' => $id]);
}
public function count(){
return $this->db->count_all($this->_table);
}
}
Controller
<?php
class Post extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('report_model');
}
public function index()
{
$data['reports'] = $this->report_model->get();
if(count($data['reports']) <= 0){
$this->load->view('admin/post_empty.php');
} else {
$this->load->view('admin/post_list.php', $data);
}
}
public function new()
{
if ($this->input->method() === 'post') {
// TODO: Lakukan validasi sebelum menyimpan ke model
// generate unique id and slug
$id = uniqid('', true);
$slug = url_title($this->input->post('title'), 'dash', TRUE) . '-' . $id;
$report = [
'id' => $id,
'title' => $this->input->post('title'),
'slug' => $slug,
'content' => $this->input->post('content'),
'draft' => $this->input->post('draft')
];
$saved = $this->report_model->insert($report);
if ($saved) {
$this->session->set_flashdata('message', 'Report was created');
return redirect('admin/post');
}
}
$this->load->view('admin/post_new_form.php');
}
public function edit($id = null)
{
$data['report'] = $this->report_model->find($id);
if (!$data['report'] || !$id) {
show_404();
}
if ($this->input->method() === 'post') {
// TODO: lakukan validasi data seblum simpan ke model
$report = [
'id' => $id,
'title' => $this->input->post('title'),
'content' => $this->input->post('content'),
'draft' => $this->input->post('draft')
];
$updated = $this->report_model->update($report);
if ($updated) {
$this->session->set_flashdata('message', 'Report was updated');
redirect('admin/post');
}
}
$this->load->view('admin/post_edit_form.php', $data);
}
public function delete($id = null)
{
if (!$id) {
show_404();
}
$deleted = $this->report_model->delete($id);
if ($deleted) {
$this->session->set_flashdata('message', 'Report was deleted');
redirect('admin/post');
}
}
}
View
<!DOCTYPE html>
<html lang="en">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<head>
<?php $this->load->view('admin/_partials/head.php') ?>
</head>
<body>
<main class="main">
<?php $this->load->view('admin/_partials/side_nav.php') ?>
<div class="content">
<h1>Write new Report</h1>
<form action="" method="POST">
<label for="title">Title*</label>
<input type="text" name="title" placeholder="Judul Report" required title="Wajib tulis judul Report"/>
<div class="control-group after-add-more">
<label for="content">Konten</label>
<textarea name="content[]" cols="30" rows="10" placeholder="Tuliskan isi pikiranmu..."></textarea>
</div>
<br>
<button class="btn btn-success add-more" type="button">
<i class="glyphicon glyphicon-plus"></i> Add
</button>
<hr>
<div>
<button type="submit" name="draft" class="button" value="true">Save to Draft</button>
<button type="submit" name="draft" class="button button-primary" value="false">Publish</button>
</div>
</form>
<div class="copy hide">
<div class="control-group">
<label for="content">Konten</label>
<textarea name="content[]" cols="30" rows="10" placeholder="Tuliskan isi pikiranmu..."></textarea>
<br>
<button class="btn btn-danger remove" type="button"><i class="glyphicon glyphicon-remove"></i> Remove</button>
<hr>
<?php $this->load->view('admin/_partials/footer.php') ?>
</div>
</main>
<script type="text/javascript">
$(document).ready(function() {
$(".add-more").click(function(){
var html = $(".copy").html();
$(".after-add-more").after(html);
});
// saat tombol remove dklik control group akan dihapus
$("body").on("click",".remove",function(){
$(this).parents(".control-group").remove();
});
});
</script>
</body>
</html>
0
2 Jawaban:
<div>nilai contentnya dianggap array. Coba cek lagi, harusnya tipe datanya string</div>
0
<div>terima kasih mas hilman sudah solved, ternyata ada data yang array</div>
0