I have a code that fetches all the customers from my database. i fetch all the customers in my drop down. Now i want to select a customer and when i submit i want to create a key for the selected customer.
This is my Controller code:
public function index()
{
$id = $this->data['org'] = $this->key_m->get_organisation();
$this->data['key'] = $this->key_m->genrate_license();
$this->data['subview'] = 'admin/key/index';
$this->load->view('admin/_layout_main',$this->data);
}
The View:
<div class="col-sm-3">
<div class="col-md-12">
<div class="form-group">
<h3><label for="sel1"> Organization Name:</label></h3>
<select required name="org-list" id="org-list" class="form-control">
<option value="">Select</option>
<?php foreach($org as $value) { ?>
<option id="emp" value="<?php echo $value['org_name'];?>"><?php echo $value['org_name'];?></option>
<?php } ?>
</select>
<?php echo "<br>"; ?>
</div>
</div>
</div>
So i want to save the the id for the customer and the key generated in database. when i select any customer[company here] in the dropdown
Write your option as below:-
<option id="emp" value="<?php echo $value['org_name'].'|'.md5(uniqid(rand(), true));?>"><?php echo $value['org_name'];?></option>
I have used md5(uniqid(rand(), true)) for generating unique key.
And in your controller,
$test = explode('|', $_POST['org-list']);
echo $test[0] gives you value and $test[1] gives you key.
It will generate always different unique string depend on micro-time and random number.
<?php
// Here microtime will be unique everytime
echo substr(md5(microtime()*rand(0,9999)),0,20); // 20 is length of key
?>
Related
I have one dropdown with a foreach loop which will pass to model using post method.
<div class="form-group" ">
<select class="form-control" id="rel" name="rl[][rel]" >
<option></option>
<?php
foreach ($relationship as $row) {
?>
<option value="<?php echo $row->relID; ?>"><?php echo $row->relCat; ?></option>
<?php
}
?>
</select>
</div>
and in the model it is getting the proper values using post method. As
$rel = $_POST['rel'];
Here the problem is when the user select one option ,I want to get two values like
"<?php echo $row->relID; ?>" and "<?php echo $row->relCatID; ?>"
like
$rel = $_POST['rel']; //this for the $row ->relID
$relcat = $_POST['relcat'];//this for the $row ->relCatID
I want to get both from one selection without adding any visble dropdown or element..
Try bellow code with ajax call
In Javascript code get value
var name = $('#rel option:selected').attr('data-cat_id');
var id = $('#rel').val();
<div class="form-group" >
<select class="form-control" id="rel" name="rl[][rel]" >
<option></option>
<?php foreach ($relationship as $row) { ?>
<option value="<?php echo $row->relID; ?>" data-cat_id="<?php echo $row->relCatID; ?>"><?php echo $row->relCat; ?></option>
<?php } ?>
</select>
</div>
I populate drop down list from mysql database and it shouldn't containt repeated values.
I try to use distinct keyword but failed to use. What is the logic I should use?
Example:
I have two bsc and two Bachelor in Mechanical in dropdown option and I want to display one bsc and one dropdown and similar for other course.
My codeigniter query is not working properly. The dropdown option is selected dynamically. How to solve it?
This is my controller
$courserecord = $this->front->get_data_wheree('tbl_course_offered.course_offrd_name');
$data['collg_id'] = json_decode(json_encode($courserecord), True);
if (empty($data['collg_id'])) {
$data['collg_id'] = json_decode(json_encode($courserecord), True);
} else {
$courserecord = $this->front->get_data_wheree('tbl_course_offered.course_offrd_name');
$data['collg_id'] = json_decode(json_encode($courserecord), True);
}
This is my view file
<div class="col-md-2">
<div class="form-group">
<div class="col-md-12">
<select name="course_offrd_name" id="course_offrd_name" onchange='' class="form-control ui fluid dropdown">
<option value="">Select Course</option>
<?php foreach($coursedata as $val)
{
?>
<option value="<?php echo $val->course_id?>" <?php if(isset($course_offrd_name)) { if($course_offrd_name==$val->course_id) { ?>selected
<?php } }?> >
<?php echo $val->course_id;?>
</option>
<?php }?>
</select>
</div>
</div>
</div>
This is my model file
function get_data_wheree($table,$where)
{
return $this->db->distinct($table,$where)->result();
}
This is table file or structure
This is my frond end dropdown option
You can use group by on cat_id to get unique value. As per you attachment cat_id is also repeated and same for respective course_id.
function get_data_wheree($table,$where)
{
$this->db->group_by("cat_id");
return $this->db->get($table,$where)->result();
}
I have a function in my controller which is trying to fetch the value from view template and print the same through post method but something else is printing on there.
public function editProfile(){
$profile = $this->input->post('profile');
echo ("PROFILE:" . $profile);
}
This is my view template which is taking profile id from the application_info array stored in the database and matching it with the other profiles id so that the value of the particular id can be fetched and display out to the user-side using form.
$profile_id = $application_info[0]->profile_id;
<form role="form" action="<?php echo base_url() ?>Job_profiles/editProfile" method="post" id="editApplication" role="form">
<div class="form-group">
<label for="profile">Profile</label>
<select class="form-control" id="profile" name="profile">
<option value="0">Select profile</option>
<?php
if(!empty($profiles))
{
foreach ($profiles as $p)
{
?>
<option value="<?php echo $p->profile; ?>" <?php if($p->profile_id == $profile_id) {echo "selected=selected";} ?>><?php echo $p->profile ?></option>
<?php
}
}
?>
</select>
</div>
While printing the value of the variable inside the view template, everything is going fine. But when displaying the value through the controller, it prints nothing.
Thanks.
Im having issues with data not being entered into the db. I have a form with 5 inputs and a select The drop down select is a dynamic list fro the db. When the form is submitted nothing is entered in the db for the campaign_owner columns. I have the values="" which I think is the problem, but I also tried
<option value="<?php echo $user['username']; ?>"><?php echo $user['username']; ?></option>
yet that doesn't help.The dropdown shows the data I want and allows you to select it on the page it just doesn't submit it and I dont know why.
HTML:
<div class="col-sm-5 col-sm-push-1 form-group required">
<label class="control-label" ><?php echo $entry_owner; ?></label>
<select name="user-list" id="user-list">
<?php foreach ($users as $user) { ?>
<option value="<?php echo $user['username']; ?>"><?php echo $user['username']; ?></option>
<?php } ?>
</select>
</div>
HTML forms work with the input/select/textarea etc. names. Your select is named "user-list" which means that when submitted it will be under the key user-list. You'll need to map that to the campaign_owner column.
My update statement isn't working correctly, I am attempting to pull the data from the database, populate a dropdown with either "Y" or "N" inside it, on submit the values are entered into the database and the page refreshes.
So far I have my list of items, each with correctly populated dropdown, it is now my submit that is failing to work.
<?php
$updatedFeatProd = $_POST['featuredProduct'];
var_dump($updatedFeatProd);
if ($_POST) {
foreach ($_POST['featuredProduct'] as $key => $val) {
$query = 'UPDATE tblProducts SET featuredProduct = ' . $updatedFeatProd . '
WHERE fldID = ' . $val;
$sql = dbQuery($query);
}
}
$sql = dbQuery('SELECT fldId, fldName, featuredProduct FROM tblProducts');
?>
<form method="post" action="#" name="featuredProd">
<table>
<tr><td><p>Product Name</p></td><td><p>Is a featured product?</p></td></tr>
<?php
$products = dbFetchAll($sql);
foreach ($products as $product) {
//var_dump($product['fldName']);
?>
<tr>
<td>
<p><?php echo $product['fldName']; ?></p>
</td>
<td>
<select name="featuredDropdown">;
<?php
if ($product['featuredProduct'] == 'Y') {
?>
<option value="<?php $product['fldId'] ?>"><?php echo $product['featuredProduct'] ?></option>
<option value="<?php $product['fldId'] ?>">N</option>
<?php
} else {
?>
<option value="<?php $product['fldId'] ?>"><?php echo $product['featuredProduct'] ?></option>
<option value="<?php $product['fldId'] ?>">Y</option>
<?php
}
?>
</select>
</td>
</tr>
<?php
}
?>
The presentaton here does not make much sence. You have a dropdown with the ProductName in one slot and a 'N' in another.
Once the user has selected 'N' for a product they have no idea what they have said NO to, as they can no longer see the product name that they have selected NO for.
It would make more sence to provide a <label> containing the product name and a YES/NO dropdown beside it for them to select from.
However the reason your update code is not working is that you have called the dropdown featuredDropdown
<Select name="featuredDropdown">
and you are trying to process a field called featuredProduct in the update code
foreach ($_POST['featuredProduct'] as $key => $val) {
Your next problem will probably be that you are oututting more than one <Select name="featuredDropdown"> so you need to make that into an array as well like this:
<Select name="featuredDropdown[]">
Then you will have an array of featuredDropdown in the $_POST array. $_POST['featuredDropdown'][]