How to check duplication of two fields in codeigniter - php

How to check the duplication of following fields in controller.
this is my view code
<div class="form-group col-lg-12">
<label class="control-label col-lg-2">Name</label>
<div class="col-lg-4">
<select id ="choose" class="form-control" name="name">
<option value="">Select One</option>
<option value="create_user">New user creation</option>
<option value="register_user">New user registration</option>
<option value="forgot_password">Forgot Password</option>
</select>
</div>
<div class="col-lg-6" id="email_tmpl"></div>
</div>
<div class="form-group col-lg-12">
<label class="control-label col-lg-2">Notification to</label>
<div class="col-lg-10">
<select id ="notification" class="form-control" name="notification">
<option value="">Select One</option>
<option value="User">User</option>
<option value="Site Admin">Site Admin</option>
</select>
</div>
</div>
if I selected the option "New user creation" in 1st combo box, then I selected "User" option in next combo box.
I want to show an error message when again select the "User" option under "New user creation". And only "Site Admin" can select under "New user creation" option.
This is my controller code.
public function save_email_tmpl()
{
$this->load->model("Email_templates");
$user_data = $this->session->userdata("logged_user");
$post_data = $this->input->post();
if($post_data != FALSE )
{
$check_name = $this->Email_templates->get_all_records
("",array("name" => $post_data["name"]));
if(count($check_name)< 2)
{
$data = array("name" => $post_data["name"],
"notification" => $post_data["notification"],
"cc" => $post_data["cc"],
"subject" => $post_data["subject"],
"email_template" => $post_data["email"]);
$this->Email_templates->insert_record($data,$user_data);
echo json_encode(array(
"status" => "success",
"msg" => lang("email_tmpl_added")));
}
else
{
echo json_encode(array(
"status" => "failed",
"msg" => lang("invalid_data")));
}
}
else
{
echo json_encode(array(
"status" => "failed",
"msg" => lang("invalid_data")));
}
}
I want to check the condition in this code.

Related

Price range filter using CodeIgniter4

I am trying to create a search with multiple filters, one of the filters is a budget or price filter and I am using dropdown instead of creating a price range bar.
In other words, my budget filters are:-
0-2500000
250000-500000
500000-10000000
10000000-25000000
25000000-50000000
50000000 and above
Here is my code and I need help modifying it. The function in Controller and SQL Query in the model only takes a single budget (price) value and displays the result, instead of taking the budget (price) range and showing the result which comes under that budget (price) range.
What I want to achieve is that when the user selects the budget (price) range in the search form, it should display properties that fall under the selected budget range or price range.
HTML Form
<div class="search__area-inner">
<?= form_open('/home/search_residential'); ?>
<div class="row">
<div class="col-12"><?= csrf_field(); ?></div>
<div class="col-6 col-lg-4 col-md-4">
<div class="form-group">
<select name="budget[]" class="wide select_option">
<option data-display="Select Budget">Select Budget</option>
<option value="0-2500000">less than 25 lacs</option>
<option value="250000-500000">25 to 50 lacs</option>
<option value="500000-10000000">50 lacs to 1 cr</option>
<option value="10000000-25000000">1 cr to 2.5 cr</option>
<option value="25000000-50000000">2.5 cr to 5 cr</option>
<option value="50000000">5 cr +</option>
</select>
</div>
</div>
<div class="col-6 col-lg-4 col-md-4">
<div class="form-group">
<select name="type" class="wide select_option">
<option data-display="Select Property Type">Select Property Type</option>
<option value="Apartments">Apartments</option>
<option value="Bungalow">Bungalow</option>
<option value="Row House">Row House</option>
<option value="Villas">Villas</option>
</select>
</div>
</div>
<div class="col-6 col-lg-4 col-md-4">
<div class="form-group">
<input type="text" class="form-control" id="inputLocation" name="city" value="" placeholder="Enter City" required>
</div>
</div>
<br>
<div class="mx-auto">
<div class="form-group">
<button class="btn btn-primary text-uppercase btn-block"> search
<i class="fa fa-search ml-1"></i>
</button>
</div>
</div>
</div>
<?= form_close(); ?>
</div>
Controller code
public function search_residential() {
$data['getfooter'] = $this->homeModel->getFooterInfo();
$data['getad'] = $this->homeModel->getCarousel();
$data['pagelist'] = $this->pageModel->getPages();
$data['cities'] = $this->db->table('tblcity')->select('*')->get()->getResult();
$params = array(
'budget' => $this->request->getVar('budget'),
'type' => $this->request->getVar('type'),
'city' => $this->request->getVar('city'),
);
$data['search'] = $this->homeModel->getSearch($params);
return view('searchresidential', $data);
}
**Model code**
//Searching residential data
public function getSearch($params) {
$budget = $params['budget'];
$type = $params['type'];
$city = $params['city'];
$builder= $this->db->table('tblresidential');
$builder->select('*');
$builder->where('1 = 1');
if ($budget != '') {
$builder->where('budget', $budget);
}
if ($type != '') {
$builder->where('type', $type);
}
if ($city != '') {
$builder->where('city', $city);
}
return $builder->get()->getResult();
}
Instead of:❌
// ...
$builder->where('budget', $budget);
// ...
Use this:✅
// ...
foreach ($budget as $range) {
$builder->orWhere((function (string $range) {
$limits = array_map(
fn($limit) => floatval($limit),
explode("-", $range)
);
return match (count($limits)) {
1 => ['budget >=' => $limits[0]],
2 => ['budget >=' => $limits[0], 'budget <=' => $limits[1]],
default => ['1 =' => 1]
};
})($range));
}
// ...
Resource: $builder->where() : Associative array method

Selected value get from DB into dropdown select box

I have an Employee Form Edit.
My problem is, my function for the edit is working fine but I can't get the value of my employee type.
I have 4 employee type:
Staff
Supervisor
Manager
Super User
Every time I go to edit form, the employee type on drop down is always on staff (I want to display, if the employee is "Manager" then the drop box show Manager).
This is the code for editroledetails.blade.php
<div class="form-group">
<label for="editCustomerType">Customer Type</label><br/>
<select name="editCustomerType">
<option value="Staff">Staff</option>
<option value="Supervisor">Supervisor</option>
<option value="Manager">Manager</option>
<option value="Super User">Super User</option>
</select>
</div>
Controller function
public function editroledetails(Request $request)
{
$user = \Auth::user();
$userphone = 0;
$reportTo = DB::select(DB::raw("select username from customer_type where customer_type = 'Supervisor' or customer_type ='Manager' "));
$select = DB::select(DB::raw("select customer_type from customer_type "));
$data = [
'editUsername' => $request->editUsername,
'editNik' => $request->editNik,
'editEmail' => $request->editEmail,
'editRegIdentities' => $request->editRegIdentities,
'editReportTo' => $request->editReportTo,
'editID' => $request->editID
];
return view('editroledetails', compact('user', 'userphone', 'data', 'reportTo', 'select'));
}
You can do something like this:
<div class="form-group">
<label for="editCustomerType">Customer Type</label><br/>
<select name="editCustomerType">
#if($user->customer_type == 'Staff')
<option value="Staff">Staff</option>
#endif
#if($user->customer_type == 'Supervisor')
<option value="Supervisor">Supervisor</option>
#endif
#if($user->customer_type == 'Manager')
<option value="Manager">Manager</option>
#endif
#if($user->customer_type == 'Super User')
<option value="Super User">Super User</option>
#endif
</select>
</div>
Use the selected attribute on the select options
<div class="form-group">
<label for="editCustomerType">Customer Type</label><br/>
<select name="editCustomerType">
<option value="Staff" #if($user->customer_type == 'Staff')selected#endIf>Staff</option>
<option value="Supervisor" #if($user->customer_type == 'Supervisor')selected#endIf>Supervisor</option>
<option value="Manager" #if($user->customer_type == 'Manager')selected#endIf>Manager</option>
<option value="Super User" #if($user->customer_type == 'Super User')selected#endIf>Super User</option>
</select>
</div>
You can use this to show the selected item in select list
<?php
$customerTypes = [
'Staff' => 'Staff',
'Supervisor' => 'Supervisor',
'Manager' => 'Manager',
'Super User' => 'Super User',
];
?>
<div class="form-group">
<label for="editCustomerType">Customer Type</label><br/>
<select name="editCustomerType">
#foreach($customerTypes as $type)
<?php
$selected = "";
#if($user->customer_type == $type) {
$selected = "selected";
}
?>
<option value="{{ $type }}" {{ $selected }} >{{ $type }}</option>
#endforeach
</select>
</div>

How to fetch data using foreach in one form codeigniter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to know the most relevant method to do this in Codeigniter. I was actually using this to populate <options> </options> in form
<div class="form-group">
<label for="int">Clients Id <?php echo form_error('clients_id') ?></label>
<select class="form-control select2 select2-hidden-accessible" se="width: 100%;">
<center><option selected="selected">--- Select ---</option></center>
<?php
foreach ($invoices as $invoices) {
?>
<option class="form-control select2" value=" <?php echo $invoices->clients_id; ?>"><?php echo $invoices->first_name; ?>
</option>
<?php
}
?></select>
</div>
<div class="form-group">
<label for="int">Suppliers Id <?php echo form_error('suppliers_id') ?></label>
<select class="form-control select2 select2-hidden-accessible" se="width: 100%;">
<center><option selected="selected">--- Select ---</option></center>
<?php
foreach ($invoices as $invoices) {
echo "<pre>"; print_r($invoices);die;
?>
<option class="form-control select2" value=" <?php echo $invoices->service_id; ?>"><?php echo $invoices->service_name; ?>
</option>
<?php
}
?></select>
</div>
This function is in my model. Is there any best method to execute.
function join() {
$this->db->select('*');
$this->db->from('invoices');
$this->db->join('clients', 'invoices.id = clients.id');
$this->db->join('services', 'invoices.id = services.id');
$query = $this->db->get();
return $query->result();
}
Is it the right method. To call the Invoice data. Actually, entire code works properly but when I call foreach() It, Shows error. Thank you in Advance.
public function create()
{
$data = array(
'button' => 'Create',
'action' => site_url('invoices/create_action'),
'id' => set_value('id'),
'clients_id' => set_value('clients_id'),
'services_id' => set_value('services_id'),
'suppliers_id' => set_value('suppliers_id'),
'contractors_id' => set_value('contractors_id'),
'charges_id' => set_value('charges_id'),
'particulars' => set_value('particulars'),
'details' => set_value('details'),
'price' => set_value('price'),
'date' => set_value('date'),
'due' => set_value('due'),
'paid' => set_value('paid'),
);
$this->load->view('invoices_form', $data);
}
First of all check your query using echo $this->db->last_query(); put this code in controller where you write $data['invoices'] and then check its working or not then use below code in your view part.
<div class="form-group">
<label for="int">Clients Id <?php echo form_error('clients_id') ?></label>
<select class="form-control select2 select2-hidden-accessible" style="width: 100%;">
<center><option selected="selected">--- Select ---</option></center>
<?php
foreach ($invoices as $row)
{
echo '<option class="form-control select2" value="'.$row['client_id'].'">'.$row['first_name'].'</option>';
}
?>
</select>
</div>
<div class="form-group">
<label for="int">Suppliers Id <?php echo form_error('suppliers_id') ?></label>
<select class="form-control select2 select2-hidden-accessible" se="width: 100%;">
<center><option selected="selected">--- Select ---</option></center>
<?php
foreach ($invoices as $row)
{
echo '<option class="form-control select2" value="'.$row['service_id'].'">'.$row['service_name'].'</option>'
}
?>
</select>
</div>

How to auto set the textbox based on dropdown selection?

How to auto set the textbox based on dropdown selection? I have a dropdown for position and textbox for department. What I would like to do is when "Guidance Faculty" is selected from dropdown, the textbox will automatically set to "Guidance" and it will become readonly and same in when "OSA Faculty" is selected from dropdown, the textbox will automatically set to "OSA" and it will become readonly. While if "Department Chair" and "Professor" is selected nothing will happen to textbox or it will set to "".
<div class="form-group">
<label for="position">Position</label>
<?php
echo form_open('signup');
$options = array(
'' => 'Select Position',
'Department Chair' => 'Department Chair',
'Professor' => 'Professor',
'Guidance Faculty' => 'Guidance Faculty',
'OSA Faculty' => 'OSA Faculty',
);
echo "<div class='drop_pos'>";
echo form_dropdown('position', $options, 'class="btn dropdown-toggle"', 'data-toggle="dropdown-menu"');
?>
<div class="text-danger"><?php echo form_error('position');?></div>
</div>
</div>
<div class="form-group">
<label for="department">Department</label>
<input class="form-control" name="department" placeholder="Department" type="text" value="<?php echo set_value('Department');?>"/>
<span class="text-danger"><?php echo form_error('department');?></span>
</div>
Try this:
<div class="form-group">
<label for="position">Position</label>
<?php
echo form_open('signup');
$options = array(
'' => 'Select Position',
'Department Chair' => 'Department Chair',
'Professor' => 'Professor',
'Guidance Faculty' => 'Guidance Faculty',
'OSA Faculty' => 'OSA Faculty',
);
echo "<div class='drop_pos'>";
echo form_dropdown('position', $options, 'class="btn dropdown-toggle"', 'data-toggle="dropdown-menu" onChange="javascript:document.getElementsByName(\'department\').value=this.value;"');
?>
<div class="text-danger"><?php echo form_error('position');?></div>
</div>
</div>
<div class="form-group">
<label for="department">Department</label>
<input class="form-control" name="department" placeholder="Department" type="text" value="<?php echo set_value('Department');?>"/>
<span class="text-danger"><?php echo form_error('department');?></span>
</div>
Make use of JS like below.
First set dropdown box like this...
echo form_dropdown('position', $options, array('id'=>'pos','class'=>'btn dropdown-toggle','data-toggle'=>'dropdown-menu','onchange'=>'changefuc();'));
Then add JS
<script type="text/javascript">
function changefuc(){
var pos = document.querySelector('#pos');
var dept = document.querySelector('[name="department"]');
pos.addEventListener('change',function(){
dept.value = this.value;
});
}
</script>
UPDATE
See demo
var pos = document.querySelector('#pos');
var dept = document.querySelector('[name="department"]');
pos.addEventListener('change',function(){
dept.value = this.value;
});
<select id="pos">
<option value="" selected >Select Position</option>
<option value="Department Chair">Department Chair</option>
<option value="Professor">Professor</option>
<option value="Guidance Faculty">Guidance Faculty</option>
<option value="OSA Faculty">OSA Faculty</option>
</select>
<input class="form-control" name="department" placeholder="Department" type="text"/>
in jquery something like this:
jQuery("select[name='position']").on("change", function() {
// value of selected option
var selectedPosition = $(this).val();
if (selectedPosition) {
// the inputbox
jQuery("input[name='department']").val(selectedPosition).attr('readonly', true);
} else {
jQuery("input[name='department']").val('').attr('readonly', false);
}
});

php array dropdown field not insert into database mysql

I am new in OOP PHP and I am still learning, I have a form where name of locations is populated through JQuery/HTML form using drop-down field with same name, my problem is when I try to insert in mysql database, I did not get the selected value,
<div id="NCR" class="drop-down-show-hide">
<div class="field">
<label for="region"> NCR </label>
<select name="region" id="ncr region">
<option value="">-- Choose --</option>
<option value="1"> Region 1 </option>
<option value="2"> Region 2 </option>
<option value="3"> Region 3 </option>
</select>
</div>
</div>
<div id="CAR" class="drop-down-show-hide">
<div class="field">
<label for="field"> CAR </label>
<select name="region" id="car region">
<option value="">-- Choose --</option>
<option value="4"> Region 4 </option>
<option value="5"> Region 5 </option>
<option value="6"> Region 6 </option>
</select>
</div>
</div>
<div id="region3" class="drop-down-show-hide">
<div class="field">
<label for="region"> CAMANABA </label>
<select name="region" id="camanaba region">
<option value="">-- Choose --</option>
<option value="7"> Region 7 </option>
<option value="8"> Region 8 </option>
<option value="9"> Region 9 </option>
</select>
</div>
</div>/HTML
PHP
if(Token::check(Input::get('token'))){
$validate = new Validate();
$validation = $validate->check($_POST, array(
'apz_account' => array(
'required' => true
),
'api_account' => array(
'required' => true
),
'wupos_tid' => array(
'required' => true
),
));
if($validation->passed()){
// check APZ account
$agent = DB::getInstance()->get('agent', array('apz_account', '=', Input::get('apz_account')));
if(!$agent->count()){
// check API account
$agent = DB::getInstance()->get('agent', array('api_account', '=', Input::get('api_account')));
if(!$agent->count()){
$agent = new Agent();
try {
$agent->createAgentAccount(array(
'apz_account' => Input::get('apz_account'),
'api_account' => Input::get('api_account'),
'wupos_tid' => Input::get('wupos_tid'),
'region' => Input::get('region'),
'registered' => date('Y-m-d H:i:s'),
));
// print_r($agent);
// Session::flash('success', 'You have registered successfully.');
// Redirect::to('../../index.php');
} catch(Exception $e){
die($e->getMessage());
}
} else {
echo Input::get('api_account'), ' account is already exists';
}
} else {
echo Input::get('apz_account'), ' account is already exists';
}
} else {
foreach ($validation->errors() as $error) {
echo $error, '<br>';
}
}
}
Please advice. Thank you
You need unique names for each form element, with php you can use region[]. this will give you an array in your server script.
Then when you get the value do current(array_filter(Input::get('region')));

Categories