Help guys.
This my error problem error:
I Think i follow right tutorial, but i don't know what the wrong from my code
there is my controllers
public function edit($id){
$where = array('id' => $id);
$data['barang'] = $this->model_barang->edit($where,'barang')->results();
$this->load->view('edit',$data);
}
public function update(){
$id = $this->input->post('id');
$jenis = $this->input->post('jenis');
$nama = $this->input->post('nama');
$harga = $this->input->post('harga');
$pemasok = $this->input->post('pemasok');
$data = array (
'jenis' => $jenis,
'nama'=> $nama,
'harga' => $harga,
'pemasok' => $pemasok
);
$where = array(
'id' => $id
);
$this->model_barang->update($where,$data,'barang');
redirect('barang/tampil');
}
and there is my model, i think my model is not showing any error
public function edit($where,$table) {
return $this->db->get($table,$where);
}
public function update($where,$data,$table){
$this->db->where($where);
$this->db->update($table,$data);
}
You need to pass the $id to the controller.
It should be something like this http://domain.com/barang/edit/1
Related
Sorry for my bad english but i have a problem when i try to open localhost:8080/blog this message show up
Too few arguments to function App\Controllers\Blog::view(), 0 passed in C:\xampp\htdocs\baru\vendor\codeigniter4\framework\system\CodeIgniter.php on line 896 and exactly 1 expected
so this is the controller:
use CodeIgniter\Controller;
use App\Models\ModelsBlog;
class Blog extends BaseController
{
public function index()
{$data = [
'title' => 'artikel'
];
$model = new ModelsBlog();
if (!$this->validate([]))
{
$data['validation'] = $this->validator;
$data['artikel'] = $model->getArtikel();
return view('view_list',$data);
}
}
public function form(){
$data = [
'title' => 'Edit Form'
];
helper('form');
return view('view_form', $data);
}
public function view($id){
$data = [
'title' => 'artikel'
];
$model = new ModelsBlog();
$data['artikel'] = $model->PilihBlog($id)->getRow();
return view('view',$data);
}
public function simpan(){
$model = new ModelsBlog();
if ($this->request->getMethod() !== 'post') {
return redirect()->to('blog');
}
$validation = $this->validate([
'file_upload' => 'uploaded[file_upload]|mime_in[file_upload,image/jpg,image/jpeg,image/gif,image/png]|max_size[file_upload,4096]'
]);
if ($validation == FALSE) {
$data = array(
'judul' => $this->request->getPost('judul'),
'isi' => $this->request->getPost('isi')
);
} else {
$upload = $this->request->getFile('file_upload');
$upload->move(WRITEPATH . '../public/assets/blog/images/');
$data = array(
'judul' => $this->request->getPost('judul'),
'isi' => $this->request->getPost('isi'),
'gambar' => $upload->getName()
);
}
$model->SimpanBlog($data);
return redirect()->to('./blog')->with('berhasil', 'Data Berhasil di Simpan');
}
public function form_edit($id){
$data = [
'title' => 'edit artikel'
];
$model = new ModelsBlog();
helper('form');
$data['artikel'] = $model->PilihBlog($id)->getRow();
return view('form_edit',$data);
}
public function edit(){
$model = new ModelsBlog();
if ($this->request->getMethod() !== 'post') {
return redirect()->to('blog');
}
$id = $this->request->getPost('id');
$validation = $this->validate([
'file_upload' => 'uploaded[file_upload]|mime_in[file_upload,image/jpg,image/jpeg,image/gif,image/png]|max_size[file_upload,4096]'
]);
if ($validation == FALSE) {
$data = array(
'judul' => $this->request->getPost('judul'),
'isi' => $this->request->getPost('isi')
);
} else {
$dt = $model->PilihBlog($id)->getRow();
$gambar = $dt->gambar;
$path = '../public/assets/blog/images/';
#unlink($path.$gambar);
$upload = $this->request->getFile('file_upload');
$upload->move(WRITEPATH . '../public/assets/blog/images/');
$data = array(
'judul' => $this->request->getPost('judul'),
'isi' => $this->request->getPost('isi'),
'gambar' => $upload->getName()
);
}
$model->edit_data($id,$data);
return redirect()->to('./blog')->with('berhasil', 'Data Berhasil di Ubah');
}
public function hapus($id){
$model = new ModelsBlog();
$dt = $model->PilihBlog($id)->getRow();
$model->HapusBlog($id);
$gambar = $dt->gambar;
$path = '../public/assets/blog/images/';
#unlink($path.$gambar);
return redirect()->to('./blog')->with('berhasil', 'Data Berhasil di Hapus');
}
}
ModelsBlog.php :
use CodeIgniter\Model;
class ModelsBlog extends Model
{
protected $table = 'artikel';
public function getArtikel()
{
return $this->findAll();
}
public function SimpanBlog($data)
{
$query = $this->db->table($this->table)->insert($data);
return $query;
}
public function PilihBlog($id)
{
$query = $this->getWhere(['id' => $id]);
return $query;
}
public function edit_data($id,$data)
{
$query = $this->db->table($this->table)->update($data, array('id' => $id));
return $query;
}
public function HapusBlog($id)
{
$query = $this->db->table($this->table)->delete(array('id' => $id));
return $query;
}
}
And this is the view.php:
<body style="width: 70%; margin: 0 auto; padding-top: 30px;">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2><?php echo $artikel->judul; ?></h2>
</div>
</div>
</div>
<hr>
<div class="row">
<div class="col-lg-12">
<div class="row">
<?php
if (!empty($artikel->gambar)) {
echo '<img src="'.base_url("assets/blog/images/$artikel->gambar").'" width="30%">';
}
?>
<?php echo $artikel->isi; ?>
</div>
</div>
</div>
</body>
i cant find any solutions for this error, pls help thank you very much
Let's go over what you're telling the code to do.
First, you make a call to /blog. If you have auto-routing turned on this will put you forward to the controller named 'Blog'.
class Blog extends BaseController
And since you do not extend the URL with anything, the 'index' method will be called.
public function index()
{$data = [
'title' => 'artikel'
];
$model = new ModelsBlog();
if (!$this->validate([]))
{
$data['validation'] = $this->validator;
$data['artikel'] = $model->getArtikel();
return view('view_list',$data);
}
}
The index method sets $data to an array filled with 'title' => 'artikel'. And then fills $model with a new ModelsBlog.
class ModelsBlog extends Model
There is no __construct method defined in ModelsBlog so just the class is loaded and specific execution related to $model stops there, which is fine.
Then, the index() from Blog goes on and checks whether or not $this->validate([]) returns false. Since there's no else statement, if $this->validate([]) were to return true, code execution would stop there. So we'll assume $this->validate([]) returns false. So far so good, there's nothing weird going on with your code.
However, IF $this->validate([]) returns false, you tell the index() to return the function called view(). Normally CodeIgniter would serve you the view you set as the first parameter. But since you also have a Blog method named 'view', CodeIgniter will try to reroute te request to that method. So in other words, the actual request you're trying to make is:
Blog::view()
And since you've stated that view() receives 1 mandatory parameter, the requests triggers an error. You can solve the problem by either renaming the view() method of Blog to something like 'show()' or 'read()'. Anything else that does not conflict with the native CodeIgniter view() function would be good.
Honestly though, you are sending through two parameters in the index() function call so I'm slightly confused why the error generated states you provided 0, but I hope at least you gain some insight from my answer and you manage to fix the problem.
If anyone could provide more information regarding this, feel free to comment underneath and I'll add your information to the answer (if it gets accepted).
Code
public function import(Request $request)
{
$id= $request->id;
$limit = $request->limit;
$path = $request->file('file')->store('uploads');
$products = (new FastExcel)->import('public/' . $path, function ($line) {
return Data::create([
'uname' => $line['name'],
'username' => $line['username'],
'limit' => $limit,
'group_id' =>$id
]);
});
flash(__('Your products imported successfully'))->success();
return redirect()->route('products.index');
}
Thhis is not woking id and limit not defined
I need solution for this im useing laravel 6.2 with FastExcel
I am just new to Codeigniter. I want to display the total sum of bill_rpt of this SQL query in the view page. here's the code
this is my Model
public function total() {
$sql="SELECT bill_rpt
FROM amilyar_bill
WHERE bill_status = 1";
return $this->db->query($sql);
}
this is my Controller
public function payments($token=''){
$this->isLoggedIn();
$data = array(
// get data using email
'token' => $token,
'admin_info' => $this->model->getAdminInfo($this->session->userdata('email'))->row(),
'notifications' => $this->model->notification_admin($this->session->userdata('email'))->result_array(),
'notification' => $this->model->all_notification_admin($this->session->userdata('email'))->result_array(),
'total' => $this->model->total($this->session->userdata('email'))->result_array(),
);
if ($this->session->userdata('position_id') == 2) {
$this->load->view('includes/admin_header',$data);
$this->load->view('admin/reports/payments',$data);
} else {
$this->logout();
}
}
this is my View
<h4 class="pull-right">Total:</h4>
Thanks in advance
Hope this will help you :
Use ci select_sum() to get the sum
Your total method should be like this :
public function total()
{
$this->db->select_sum('bill_rpt');
$this->db->where('bill_status' , '1');
$query = $this->db->get('amilyar_bill');
return $query->row()->bill_rpt;
}
Your $data part in controller payments method should be like this :
$data = array(
'token' => $token,
'admin_info' => $this->model->getAdminInfo($this->session->userdata('email'))->row(),
'notifications' => $this->model->notification_admin($this->session->userdata('email'))->result_array(),
'notification' => $this->model->all_notification_admin($this->session->userdata('email'))->result_array(),
'total' => $this->model->total()
);
In payments view part use $total like this :
<h4 class="pull-right">Total: <?=$total;?></h4>
You can use num_rows() method of Active Class.
$query->num_rows();
public function total(){
$sql="SELECT bill_rpt FROM amilyar_bill WHERE bill_status = 1";
$query = $this->db->query($sql);
return $query->num_rows();
}
And in View,
<h4 class="pull-right">Total: <?php echo $total;?></h4>
Try SUM function of MySql as:
In Model
public function total(){
$sql="SELECT SUM(bill_rpt ) as total_sum
FROM amilyar_bill
WHERE bill_status = 1";
return $this->db->query($sql);
}
I am having trouble saving this into database. When I submit my data into database it will only show name_of_bear and all the many relationship stuff(type_of_fish) but not the type_of_bear
Can someone explain to me why it can't work and also maybe give me an example on how it should be done. Thank you
Controller: (this works)
public function submit(Request $request)
{
$fishType= $request->input('type_of_fish');
$Name = $request->input('Name');
$bearType = $request->input('bearType');
$bear = Bear::create(['Name' => $request->input('name_of_bear')]);
$bear->fishs()->create(['type_of_fish' => json_encode($fishType)]);
return ('thank you');
}
But if I were to do this:
Controller : (doesn't work)
public function submit(Request $request)
{
$fishType= $request->input('type_of_fish');
$Name = $request->input('Name');
$bearType = $request->input('bearType');
$bear = Bear::create(['Name' => $Name]);
$bear = Bear::create(['bearType' => $bearType]); --> doesn't work if add in this
$bear->fishs()->create(['type_of_fish' => json_encode($fishType)]);
return ('thank you');
}
or this:
Controller: (doesn't work)
public function submit(Request $request)
{
$fishType= $request->input('type_of_fish');
$Name = $request->input('Name');
$bearType = $request->input('bearType');
$bear = Bear::create(['Name' => $Name], ['bearType' => $bearType]); --> doesn't work
$bear->fishs()->create(['type_of_fish' => json_encode($fishType)]);
return ('thank you');
}
You can add it like this
$bear = Bear::create(['Name' => $Name , 'bearType' => $bearType]);
full code:
public function submit(Request $request)
{
$fishType= $request->input('type_of_fish');
$Name = $request->input('Name');
$bearType = $request->input('bearType');
$bear = Bear::create(['Name' => $Name , 'bearType' => $bearType]);
return ('thank you');
} // removed extra }
For further information you can read documentation
You can just use:
$bear = Bear::create(['Name' => $Name, 'bearType' => $bearType]);
For more info, visit this link.
You can insert data like this:
public function submit(Request $request)
{
$data = array();
$data['type_of_fish']= $request->type_of_fish;
$data['Name'] = $request->Name;
$data['bearType'] = $request->bearType;
$bear = Bear::create($data);
return ('thank you');
}
For more information read Documentation
I would do it like this, assuming that post array keys matches column names.
$bear = Bear::create($request->all()->except(['_token', 'type_of_fish']));
it's my controller
function set_hargabesar($id){
$this->load->library('cart');
$condition['id'] = 'id';
$get = $this->myigniter_model->getharga('harga_satuan','id');
$data = array(
'rowid' => $id,
'qty' => 5,
'price' => $get,
);
$this->cart->update_all($data);
}
it's my model
public function getharga($harga, $id){
$this->db->select($harga);
$this->db->from('barang');
$this->db->where('id',$id);
$query=$this->db->get();
}
How can I get a 'harga_satuan' (price cart) from database
please help me.
I can get the data(harga_satuan) from database with this code.
You've only executed your query, you need to retrieve data.
Model:
public function getharga($harga, $id){
$this->db->select($harga);
$this->db->from('barang');
$this->db->where('id',$id);
$query=$this->db->get();
$row = $query->row_array();
return $row;
}
In your controller just change this:
'price' => $get['harga_satuan'],
And, of course, you need to pass id to your model:
$get = $this->myigniter_model->getharga('harga_satuan', $id);
(but I hope you are doing it)