I'm working with CakePHP now and I need to do a search with one view and then send the data to another action on the same controller, that will trigger another view with the results of that search. The thing is, on the search screen, there's also a table showing some data of the same model, and I believe that's one big problem.
So right now, it's here what I got:
public function busca() {
$emergency = TableRegistry::get('EmergencySheets');
$manufacturers = TableRegistry::get('Manufacturers');
$data = $this->request->is('get') ? $this->request->query : $this->request->getData();
$query = $emergency->find()
->select(['id', 'data_atualizacao_fabricante', 'tarja', 'manufacturer_id', 'nome_comercial'])
->where('EmergencySheets.data_atualizacao_fabricante')
->order(['data_atualizacao_fabricante'=>'DESC'])
->limit(7);
$manufacturer_query = $manufacturers->find()
->select(['id','nome'])
->where($query->manufacturer_id = 'id');
$manufacturer = $manufacturer_query->toArray();
$sheets = $query->toArray();
$this->set('manufacturers', $manufacturer);
$this->set('sheets', $sheets);
if($data){
return $this->redirect(['action' => 'ficha' , $data]);
}else{
return $this->redirect(['action' => 'busca404']);
}
}
How can I handle this?
Thank you all!
Edit:
Forgot to mention, but the $data variable always come empty on the form, even when I type something on the form input. Here's the view code, too!
<section class="search-section">
<div class="container px-0">
<div class="search-wrapper">
<div class="search-title">
<h2><span>Quais produtos</span>você vai transportar?</h2>
<p><span>Pesquise pelos produtos no campo de busca</span>
ou clique nas letras ao lado.
</p>
</div>
<div class="search-bar">
<?=$this->Form->create()?>
<div class="ml-5 bar">
<input type="text" placeholder="Procure várias fichas de uma só vez" class="formcontrol tip"
data-toggle="tooltip" data-placement="top">
<span class="removeClick"><i class="fas fa-times-circle fa-2x"></i></span>
<button type="submit" class="btn"><i class="fa fa-search fa-2x"></i></button>
</div>
<?=$this->Form->end()?>
<div class="ml-5 alfabeto text-center">
<button href="#A">A</button> <button href="#B">B</button> <button href="#C">C</button> <button href="#D">D</button>
<button href="#E">E</button> <button href="#F">F</button> <button href="#G">G</button> <button href="#H">H</button>
<button href="#I">I</button> <button href="#J">J</button> <button href="#K">K</button> <button href="#L">L</button>
<button href="#M">M</button> <button href="#N">N</button> <button href="#O">O</button> <button href="#P">P</button>
<button href="#Q">Q</button> <button href="#R">R</button> <button href="#S">S</button> <button href="#T">T</button>
<button href="#U">U</button> <button href="#V">V</button> <button href="#W">W</button> <button href="#X">X</button>
<button href="#Y">Y</button> <button href="#Z">Z</button><button href="#0-9">0-9</button>
</div>
</div>
</div>
</div>
I would use the same function and template for the search form and results. Eliminate the whole if section with redirects, only do the search if there is data to search on, and change your view to check whether there are results to display. Something like this:
$data = $this->request->is('get') ? $this->request->getQueryParams() : $this->request->getData();
if (!empty($data)) {
// Do your searches using $data here, set the results for the view
$this->set('results', $results);
}
Then in your template, you have it like you've shown, but add a section with
if (isset($results)):
// Display your search results here
endif;
Please consider that you are puting code after the line:
return $this->redirect(['action' => 'busca404']);
}
In that case all those lines wont be executed in any case because you are forcing redirects either if the request is "get" or not. So all that code wont be executed.
I think that you need to define the conditions to redirect to the "ficha" action and in which conditions remain in to the "busca" action
Related
Codeigniter: Message: Trying to get property of non-object Filename: edit/edit_nilai.php Line Number: 9
controller:
$this->load->model('M_data');
$data['f']=$this->M_data->selectNilai($this->uri->segment(6));
$this->load->view('kurikulum/penilaian/penilaian/edit/edit_nilai',$data);
model:
public function selectNilai($id)
{
$this->db->where('id_nilai_siswa', $id);
return $this->db->get('nilai_siswa')->row();
}
views (the error file edit/edit_nilai) the bold code is the error line
<html>
<body>
<div class="modal-dialog " >
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h2 class="modal-title">Edit Nilai Siswa</h4>
</div>
<div class="modal-content">
**<form class="form-horizontal formgrup " action="<?php echo base_url('penilaian/ubah_nilai/'.$f->id_nilai_siswa); ?>" method="post" >**
<div class="bigbox-mapel" >
<div class="box-mapel">
</div>
<div class="modal-footer">
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button class="btn btn-success" type="submit">Submit</button>
<button class="btn btn-danger" data-dismiss="modal" href="#lihatkategori" data-toggle="tab">Back</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
Please check below steps.
1. Please check id exist or not.
2. Please print your result.
3. Please try using below method.
public function selectNilai($id)
{
echo $id;
$this->db->where('id_nilai_siswa', $id);
return $this->db->get('nilai_siswa')->result();
}
echo $f[0]->id_nilai_siswa;
Because your model function selectNilai($id) is returning nothing.
Possible cause:
No matching value found in the table with given $id
Solution:
Try to check the value of $id, and manually check if exists in the table.
Best practice:
Always check the value of returned query result before sending it for processing (or write your processing mechanism in a way to handle error). In this case what you can actually do is something like below:
public function selectNilai($id){
$this->db->select('*')->from('nilai_siswa')->where('id_nilai_siswa', $id);
$query = $this->db->get();
if($query->num_rows()){
//found some data, handle it
return $query->result();
} else {
//no matching data found
return FALSE;
}
}
OR
public function selectNilai($id){
$this->db->where('id_nilai_siswa', $id);
$data = $this->db->get('nilai_siswa')->row();
if(isset($data)) return $data; //data found, return it for further processing
else return FALSE; //no data found, handle the case
}
Ref: https://www.codeigniter.com/userguide3/database/results.html#result-rows
Whenever I try to access /skills/add through an anchor in another page I get this error
The anchor that redirects to this page(with GET method) is:
<a class="btn icon-btn btn-success" href="/skills/add">
<span class="glyphicon btn-glyphicon glyphicon-plus img-circle text-success"></span>
Add
</a>
Tried using dd("test") to test it out but won't even work.
This are my routes for skills/add this:
Route::put('/skills/add', 'SkillsController#add');
Route::get('/skills/add', 'SkillsController#addIndex');
Here are my functions in SkillsController
public function addIndex() {
if (Auth::check()) {
return view('skills.add');
} else {
return redirect('/home');
}
}
public function add(Request $request) {
/*Sets validation rules for Skill object*/
$skillRules = [
'name' => 'required|max:25|regex:/[1-9a-zA-Z ]\w*/',
];
if (Skills::where('name', '=', $request->name)->count() > 0) {
return redirect('/skills')->with('message', "EXISTS");
}
$validator = Validator::validate($request->all(), $skillRules);
if ($validator == null) {
$newSkill = new Skills;
$newSkill->name = strtolower($request->name);
$newSkill->save();
return redirect('/skills')->with('message', "CREATED");
}
}
the skills.add view is this
#extends('layouts.app')
#section('content')
<div class="container">
<h1>Edit Skill</h1>
<form method="POST" action="/skills/add">
{{method_field('PUT')}}
{{csrf_field()}}
<div class="row">
<div class="col-lg-5">
<div class="form-group">
Name:
<input name="name" class="form-control">
</div>
</div>
</div>
<div class="row">
<div class="col-lg-2">
<div class="form-group">
<button type="submit" class="btn btn-primary">Add Skill</button>
<button type="button" class="btn btn-danger">Cancel</button>
</div>
</div>
</div>
</form>
</div>
#endsection
Not sure what happened, if someone in the future has this same problem, I literally just deleted and made manually thee controller, model and blade and it started to work. Not a lot of science or explain, sorry for that.
Change your route to named ones
Route::GET('/skills/add', 'SkillsController#addIndex')->name('addSkills');
Route::POST('/skills/add', 'SkillsController#add')->name('saveSkills');
and your blade to
#extends('layouts.app')
#section('content')
<div class="container">
<h1>Edit Skill</h1>
<form method="POST" action="{{route('saveSkills')"> //** Change this
{{csrf_field()}}
<div class="row">
<div class="col-lg-5">
<div class="form-group">
Name:
<input type="text" name="name" class="form-control"> //** Change this
</div>
</div>
//Remaining form groups
</div>
<div class="row">
<div class="col-lg-2">
<div class="form-group">
<button type="submit" class="btn btn-primary">Add Skill</button>
<button type="button" class="btn btn-danger">Cancel</button>
</div>
</div>
</div>
</form>
</div>
#endsection
Change your anchor tag's href value to named route like
<a class="btn icon-btn btn-success" href="{{route('addSkills')}}">
<span class="glyphicon btn-glyphicon glyphicon-plus img-circle text-success"></span>
Add
</a>
and your controller
// SHOW ADD NEW SKILL FORM
public function addIndex() {
if (Auth::check()) {
return view('skills.add'); //view path is skills->add.blade.php
} else {
return redirect('/home');
}
}
//SAVE ADD NEW SKILL FORM DATA
public function add(Request $request) {
dd($request->name); //Check if value is getting
/*Sets validation rules for Skill object*/
$skillRules = [
'name' => 'required|max:25|regex:/[1-9a-zA-Z ]\w*/',
];
$validator = Validator::validate($request->all(), $skillRules);
if (Skills::where('name', '=', $request->name)->count() > 0) {
return redirect('/skills')->with('message', "EXISTS");
}
if ($validator == null) {
$newSkill = new Skills;
$newSkill->name = strtolower($request->name);
$newSkill->save();
return redirect('/skills')->with('message', "CREATED");
}
}
also add use App\RampUp\Skills; on top of controller
Sorry but there is no POST-route for <form method="POST" action="/skills/add">
maybe the POST is still executed.
On the other hand most Route errors could be resolved by clearing the cache (if present), rearranging the routes or put a group in between like
Route::group(['prefix' => 'skills'], function() {
Route::put('/add', 'SkillsController#add');
Route::get('/add', 'SkillsController#addIndex');
}
the function sumqtyin() inside codeigniter model
function sumqtyin(){
$kdbahan = $_POST['kode_bahan_baku']; //i wanna echo a value from the input text kode_bahan_baku but it always says error "undefined index kode_bahan_baku"
$datenow = date("Y-m-d");
return $this->db->query("
SELECT IFNULL(SUM(qty_in),0) AS qty_in
FROM trans_stock_movement
WHERE tanggal_movement='$datenow'
AND status_aktif='YES' AND kode_bahan_baku = '$kdbahan' ");
}
my view input text for kode_bahan_baku inside a post form
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Bahan Baku</label>
<div class="col-sm-2">
<div class="input-group">
<input type="text" class="form-control" id="nama_bahan_baku" name="nama_bahan_baku" placeholder="Bahan Baku" value="" style="width:150px" required="required">
<input type="text" id="kode_bahan_baku" name="kode_bahan_baku" value="" class="form-control">
<div class="input-group-btn">
<button type="button" class="btn btn-info btn-flat" data-toggle="modal" id="btnMenu" data-target="#menuModal">
<i class="fa fa-fw fa-search"></i>
</button>
</div>
</div>
</div>
</div>
please help :(
Your controller is the go-between of your view and your model, as such, you should be grabbing the data out using $this->input->post('kode_bahan_baku') in the controller and passing those details through to the model:
Controller:
$kdbahan = $this->input->post('kode_bahan_baku');
$dbResultObj = $this->yourLoadedModel->sumqtyin($kdbahan);
then use the object $dbResultObj like: $dbResultObj->qty_in to get out the result
Model:
function sumqtyin($kdbahan){
$datenow = date("Y-m-d");
return $this->db->select('SUM(IFNULL(qty_in,0)) AS qty_in')
->where('tanggal_movement', $datenow)
->where('status_aktif', 'YES')
->where('kode_bahan_baku', $kdbahan)
->get('trans_stock_movement')
->result();
}
Please Follow the MVC structure , Before you make any manipulation on database you just have to follow the MVC method .
VIEW => CONTROLLER => MODEL => CONTROLLER => VIEW
like #Brian Ramsey did . check out the guidelines of codeigniter
https://www.codeigniter.com/user_guide/
I am trying to load a page on click,
I created an email verification link which open the page and updates my Database all it works fine, but the page contains a button which i want to be clicked by user and open first page which have login window.
My verify.html:
<div class="container container-table">
<div class="row vertical-center-row">
<div class="text-center col-md-4 col-md-offset-4" style="background:blue">
<h3> Thankyou For Veryfing</h3>
<p>You Are Verified</p>
<form action="login.php/verify_to_login" method="post">
<button type="submit" class="btn btn-success" >Sign Up>Go To Login</button>
</form>
</div>
</div>
</div>
My Controller function which loads the verify page like this : http://localhost/index.php/login/verify/1567af19e10ce4
public function verify($VerificationCode){
$this->load->model('My_model');
$this->load->helper('url');
$this->My_model->verifymail($VerificationCode);
/* Send to the verification page */
$this->load->view("verify.html");
}
This is my verify_to_login() function which i want when i click on button it should open header.html, This should work like http://localhost/index.php/login/verify_to_login
but instead this it is showing http://localhost/index.php/login/verify/login.php/verify_to_login when i click on the button
public function verify_to_login(){
$this->load->view("header.html");
}
I am unable to understand why is this happening.?? :(
On html
<form method="" action="<?php echo base_url(); ?>controller_name/function_name">
<button id="submit-buttons" type="submit" >Submit 1</button>
</form>
And on Controller
function functionname(){
$this->load->view('some_view')
}
So basically your controller is fine, so just fix the button by putting the controller name and then the function name and all should work hopefully.
try
this
<button type="submit" class="btn btn-success" onclick="window.location.replace('YOUR_LINK_HERE');">Sign Up>Go To Login</button>
OR
<button type="submit" class="btn btn-success" onclick="window.location.href('YOUR_LINK_HERE');">Sign Up>Go To Login</button>
I have a list of users on my web page echoed using a PHP loop. The names are links look like this.
{$user['username']}
So, when I click on each user, I am taken to user.php page.
user.php displays each user's information based on the query parameter $user['id'] in the URL. Using $_GET['id'] method.
So, that's the current situation I have.
What I now want to do is, display user's information in a Bootstrap Model
So, how could I get that query parameter into the Bootstrap model. And do the same PHP processes to get the same functionality ?
If you have google this you can get number of results related to it.
Here is code seems to be referred from one of this question passing-data-to-a-bootstrap-modal
HTML
<a data-toggle="modal" data-id="ISBN564541" title="Add this item" class="open-AddBookDialog btn btn-primary" href="#addBookDialog">test</a>
<div class="modal hide" id="addBookDialog">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h3>Modal header</h3>
</div>
<div class="modal-body">
<p>some content</p>
<input type="text" name="bookId" id="bookId" value=""/>
</div>
</div>
Javascript
$(document).ready(function () {
$(".open-AddBookDialog").click(function () {
$('#bookId').val($(this).data('id'));
$('#addBookDialog').modal('show');
});
});