Currently I have a refno. column in my database with the format LP00001. Now I have a add page that inserts all data that the user inserts in the input field and all this data gets sent to a table in my database, but my refno column stays null which I want to have an auto incrementing value where the last 5 digits will be +1 everytime the user submits the form.
I do not want to have it be shown before the submit button as 2 users can be on that page at the same time which means they both will get the same id which means it has to generate it only at the time of submit. Currently this is my code:
Controller class:
if ($this->form_validation->run() == FALSE)
{
$main['page'] = 'crm/listings/add';
$this->load->view('crm/index', $main);
}else {
$maindata=array('clients_id'=>$this->session->userdata('clientsessid'),
'property_for'=>$this->security->xss_clean($this->input->post('property_for')),
'property_type'=>$this->security->xss_clean($this->input->post('property_type')));
$insertid=$this->listings_model->insert_listing($maindata);
if($insertid){
$this->session->set_flashdata('message', '<div>Successfully</div>');
redirect('listings/sales');
Model Class:
function insert_listing($maindata){
$this->db->insert("crm_listings",$maindata);
$prime=$this->db->insert_id();
return $prime;
}
So I assume I'll need to do this in my model class function insert_listing since that is called when I press submit button. Here another thing will be that the model will have to check what was the last entry in the database that has to be incremented for which I'm assuming I'll have to use echo str_replace("LP","","LP00001");
I guess you just have to prepend the initials LP to the returned autoIncremented id.
function insert_listing($maindata){
// ...
$this->db->set("refno", "LP". sprintf('%05d', $prime));
$this->db->where('id', $prime);
$this->db->update("crm_listings");
return $prime;
}
Related
Firstly I'm new to Laravel and this is my first post. Please forgive me if I'm doing things wrong.
I have an HTML table, which is a Livewire component. In this HTML table, each row has a dropdown box and some text fields. This is for a timesheet application. The dropdown is the job and there are 7 text boxes, for each day of the week for the hours to be entered.
I have tried to include an Add button, I want this to add a new row at the end of the HTML table.
The issue I'm getting is when I click on Add, it will append the row, however, my dropdown is not shown, UNTIL I refresh the page, then it appears as it should.
The way I currently have my program is that when a user clicks on add, it will create a new record in my Entries table, with the timesheet ID included, and the rest of the values as null. Is there another way of temporarily storing the lines the user creates, then commit to the database? Is there a way to refresh my table so that it displays the new line correctly?
Please let me know if you need further details or a better explanation of my issue.
My code is as follows:
Controllers\Livewire\ShowTimesheet.php
...
public function mount($entries)
{
$this->entries = $entries;
}
public function render()
{
return view(‘livewire.show-timesheet');
}
public function add($i)
{
$entries_new = new Entries;
$entries_new->timesheet_id = $i;
$entries_new->save();
$new = array(
'id'=>$entries_new->id,
'timesheet_id'=>$i,
'job_id'=>null,
'worktype_id'=>null,
'hours_sun'=>null,
'hours_mon'=>null,
'hours_tue'=>null,
'hours_wed'=>null,
'hours_thu'=>null,
'hours_fri'=>null,
'hours_sat'=>null,
);
$this->entries[] = $new;
}
...
Layouts\livewire\show-timesheet.blade
...
#foreach($timesheet as $sheet)
...
<div wire:click="add({{$sheet['id']}})" class='cursor-pointer'>
...
#endforeach
...
So I'm creating an application for clients to fill in an order to calculate the price. The client has to fill in a form, the form will be posted to a table in MySQL. When the order is complete the client gets an overview of his order with the calculated price. I'm having trouble with storing a variable (from tblPanel_Prices) which is related with a variable in tblPaneltypes. So after filling in the form data from column tblPaneltypes and tblPanel_Prices should be stored together in another table. Both tables (types and prices) are related.
The client has to select a type of panel. These types are stored in the table (tblPaneltypes). Every panel has a certain price-categorie (field price_id) which is needed to calculate the price. The prices of the paneltypes are stored in another table (tblPanel_Prices). There is a relation between the tblPaneltypes and tblPanel_Prices (both integer)
So the client fills in the form and selects a certain type of panel, this gets posted (controller) to the table with the function: this->input->post('paneltype').
After this I want to calculate the surface of the panel and make select the right price-category on the field price-id.
So in the form there is a type of panel choosen by the client. This already goes to my table, but I also want to pass the price_id tag in the form.
Then, I would like to get the price_id tag from the table tblPaneltypes to link this with the price_id tag in tblPanel_Prices. The value of the price_id isn't important for the client, so no need to select or show this value. I just need it to calculate the price of the panel (which depends on the type of panel filled in by the client)
Thanks in advance!
Store function in controller
public function store()
{
$this->load->model("orders/panels/Panels");
$this->form_validation->set_rules('amount', 'aantal', 'numeric|required|max_length[2]');
$this->form_validation->set_rules('dwidth', 'paneelbreedte', 'numeric|callback_check_width');
$this->form_validation->set_rules('dheight', 'paneelhoogte', 'numeric|callback_check_height');
$this->form_validation->set_rules('paneltype', 'paneeltype', 'required');
$this->form_validation->set_rules('ral_code', 'ral kleur', 'callback_check_double');
if ($this->auth()) {
if ($this->form_validation->run() == FALSE) {
$this->read_panel();
} else {
$panel = $this->Panels->create__entry(
$this->input->post('order_id'),
$this->input->post('amount'),
$this->input->post('dwidth'),
$this->input->post('dheight'),
$this->input->post('paneltype'),
$this->input->post('panelcategory'),
$this->input->post('color'),
$this->input->post('othercolor'),
$this->input->post('tubes'),
$this->input->post('colorswatch'),
$this->input->post('structurepaint'),
$this->input->post('uv'),
$this->input->post('window'),
$this->input->post('paintwindow'),
$this->input->post('windowsetup'),
$this->input->post('glastype'),
$this->input->post('door')
);
redirect("rail/read_rail?order=" . $this->input->post('order_id'));
}
} else {
login();
}
create__entry in model: at this moment I'm filling in the panelcategory manually in the model in. In the future it should filled in automaticly.
function create__entry($order_id, $amount, $dwidth,
$dheight, $paneltype, $panelcategory, $color,
$othercolor, $tubes,
$colorswatch, $structurepaint,
$uv, $windowtype, $paintwindow,
$windowsetup, $glastype, $door)
{
$this->order_id = $order_id;
$this->amount = $amount;
$this->dwidth = $dwidth;
$this->dheight = $dheight;
$this->paneltype = $paneltype;
$this->panelcategory = $panelcategory;
$this->color = $color;
$this->othercolor = $othercolor;
$this->tubes = $tubes;
$this->colorswatch = $colorswatch;
$this->structurepaint = $structurepaint;
$this->uv = $uv;
$this->windowtype = $windowtype;
$this->paintwindow = $paintwindow;
$this->windowsetup = $windowsetup;
$this->glastype = $glastype;
$this->door = $door;
$this->db->insert(self::TABLE_NAME, $this);
return $this->read__entry($this->db->insert_id());
}
I create one quiz app. In which students attend the question and click on the next button next question appears. now the issue is that the student is on the second question and the URL like "takeQuizStudent/149?page=2" and he manually changes URL to "takeQuizStudent/149?page=1". so it should not happen. Once clicks on the next question he can't go back to the previous question.
You should check request page number from session in your controller,for example
public function show(Request $request, $id)
{
$value = $request->session()->get('pageNumber'); //2
//$id = 1
if($id < $value){
return response("invalid request",504);
}
//else update pageNumber value and do continue
// Via a request instance...
$request->session()->put('pageNumber', '3');
// Via the global helper...
session(['pageNumber' => '3']);
.....
}
Determining If An Item Exists In The Session
To determine if an item is present in the session, you may use the has method. The has method returns true if the item is present and is not null:
if ($request->session()->has('pageNumber')) {
//
}
To determine if an item is present in the session, even if its value is null, you may use the exists method. The exists method returns true if the item is present:
if ($request->session()->exists('pageNumber')) {
//
}
Currently, I have controller & model for form edit. If user submit different value, this will modify the value in database and if there is an affected rows, will set flash message as "success" and if same data is submitted by user without modify the value,then will set flash message as "nochange".
if($this->db->affected_rows() > 0 ) {
$this->session->set_flashdata('success','Database is updated');
redirect('registrar');
} else {
$this->session->set_flashdata('nochange','There is no changes in database');
redirect('registrar');
}
In Codeigniter, how can I check if only specific columns are affected?
For example, if my table in database have 12 columns, if user only change the value let say in column 10, then set flash message as "nochange".
If user changes values in other columns except column 10, set flash message as "success"
There is no in-built functionality in CodeIgniter or php to do this.
You would have to essentially get the entire entry before updating it. And then column by column compare the submitted value with the current value of the entry as per your required logic.
Example pseudo-code:
$q = $this->db->get_where('table', array('id' => $id))->row();
$change = false;
if ($this->input->post('col1') !== $q->col1) {
$change = true;
}
...
if ($this->input->post('col10') == $q->col10) {
$change = false;
}
$this->db->update('table', $data);
I ended up using a session array and storing data there so that I can reference it again later. I just added my post data from each form into this array and referenced it later in my else block. Thanks for the help!
I am using CodeIgniter for a school project. I have some experience with PHP but am relatively new to this framework. I am having trouble using two forms on one page.
I have one form that displays a dropdown of artists. After clicking the submit button for this form, it updates the second form (another dropdown) on the same page with the portfolios belonging to the artist selected in the first dropdown.
I am trying to echo the values from each form just for testing purposes right now, I will implement other features later. My issue is that after my second form is submitted, the post value for the first form is changed. If I echo the selected value of the first form before submitting the second form, it shows the value that was selected. If I echo the value of the first form after both forms have been submitted, it shows the first available value from that dropdown.
I need to be able to take the values from both of these forms and then use them later after both forms have been submitted. So I can't have the values changing right when I need to use them, obviously, any help would be appreciated. Thank you much.
Controller
public function formtest(){
//Making a call to the model to get an array of artists from the DB
$data = $this->depot_model->get_artists_list();
$this->form_validation->set_rules('artist', 'Artist');// Commenting this out for now, 'required');
$this->form_validation->set_rules('ports', 'Portfolios', 'required');
if ($this->form_validation->run() == FALSE)
{
//Building the artists dropdown form
$data['data'] = form_open('formtest', 'class="superform"')
. form_label('Artist<br/>', 'artist')
. form_dropdown('artist', $data)
. form_submit('mysubmit', 'Submit')
. form_close();
//Setting up a temp array of the selected artist's portfolios
$ports = $this->depot_model->get_portfolios(url_title($data[$this->input->post('artist')]));
//Culling out the names of the portfolios from my temp array
$newdata = array();
foreach($ports as $port){array_push($newdata, $port['name']);}
//Building the artist's portfolio dropdown
$newdata['data'] = form_open('formtest', 'class="superform"')
. form_label('Portfolios<br/>', 'ports')
. form_dropdown('ports', $newdata)
. form_submit('mysubmit', 'Submit')
. form_close();
//Send the information to my view
$this->load->view('formtest', $data);
$this->load->view('formtest', $newdata);
}
else
{
//This echos out the first available value from my dropdown rather than the one I selected.
echo $data[$this->input->post('artist')];
echo "success";
}
}
The forms are separate, so when the second gets submitted, there is in effect no value received from the first form, as it isn't included as a field in the second. So you can do that, include say a hidden field in the second form that has the value of the artist. eg:
$newdata['data'] = form_open(
'formtest',
'class="superform"',
array('artist' => $this->input->post('artist'))
);