I'm working on a category section for stories submitted to my website. I have a html select going on in my html and I have that submitting to the database using codeigniter functions. It is submitting to the database, but there seems to be a small problem...the select is only grabbing the last value that is selected. I need to have all the values that are selected entered into the database so I can pull them out at a later date.
This is the html I am using.
<select multiple class="multi" name="wGenre">
<option value="Action/Adventure">Action/Adventure</option> <option value="Angst">Angst</option>
<option value="Crime">Crime</option>
</select>
and then this is what I have in my model.
$this->title = $this->input->post('wTitle');
$this->genre = $this->input->post('wGenre');
$this->db->insert('story_tbl', $this);
Now, I believe the problem is with my database. I originally set up the field as an enum, but that won't work because it's either one or the other selection and I need to have multiples. So then I tried it as a text field, and it's just not grabbing everything. To be honest I'm at a loss and in need of some help. :)
Firstly you need to make sure the select tag is named the array manner:
<select multiple="multiple" class="multi" name="wGenre[]">
Then To get all values of the multiple select you could do the following to save the values in an array!
$this->title = $this->input->post('wTitle');
$select_vals = array();
foreach($this->input->post('wGenre') as $val){
$val.' - '; // Used as a delimiter for later use
array_push($select_vals, $val);
}
$this->genre = $select_vals;
$this->db->insert('story_tbl', $this);
I ended up solving this without a foreach loop.
First of all, I was writting my html select tag wrong. For it to select multiple values you have to add both [] at the end of the class and a muti tag...like this.
<select multiple="multiple" class="multi" name="wGenre[]">
and then in the php it has to be json encoded to be placed in the database correctly...like this...
$this->genre = json_encode($this->input->post('wGenre'));
Thank you for all your help! I hope this helps someone else in the future! :)
Related
Situation: When the page loads, it will automatically populate the dropdown box with 'employees'. (2,000+ records fetched as of now)
Then I have to select an employee over thousands of it from the records.
Everything works absolutely fine.
Question:
How am I going to increase its fetching speed if ever I have 10,000+ of data to be retrieved in the future?
Are there any better options for this?
I want to stick on searchable selects as possible. If not, please suggest a good one.
Here are my codes on CodeIgniter:
Controller:
public function employee()
{
$data["get_employee"] = $this->Home_model->get_query_array("SELECT employee_id, employee_name FROM Employee_List");
$this->load->view('header_view');
$this->load->view('employee_view', $data);
$this->load->view('footer_view');
}
Model:
public function get_query_array($query_statement)
{
$query = $this->db->query($query_statement);
return $query->result();
}
View:
<label for="employee">Employee</label>
<select class="selectpicker" data-show-subtext="true" data-live-search="true" name="employee" id="employee">
<option value=""></option>
<?php
foreach($get_employee as $row)
{
$id = $row->employee_id;
$name = $row->employee_name;
echo "<option value='".$id."'>".$name."</option>";
}
?>
</select>
Thanks a lot guys!
When you execute this query for large data definitely your page will going slow.
For this, i will prefer you go with ajax select PHP search dropdown. Here is a good example https://codeforgeek.com/2014/09/ajax-search-box-php-mysql/ Please try with it. This will be very usefull for you. In this section , You will find your page is fast and its looking good as a feature in form or where you use this. Thanks
You can create dropdown of alphabets from A to Z then click hover of any character using ajax load the employees matching that character.In this way you can reduce the no. of employee loading at a time and it will reduce the fetch time from server because if you try to load all at once it will surely take time.
I know this question was asked before but the answers don't work for me.
My Code:
I have this html select box:
<select name="usergroups[]" id="usergroups" multiple="multiple" style="width:210px;">
<?php
$result = dbselect("SELECT * FROM Groups");
foreach($result as $row){
echo "<option value='".$row['GroupName']."'>".$row['GroupName']."</option>";
}
?>
</select>
The options for the select box come from a database and it worked fine. It shows what should be displayed.
Then i want the selected options to work with them in my php code.
$usGroups = $_POST['usergroups[]'];
$usgr = implode(",",$usGroups);
I convert the array to a string to save the values in my database.
My problem:
It seems that with $_POST I don't get the values and I really don't know why?
Is it because I fill in the options in a dynamic way?
Or is a mistake in the code which I'm not able to see?
It seems so simple but I don't get it.
Thank's for any help! (and sorry for my bad english...)
after submit use:
var_dump($_POST)
die();
or:
var_dump($_REQUEST)
die();
you can as well check like that:
if(isset($_POST['usergroups']) {
//do something
}
to see if the value is in those super globals.
If it's not there then you're not passing it right from the form.
Remember that you need to set method of FORM to POST for this to work ($_REQUEST will have values of either post or get).
Depending on which data you're sending from your form you might need to add enctype="multipart/form-data", i.e.
<form action="/someaction" method="post" enctype="multipart/form-data">
You're using the name of POST variable wrong, you should do it like this:
$usGroups = $_POST['usergroups'];
$usgr = implode(",",$usGroups);
Don't use array brackets to fetch the variable in PHP
Hope this helps!
I am having trouble figuring out how to implement this. I am returning user data to populate a profile page where the user can edit information. It fills in fields like name, job, title, etc. I have a field called Interests where I grab the Main Interests before I execute the code returning the user data. So I store those in an array and then return them to populate the first one. But the second is dependent on which choice they make. How should I go about filling that second box (I know the code that I will use I asking about how I should store the data to be retrieved. Because I cant execute a sql command inside the while loop, and I dont really want to return every sub-interest that just seems like a waste of resources)
Code for returning user data:
$result = mysql_query("SELECT * FROM WHERE U.uid = '$uid';");
while($row = mysql_fetch_assoc($result)){
Code for Adding Interests:
<option selected value="">Select an Interest</option>
<?
for ($i=0; $i<$setintcount; $i++)
{
if($setinterests[$i] == $interests[$i])
{
echo "<option selected value='$setinterests[$i]'>$setinterests[$i]</option>";
}
else
{
echo "<option value='$setinterests[$i]'>$setinterests[$i]</option>";
}
}
?>
</select>
Sub-Interest: <select name="selectedsubint" id="selectedsubint" onchange="checkval(id);">
<option selected value="">Select an Sub-Interest</option>
</select>
The easiest way is load all sub-interests, but you wrote its waste of resources. It depends on how much sub-interest you will have stored.
Another solution is to refresh page and get needed sub-interests, but it seems rough to me.
Elegant solution could be by using AJAX to get sub-interest. Throu AJAX you could call page that will generate right sub-interests for selected main-interest.
For my code, my drop down lists are initiated on the original page, via
<select name =country id=country
onchange=showRecords(this.value,'country','province')>"
This function is taking the value, equating it to country, then querying MySQL, and setting the results where id=province, and creating cascading dropdown lists. This is obviously via Ajax.
So, when $_REQUEST['province'] is set, then the Province dropdown list gets populated with all provinces from the country to which it belongs, etc.; i.e.;
<?if(isset($province)){
echo "<script>showRecords('".$country."','country','province');</script>";}?>
However, for the life of me, I cannot figure out how I can set the default value equal to $_REQUEST['province']. I cannot use the traditional way:
if (($selected) == ($value)) {
$options.= " selected";
}
Because it is querying the AJAX page with one piece of information at a time.
Any ideas would be greatly appreciated.
Your code doesn't seem to make a lot of sense. The particular thing that worries me is that you say ajax is loading one item at a time?
Perhaps something like this. A country select tag like...
<select onchange="showRecords(this)">
As well as creating the javascript function showRecords() which will be called when someone chooses an option in the select tag.
<script>
function showRecords(calling_element) {
// do AJAX call here using calling_element.options[calling_element.selectedIndex].value as the selected country. this.value does not work for select tags.
}
</script>
the PHP page that receives this AJAXed request would reply with a JSON object containing all of the province values, or a delimited list.
once the Javascript showRecords function receives the responce from the PHP page, it would add each of these options to the correct select tag. Once finished, it would set the default value to whichever option it wants by something like the following:
target_element.selectedIndex = {desired list index here};
I have a lot of assumptions to your questions,
first is, if bydefault you have the select province like this
<select id="province">
<option value=""></option>
<option value="California">California</option>
<option value="Washington">Washingthon</option>
</select>
then you can use this script to default select
document.getElementById("province").value="Washington";
but if bydefault you have the select province like this
<select id="province"></select>
then you can use this script to default select
document.getElementById("province").innerHTML='<option value="Wahsington">Washington</option>';
so it depend on your code and your need. maybe if you have another case the problem should be solved in another way.
cmmiiw :)
I'm building a theme options page for my WordPress theme and I would like to have a functionality to select multiple items from a list.
The "one option" select code I use looks like this: http://pastie.org/684800 and it works perfectly.
I'm a PHP newbie so I tried to modify the above code to achieve the result I want. Here's what I came up with: pastie.org/684804. As you can see, I basically added a some html values multiple="yes" hoping it will work ;)
The code displays the select item properly, but seems to only save the last selected one. Could someone please give some advice on how to achieve saving multiple chosen items?
If you change the name of the select element to end with "[]", PHP will treat it as an array. All of the selected items will be elements in the array. For example:
<select name="myChoices[]" multiple="multiple"> ... </select>
<?php
$selectedChoices = $_POST['myChoices']; // selectedChoices is an array
?>
If you give the select a name followed by [] in the form,
name="my_select[]"
you will get an array in the target PHP script that you can parse.
<select name="mySelection[]" multiple="multiple">
<option></option>
<option></option>
<option></option>
</select>
This will let you to access the multiple choice you in php