PHP MySQL set selected selectpicker multiple - php

i need help for my php script with bootstrap selectpicker integration
i have php script for show selectpicker multiple value
<select id="skill" name="skill" class="selectpicker" multiple data-size="5" data-selected-text-format="count">
<?php
$skills = mysql_query("SELECT skill_id, skill_name FROM skill",$conn);
while($r_skills = mysql_fetch_assoc($skills)){
echo '<option value="'.$r_skills['skill_id'].'">'.$r_skills['skill_name'].'</option>
';
}
?>
</select>
and on my other table i have user and skill_id, for skill_id content : 1, 2, 3
i had explode it into each value like this :
$q = ("SELECT nik, skill_id FROM dosen WHERE nik = '".$nik."'",$conn);
while($row_dosen = mysql_fetch_assoc($q)){
$pieces = explode(",", $row_dosen['skill_id']);
for($i = 0; $i < count($pieces) ; $i++){
$pp = mysql_query("SELECT skill_id, skill_name FROM skill WHERE skill_id = '".$pieces[$i]."'",$conn);
while($p = mysql_fetch_assoc($pp)){
$var_each = $p['skill_id'];
}
}
}
The question is : how i can set default selected value for my multiple selectpicker option to make them selected as my user data on table ?
Thank you.
The help will be appreciated :)

How about something like:
<select id="skill" name="skill" class="selectpicker" multiple data-size="5" data-selected-text-format="count">
<?php
$skills = mysql_query("SELECT skill_id, skill_name FROM skill",$conn);
while($r_skills = mysql_fetch_assoc($skills)){
echo '<option value="'.$r_skills['skill_id'].'"';
if ($r_skills['skill_id'] == '1337 codingz')
echo ' selected';
echo '>'.$r_skills['skill_name'].'</option>';
}
?>
</select>

Related

Writing an IF statement in a dynamic dropdown select menu gives ERROR

I want to write an IF statement for a dynamically generated dropdown menu, but I keep getting errors or it doesn't just work, what I am trying to do is compare two variables to see if they match from a data in the database
$loma = "Asokoro";
$row['locales'] is from locality table in the database
$row['locales'] = "Asokoro";
$row['locales'] = "Dutse";
$row['locales'] = "Mari";
$row['locales'] = "Cook";
That means if $loma which is Asokoro matches $row['locales'] = "Asokoro"; select it as the option menu
<select name="checkout_area_name" id="checkout_area_name" required>
$query = "SELECT * FROM `locality` WHERE state_name = '$hi_state'";
$sql = mysqli_query($con, $query) or die(mysqli_error($con, $query));
$r = ' <option value="">Please Choose Locality</option>';
?>
<?php
while ( $row = mysqli_fetch_assoc($sql))
{
?>
<?php $r = $r . '<option value="'.$loma.'" if ("'.$loma.' == '.$row["locales"].'") selected="selected" >'.$row['locales'].'</option>'; ?>
<?php
}
echo $r;
?>
</select>
I am trying to select the options menu that has $loma and $row['locales'] matching but I keep getting errors or when I console.log, it does not produce the result i want.
You are outputting php script as html markup, try changing your code to:
<select name="checkout_area_name" id="checkout_area_name" required>
$query = "SELECT * FROM `locality` WHERE state_name = '$hi_state'"; $sql = mysqli_query($con, $query) or die(mysqli_error($con, $query)); $r = '
<option value="">Please Choose Locality</option>'; ?>
<?php
while ( $row = mysqli_fetch_assoc($sql))
{
$r = $r . '<option value="'.$loma.'" '.(($loma==$row["locales"])?'selected':'').'>'.$row['locales'].'</option>';
}
echo $r;
?>
</select>

Insert to a table column based on php variable

I have a comboBox that has options: transpo, meal and medical. I want to insert the values into the column that corresponds to option that the user chooses.
Is it possible?
This is my latest query.
$conn->query("INSERT INTO expenses
SET employee_id = :emp_id,
client_id = :client_id,
CASE
WHEN :cat == 'transpo' THEN transpo_exp = :worth
WHEN :cat == 'meal' THEN meal_exp = :worth
WHEN :cat == 'medical' THEN medical_exp = :worth");
please use array [] symbol in select name.
<select name="expenses[]" multiple="multiple">
<option value=""></option>
<option value=""></option>
<option value=""></option>
</select>
and get array variable.
$expenses= $_POST['expenses'];
for($i = 0; $i < count($expenses); $i++){
echo $expenses[$i];
$Qry = mysql_query("INSERT INTO expenses VALUES('$expenses[$i]]')");
}

bootstrap select option multiple selection not inserting all selected options

I have bootstrap chosen-select multiple select option. It is wokring fine but when i go to insert multiple selections into database it doesnt insert all selected options it inserts only the biggest value from the selected options.
Select Box
<select name="type" data-placeholder="Choose a Card Type..." class="chosen-select" style="text-align: left;" tabindex="2" multiple>
<?php
$q_all_categories = mysql_query("SELECT * from sort_kcc");
while ($all_categories = mysql_fetch_array($q_all_categories)) {
$category_id = $all_categories['id'];
$categories_name = $all_categories['ename'];
$categories_aname = $all_categories['aname'];
$q_selected_cat = mysql_query("
SELECT * FROM rel_sort_kcc WHERE sort_id=".$category_id."
AND sr_id=".$_GET['id']."
");
$selected = "";
while ($category = mysql_fetch_array($q_selected_cat)) {
$selected_category = $category['sort_id'];
if($category_id == $selected_category){$selected = "selected";}
}
print "<option class='ur' ".$selected." value='".$category_id."'>".$categories_name."</option>";
$selected = "";
}
?>
</select>
Insert Query
$categoriesCT = $_POST['type'];
for($i=0; $i < count($categoriesCT); $i++) {
mysql_query("
INSERT INTO
rel_sort_kcc
(sr_id, sort_id)
VALUES
('".$_POST['idName']."', '".$categoriesCT[$i]."')
");
}

mysql_fetch_array problem code not processing

My page stops loaded every time I turn on the code below... it looks correct and the tables and fields are correct.
<select name="common" style="width: 136px;">
<?php
$group1 = mysql_fetch_array(mysql_query("SELECT country FROM lang_list WHERE grouping = '1' ORDER BY p_order"));
while($row = $group1){
echo "<option value=\"$group1\">$group1</option>\n";
}
?>
</select>
<?php
$group1 = mysql_query("SELECT country FROM lang_list WHERE grouping = '1' ORDER BY p_order");
while($row = mysql_fetch_array($group1)){
echo "<option value=\"$row[country]\">$row[country]</option>\n";
}
?>
Try this:
<select name="common" style="width: 136px;">
<?php
$recordset = mysql_query("SELECT country FROM lang_list WHERE grouping = '1' ORDER BY p_order") or die("Error found: " . mysql_error());
while($row = mysql_fetch_array($recordset)){
echo "<option value=\"".$row['country']."\">".$row['country']."</option>\n";
}
?>
</select>

PHP help with building query

I have several drop lists where if no option is selected then the value is = ""...
I cant figure out how to build the query for mysql in PHP.
query = SELECT * FROM db
I assume you have a select like this:
<select name="data[]" multiple="multiple">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
Your php can be something like
<?php
$data = array();
$data = $_POST['data'];
$query = "select * from table";
if (count($data > 0)) {
for ($i = 0; $i < count($data); $i++) {
$data[$i] = "'{$data[$i]}'";
}
$query .= " where field in (".implode(",", $data).")";
}
Too less information, but here's what I would do
$rows = $db->query(
'select *
from
table
where
checkbox_value = ?',
$_POST['checkbox']
);
In $rows you will have all the data you need.
You can run a SELECT on a table not on a DB! A Database consists of many tables. See http://www.php.net/manual/en/function.mysql-select-db.php
Check out w3Schools sql tutorials.
Or more specifically the select tutorial
Also the PHP/mysql tutorial will give you all that you need for this stuff.

Categories