I am fairly new to php. I am trying to update the score everytime I loop over this array. my code only works if the first value of the listbox was selected by the user and it gives a zero if its not selected. please help.
This is A.php
{
$SkillsArray = array();
$Score=0;
$SkillsArray = $_POST['DutiesDesc'];
//foreach($SkillsArray as $key =>$value )
{
$Sentence = $SkillsArray[0]." ".$SkillsArray[1]." ".$SkillsArray[2]." ".$SkillsArray[3]." ".$SkillsArray[4]." ".$SkillsArray[5]." ".$SkillsArray[6]." ".$SkillsArray[7 ]." ".$SkillsArray[8]." ".$SkillsArray[9]." ".$SkillsArray[10];
}
//Get the applicants score
for($i=0;$i<11;$i++)
{
if ($SkillsArray[$i] == $Text[$i])
{
$Score = $Score+$Val[$i];
}
}
} //**** The following is the HTML part of the code(form)
<form action = "A.php" method ="POST" enctype="multipart/form-data">
<label for="Position">Position:</label><input type="type" name="Position" size="35" /><br />
</p>
<p>
<!-- <label for="DutiesDesc">Duties Description: </label><textarea name="DutiesDesc" cols="30" rows="5" /></textarea>--> <br />
Job Description
<select name="DutiesDesc[]" size=5 multiple="multiple">
<option value="<?php echo $Arow['TextF1']?>"><?php echo $Arow['TextF1']?></option>
<option value="<?php echo $Arow['TextF2']?>"><?php echo $Arow['TextF2']?></option>
<option value="<?php echo $Arow['TextF3']?>"><?php echo $Arow['TextF3']?></option>
<option value="<?php echo $Arow['TextF4']?>"><?php echo $Arow['TextF4']?></option>
<option value="<?php echo $Arow['TextF5']?>"><?php echo $Arow['TextF5']?></option>
<option value="<?php echo $Arow['TextF6']?>"><?php echo $Arow['TextF6']?></option>
<option value="<?php echo $Arow['TextF7']?>"><?php echo $Arow['TextF7']?></option>
<option value="<?php echo $Arow['TextF8']?>"><?php echo $Arow['TextF8']?></option>
<option value="<?php echo $Arow['TextF9']?>"><?php echo $Arow['TextF9']?></option>
<option value="<?php echo $Arow['TextF10']?>"><?php echo $Arow['TextF10']?></option>
<option value="<?php echo $Arow['TextF11']?>"><?php echo $Arow['TextF11']?></option>
</select><br />
The values in the array $SkillsArray do not have the same index as your $Text array. If you select the 3rd (lets say the value is "c") and 5th (e.g. "e") item in your list, the array $SkillsArray will contain the following:
$SkillsArray[0] = "c";
$SkillsArray[1] = "e";
So what you need is a simple search over your $SkillsArray, since you can't rely on the indexes being conform with your $Text array.
if (is_array($SkillsArray)) {
for($i = 0; $i < count($Text); $i++) {
if (array_search($Text[$i], $SkillsArray) !== false) {
$Score += $Val[$i];
}
}
}
Please do remember to check, if $SkillsArray is indeed an array. If the user doesn't check any skills, the function array_search will return false for every item, which will result in the maximum score.
Documentation for functions: count, array_search, is_array
The
for($i=0;$i<11;$i++)
{
for($j=0;$j<11;$j++)
{
if (($SkillsArray[$i] == $Text[$j]))
{
$Score = $Score+$Val[$j];
}
}
This required a second loop in order to work properley:)
Related
Iam trying a solution in PHP/MYSQL where in there is a some country list with a checkbox. Certain countries have something called entities which is a drop down which shows Micro, Small, Large as a dropdown. This entity dropdown appears for certain countries only. I am able to pass the checkbox checked values to next page. But i need to pass the value of the entity selected in the drop down also. Iam not able to acheieve that. My code ie posted below. Here i need to pass the value of the drop down (select - entity_selected) also to next page:
<?php
echo '<table border="1">';
while($row1 = mysqli_fetch_array($result_country)) {
$country1 = $row1["country"];
$entity1 = $row1["entity"];
echo '<tr>';
?>
<td><input name="check_list[]" type="checkbox" value="<?php echo $country1;?>"> <?php echo $country1;?></td>
<td>
<?php
if($entity1 == 'Yes'){ ?>
<select class="form-control selectpicker" name="entity_selected">
<option value="Micro">Micro</option>
<option value="Small">Small</option>
<option value="Large">Large</option>
</select>
<?php }
?>
</td>
<?php echo '</tr>'; } ?>
</table>
The Next page code is below to get the check box selected countries.
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check;
}
}
How can i get the drop down values here? iam getting the checkbox (countries values correctly). Can anyone pls help me on this pls?
<form method="POST" action="nextpage.php">
<table border="1">
<?php
$i = 0;
while($row1 = mysqli_fetch_array($result_country)) {
$country1 = $row1['country'];
$entity1 = $row1['entity'];
echo "<tr>";
?>
<td>
<input name="check_list<?=$i ?>" type="checkbox" value="<?php echo $country1;?>"> <?php echo $country1;?>
</td>
<td>
<?php
if($entity1=="Yes") {
?>
<select class="form-control selectpicker" name="entity_selected<?=$i ?>">
<option value="Micro">Micro</option>
<option value="Small">Small</option>
<option value="Large">Large</option>
</select>
<?php
};
?>
</td>
<?php
$i++;
echo "</tr>";
};
?>
</table>
</form>
nextpage.php:
<?php
$entities = $checklist = [];
foreach($_POST as $k => $v) {
if(preg_match("/^checklist(\d+)$/", $k, $matches))
$checklist[intval($matches[0])] = $v;
unset($matches);
if(preg_match("/^entity_selected(\d+)$/", $k, $matches))
$entities[intval($matches[0])] = $v;
};
?>
I have some options to select and would like to set some textinputs to visible or hidden depending on what user selects. I have the functionality already but in this case I need to take the second value of the echo and check if its equal to something, I can currently only check the first value of the echo:
<select id="type_id" name="device_type_id" onchange="if (this.value==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
So, I need to get ONLY the value of $row['serial_or_imei'] and compare onchange="if (this.value=='') , how can I do that?
EDIT: further explanation
I need to read the value of $row['serial_or_imei'] which could be empty, 'serial' OR 'imei', so, one of these three.
P.S: I must keep the $row['id'] in the value attribute because I am passing that value later to server so dont suggest removing that.
You can add one attribute to option element and test with it. something like
<select id="type_id" name="device_type_id" onchange="if (this.getAttribute('data')==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option data="<?php echo $row['serial_or_imei'] ?>" value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
You can compare the value of an array in the onchange by assign the value to a variable which will be a quite neat format as
<?php $value = echo $row['serial_or_imei']; ?>
<select id="type_id" name="device_type_id" onchange="if (this.value=='<?php empty($value)?>'){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
You add a new custom attribute serial and supply the value of serial_or_imei to it and then compare in javascript with onchange="if (this.value=='')
<select id="type_id" name="device_type_id" onchange="if (this.value==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>" data-serial="<?php echo $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
EDIT: Attribute “serial” is not allowed on element “option”. Changed this to data-seial.
You say you need the values back to server because they change dynamically. A clean way would be to have a dedicated hidden field:
<?php foreach ($result as $row) { ?>
<input type="hidden" name="map[<?php echo $row['id']; ?>]" value="<?php echo $row['serial_or_imei']; ?>">
<? } ?>
Here's a full self-contained script to illustrate it:
<?php
$random_serial_or_imei = range(1000, 9999);
shuffle($random_serial_or_imei);
$result = [];
for ($i = 0; $i < 5; $i++) {
$result[] = [
'id' => $i,
'serial_or_imei' => $random_serial_or_imei[$i],
'name' => "Name #$i",
];
}
?>
<select name="device_type_id">
<option value="">Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id']?>"><?php echo $row['name']; ?></option>
<?php } ?>
</select>
<?php foreach ($result as $row) { ?>
<input type="hidden" name="map[<?php echo $row['id']; ?>]" value="<?php echo $row['serial_or_imei']; ?>">
<? } ?>
This prints something like:
<select name="device_type_id">
<option value="">Select</option>
<option value="0">Name #0</option>
<option value="1">Name #1</option>
<option value="2">Name #2</option>
<option value="3">Name #3</option>
<option value="4">Name #4</option>
</select>
<input type="hidden" name="map[0]" value="9513">
<input type="hidden" name="map[1]" value="3931">
<input type="hidden" name="map[2]" value="3971">
<input type="hidden" name="map[3]" value="4476">
<input type="hidden" name="map[4]" value="6429">
Selected type can be found at $_POST['device_type_id'] and the corresponding IMEI at $_POST['map'][ $_POST['device_type_id'] ].
Based on the information from the comments, I think this solution is what you are looking for. Note I move the onchange to a function for clarity.
You can see the "three" option hides the text input as it doesn't have a imei.
Also notice how I use the data- attribute inside your option so you can retrieve that information as well, independent of the option id. Also note the this.form['device_imei_textinput'] didn't worked. The elements reference is required.
function check_imei(obj, form) {
var imei = obj.options[obj.selectedIndex].getAttribute('data-imei');
if (imei == '') {
form.elements.device_imei_textinput.style.visibility='hidden';
} else {
form.elements.device_imei_textinput.style.visibility='visible';
}
}
<form>
<select id="type_id" name="device_type_id" onchange="check_imei(this, this.form);">
<option value='Select'>Select</option>
<option value="1" data-imei="foo">One</option>
<option value="2" data-imei="bar">Two</option>
<option value="3" data-imei="">Three</option>
</select>
<input type="text" id="device_imei_textinput" name="device_imei_textinput" />
</form>
Mirror of example : https://jsfiddle.net/asp1hewx/
EDIT : To address the PHP side of things, the form above could be generated with :
<form>
<select id="type_id" name="device_type_id" onchange="check_imei(this, this.form);">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>" data-imei="<?php $row['serial_or_imei'] ?>"><?php echo $row['name'] ?></option>
<?php } ?>
</select>
<input type="text" id="device_imei_textinput" name="device_imei_textinput" />
</form>
I have a select box with options up to 20, for example, if option 5 or any other is selected and saved in a database in the specific column it won't show next time...
please guide me
<select class="form-control text-center" name="code" id="Code" >
<option value="">Select Code</option>
<?php for($i = 1; $i <= 20; $i++){ ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>
Controller:
function index(){
//below code should be in Model this is only for example
$this->db->select('code');
$query = $this->db->get_where('hr_levels', array('created_by' => $_SESSION['username']));
if ($query->num_rows() > 0) {
$code = $query->row('code');
} else {
$code = 0;
}
$option = array();
for($i=1;$i<=20;$i++){
if ($code != $i) {//no need to add if in view, just pass the data to view
$option[$i] = $i;
}
}
$data['options'] = $option;
$this->load->view('option_view',$data);
}
view:
<select class="form-control text-center" name="code" id="Code" >
<option value="">Select Code</option>
<?php foreach($options as $key => $value){
<option value="<?php echo $key; ?>"><?php echo $value; ?></option>
<?php } ?>
</select>
You can approach it like this
$availableOption = range(1,20);
$selectedOrInDb = [5,6,8,9]; // Selected OR from DB
$remaining = array_diff($availableOption, $selectedOrInDb);
Now you can use $remaining to loop through
<select class="form-control text-center" name="code" id="Code" >
<option value="">Select Code</option>
<?php foreach($remaining as $key => $value){ ?>
<option value="<?php echo $value; ?>"><?php echo $value; ?></option>
<?php } ?>
</select>
Controller
public function get_selected(){
$this->load->model('model_name');
$data['selected'] = $this->model_name->function_name($_SESSION['username']);
//Already saved values will be get on the basis of any condition
return $this->load->view('file.php');
}
Model
public function function_name($username){
return $this->db->get_where('table', ['created_by' => $username])->row()->value;
}
Now you get already selected value in $selected
<select class="form-control text-center" name="code" id="Code" >
<option value="">Select Code</option>
<?php for($i = 1; $i <= 20; $i++){
if($i != $selected)?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } } ?>
</select>
I used selection box with multiple selection now the problem is that it is selectable with ctrl+click of mouse. It is work properly but not that much prefrble to me and lookes like simple selection box and user cant get that its multiple selector not single.so thats why i want it with check box so user easly get it is multiple selector.please give apropriate solution thanks in advanced...
<select class="selopt" id="selPreLoc" name="SelPreLoc[]" multiple="multiple" size=5>
<option label="No Preference">No Preference</option>
<?php
//<option value=-1 selected>No Preference</option>
while ($rec = mysql_fetch_array($GetCityRecord)) {
if ($rec['City_Id'] == 30 || $rec['City_Id'] == 34 || $rec['City_Id'] == 35) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
?>
<option value="<?php $rec['City_Id']; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $rec['City']; ?>
</option>
<?php
}
foreach ($others as $ind => $val) {
?>
<option value="<?php echo $ind; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $val; ?>
</option>
<?php }
?>
</select>
<label class="formtxt" valign="bottom">Use Ctrl + Click to multi-select.</label></td>
I got many other solution with using div or other.
but i just want it with select option only is it posible if yes how .andi can fetch the result in mysql and i want that result with comma seprator in mysql.
My code is not tested, since i do not have the data, but based on your logic, you can use it like this:
$theOthers = array(30, 34, 35);
while ($rec = mysql_fetch_array($GetCityRecord)) {
if (in_array($rec['City_Id'], $theOthers)) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php $rec['City_Id']; ?>" <?php echo $checked; ?> /> <?php echo $rec['City']; ?> <br />
<?php
}
foreach ($others as $ind => $val) {
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php echo $ind; ?>" <?php echo $checked; ?> /> <?php echo $val; ?> <br />
<?php
}
NOTE: I improved your code a littlebit with the $theOthers array, i think it's more readable.
When form is submitted, let's var_dump($_POST["city"]);
Background
I have a for loop creating a table of inputs for an html form:
for ($i = 1; $i <= $x; $i++) {
echo '<select name="waldo_'.$i.'" id="waldo_'.$i.'">
<option value="">...</option>
<option value="foo">Foo</option>
<option value="bar">Bar</option>
</select>
<label for="foo_'.$i.'">Foo '.$i.'</label>
<input id="foo_'.$i.'" type="text" value="" name="foo_'.$i.'">
<label for="bar_'.$i.'">Bar '.$i.'</label>
<input id="bar_'.$i.'" type="text" value="" name="bar_'.$i.'">';
}
On submit, this populates a database.
Problem
Each submission needs to be editable. When I return to the form (as an admin) I need to see everything that was stored in the database by a particular user.
for ($i = 1; $i <= $x; $i++) {
echo '<select name="waldo_'.$i.'" id="waldo_'.$i.'">
<option value="">...</option>
<option value="foo"';
if($row['waldo_'.$i] == "foo") echo " selected='selected'";
echo '>Foo</option>
<option value="bar"';
if($row['waldo_'.$i] == "bar") echo " selected='selected'";
echo '>Bar</option>
</select>
<label for="foo_'.$i.'">Foo '.$i.'</label>
<input id="foo_'.$i.'" type="text" value="'./*...*/.'" name="foo_'.$i.'">
<label for="bar_'.$i.'">Bar '.$i.'</label>
<input id="bar_'.$i.'" type="text" value="'./*...*/.'" name="bar_'.$i.'">';
}
My select properly "selects" the correct option, but I don't seem to be able to populate the text input values in a similar manner.
Somehow I need to echo the content in $foo_1, $foo_2, $foo_3, ..., $foo_x.
I have tried using $foo_.$i but that doesn't seem to work.
Is there a simple solution to this problem? Or is there a better method to format everything?
If I'm not misunderstanding your question:
$_POST["foo_".$i]
Should show you the submitted data.
EDIT: Or maybe this is what you are looking for?
for ($i = 1; $i <= $x; $i++) {
echo '<select name="waldo_'.$i.'" id="waldo_'.$i.'">
<option value="">...</option>
<option value="foo"';
if(isset($row['waldo_'.$i]) && $row['waldo_'.$i] == "foo") echo " selected='selected'";
echo '>Foo</option>
<option value="bar"';
if(isset($row['waldo_'.$i]) && $row['waldo_'.$i] == "bar") echo " selected='selected'";
echo '>Bar</option>
</select>
<label for="foo_'.$i.'">Foo '.$i.'</label>
<input id="foo_'.$i.'" type="text" value="';
if(isset($row['foo_'.$i]) && $row['foo_'.$i] != "") echo $row['foo_'.$i];
echo '" name="foo_'.$i.'">
<label for="bar_'.$i.'">Bar '.$i.'</label>
<input id="bar_'.$i.'" type="text" value="';
if(isset($row['bar_'.$i]) && $row['bar_'.$i] != "") echo $row['bar_'.$i];
echo '" name="bar_'.$i.'">';
}