How can I make my queries short?
I have the same query multiple times on my page on different sections.
Here's the code to see what I mean:
//sql for dropdown contact type
$sql_getContactType1 = $db->query('SELECT * FROM tb_phone_contact_type');
$sql_getContactType2 = $db->query('SELECT * FROM tb_phone_contact_type');
$sql_getContactType3 = $db->query('SELECT * FROM tb_phone_contact_type');
Output in dropdown:
<select name="contact_type1" class="form-control">
<?php
while($row = $sql_getContactType1->fetch_assoc())
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
</select>
<select name="contact_type2" class="form-control">
<?php
while($row = $sql_getContactType2->fetch_assoc())
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
<select name="contact_type3" class="form-control">
<?php
while($row = $sql_getContactType3->fetch_assoc())
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
</select>
How can I achieve it in just one query statement on my output?
Do the query once, put the results in an array, and then use that array in each <select>.
<?php
$sql_getContactType = $db->query('SELECT id, name FROM tb_phone_contact_type');
$contact_types = array();
while ($row = $sql_getContactType->fetch_assoc()) {
$contact_types[] = $row;
}
?>
<select name="contact_type1" class="form-control">
<?php
foreach($contact_types as $row)
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
</select>
<select name="contact_type2" class="form-control">
<?php
foreach($contact_types as $row)
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
<select name="contact_type3" class="form-control">
<?php
foreach($contact_types as $row)
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
</select>
Why using 3 select? u can use one they do same job.
For selects you must use foreach.
First, get all data in a single query. Then you can add if inside your loop to determine the contact type to show. This example code may help you:
$contacts = $db->query('SELECT * FROM tb_phone_contact_type')->fetch_assoc();
<select name="contact_type1" class="form-control">
<?php
foreach ($contacts as $contact) {
// fit this code by the `type` you want to show
if ($contact['type'] === 'CONTACT_TYPE_1') {
echo '<option value="'.$contact['id'].'">'.$contact['d_name'].'</option>';
}
}
?>
</select>
<select name="contact_type2" class="form-control">
<?php
foreach ($contacts as $contact) {
// fit this code by the `type` you want to show
if ($contact['type'] === 'CONTACT_TYPE_2') {
echo '<option value="'.$contact['id'].'">'.$contact['d_name'].'</option>';
}
}
?>
<select name="contact_type3" class="form-control">
<?php
foreach ($contacts as $contact) {
// fit this code by the `type` you want to show
if ($contact['type'] === 'CONTACT_TYPE_3') {
echo '<option value="'.$contact['id'].'">'.$contact['d_name'].'</option>';
}
}
?>
</select>
Execute query and store data into array... You can use array multiple times..
$sql_getContactType1 = $db->query('SELECT * FROM tb_phone_contact_type');
while($row = $sql_getContactType1->fetch_assoc())
{
$row_info[] = $row;
}
foreach ($row_info as $info) {
echo "Id: {$info[id]}<br />"
. "Name: {$info[name]}<br />"
. "Code: {$info[code]}<br /><br />";
}
SQL for dropdown contact type
<?php
$sql_getContactType1 = $db->query('SELECT * FROM tb_phone_contact_type');
while($row = $sql_getContactType1->fetch_assoc())
{
$row_data[] = $row;
}
?>
Output in dropdown :
<select name="contact_type1" class="form-control">
<?php
foreach($row_data as $row_val)
{
echo '<option value="'.$row_val[id].'">'.$row_val[d_name] . '</option>';
}
?>
</select>
<select name="contact_type2" class="form-control">
<?php
foreach($row_data as $row_val)
{
echo '<option value="'.$row_val[id].'">'.$row_val[d_name] . '</option>';
}
?>
<select name="contact_type3" class="form-control">
<?php
foreach($row_data as $row_val)
{
echo '<option value="'.$row_val[id].'">'.$row_val[d_name] . '</option>';
}
?>
</select>
Use above the code if you need 3 dropdown on a page.
Related
i already made a box in which skills are multiselected if we type something in text box,but i want to add those skills which are not inside my database that user type.
my question is how i make that?
my code is-
<span class="pf-title">skills</span>
<div class="pf-field no-margin">
<select id="lstFruits"multiple="multiple"class="chosen"name="skill[]"
data-placeholder="Please Select Skill">
<?php
$fetch="SELECT * FROM `skill`";
$record=mysqli_query($conn,$fetch);
while($records_row=mysqli_fetch_array($record))
{?>
<option value="<?php echo $records_row['skill_name'];?>"<?
php foreach($sk as $s){if($records_row['skill_name'] == $s)
{ echo 'selected="selected"';}}?>><?php echo
$records_row['skill_name'];?></option>
<?php }
?>
</select>
Try this way:-
<select id="lstFruits" multiple="multiple" class="chosen" name="skill[]" data-placeholder="Please Select Skill">
<?php
$fetch="SELECT * FROM `skill`";
$record=mysqli_query($conn,$fetch);
$flag = false;
while($records_row = mysqli_fetch_array($record)){
foreach($sk as $s){
if($records_row['skill_name'] == $s) {
echo '<option value="'.$records_row['skill_name'].'" selected="selected">'.$records_row['skill_name'].'</option>';
$flag = true;
break;
}
}
if(!$flag) {
echo '<option value="'.$records_row['skill_name'].'">'.$records_row['skill_name'].'</option>';
}
$flag = false;
}
?>
</select>
Actually, I have a field name (industry) the problem is that I want to get two value from the database change of industry by jquery but how do I do this can't understand. Please help-
Here is my code-
<label>Select Type of Industry</label>
<select class="form-control" name="industry" id="industry" onchange="industry_id(this.value);" required="">
<option value="">------------------Select Your Industry----------------</option>
<?php
foreach ($industry as $key => $value) { ?>
<option value="<?= $value['id']; ?> "> <?= $value['industory']; ?></option>
<?php } ?>
</select>
</div>
<script>
function industry_id(value) {
$.get("<?php echo base_url()?>dive/getebitvalue/"+value,function (data) {
$("#ebit_name").html(data);
// alert(data);
});
}
</script>
one value I get easily but how to get second from the same id?
Here is my model---
function getebit()
{
$industry=$this->uri->segment(3);
$sql = $this->db->query('SELECT * FROM industry WHERE id='.$industry);
$result = $sql->result();
$html='';
foreach ($result as $data) {
$html.= "<option value='" . $data->ebit . "' >" . $data->ebit . "</option>";
}
echo $html;
}
}
and here is my controller--
function getebitvalue(){
$data['ebit']= $this->site_model->getebit();
}
Method getebitvalue in controller should be like this :
function getebitvalue()
{
$industry_id = $this->uri->segment(3);
$result = $this->site_model->getebit($industry_id);
$html='';
if ( !empty($result)){
foreach ($result as $item)
{
$edita[$key] = $item->edita;
$html.= "<option value='" . $item->ebit . "' >" . $item->ebit . "</option>";
}
$data['edita'] = $edita;
$data['html'] = $html;
print_r($data);
exit;
}
}
Here is your model---
function getebit($industry_id)
{
$sql = $this->db->query('SELECT * FROM industry WHERE id='.$industry);
$result = $sql->result();
return $result;
}
You can also do this-
view page-
<div class="form-group">
<label>Select Type of Industry</label>
<select class="form-control industryData" name="industry" id="industry" required="">
<option value="">------------------Select Your Industry----------------</option>
<?php
foreach ($industry as $value) { ?>
<option data1="<?php echo $value['ebitda']; ?>" data2="<?php echo $value['ebit']; ?>" value="<?= $value['id']; ?>"> <?= $value['industory']; ?></option>
<?php } ?>
</select>
</div>
model page-
function select($tbl,$con=''){
$this->db->select("*");
$this->db->from($tbl);
if(!empty($con))
$this->db->where($con);
$query = $this->db->get();
return $query->result();
}
controller page-
public function dive(){
$this->load->view('common/header');
$data['industry'] = $this->site_model->select('industry');
$this->load->view('dive/dive',$data);
$this->load->view('common/footer');
}
Now use jquery on your view page--
<script>
$(document).on("change", ".industryData", function () {
var value = $(this).val();
var data1 = $('.industryData option:selected ').attr('data1');
var data2 = $('.industryData option:selected ').attr('data2');
alert(data1);
});
</script>
how to fix this syntax for this logic ?
i want to select my select option to select the another select option
<?php
$query_string = "SELECT * FROM products";
$query_string1 = "SELECT * FROM suppliers where ProductID = // firstSelectoption(value)";
$query_string2 = "SELECT * FROM categories";
$query = mysql_query($query_string);
$query1 = mysql_query($query_string1);
$query2 = mysql_query($query_string2);
?>
and in the body i make
<select name="first" id="first" onchange="childrenOnChange(this.value)">
<?php
while ($row = mysql_fetch_array($query)) {
echo '<option value=' . $row["ProductID"] . '>';
echo $row['ProductID'];
echo '</option>';
}
?>
</select>
<select name="second" id="second">
<?php
while ($row = mysql_fetch_array($query1)) {
echo '<script>';
echo 'var arr = array(';
$row['SupplierID'] . ',';
echo ')';
echo '</script>';
}
?>
</select>
i want to set the second select option value with $query1;
If you get the value from the query with $val = mysql_fetch_array($query1), then you can include the following in your loop:
$selected = '';
if ($row['ProductID'] == $val) {
$selected = "selected";
}
echo '<option value="'.$row['ProductID'].'" '.$selected.'>'.$row['ProductID'].'</option>';
Here goes my first post.
I am currently pulling two fields out from my MySQL database with a fetch all and am trying to get the data in those fields to become options in a listbox.
This is my code:
<fieldset class="contact">
<legend>Select a Band</legend>
<!-- Drop List -->
<select id="lst1" name="lst1" tabindex="281" size="1">
<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"];
$options .= '<option value="' . $id . '">' . $name . '</option>';
}
echo $options;
?>
</select>
and here is the result of echoing out $options:
<option value="1">Rise Against</option><option value="2">Alter Bridge</option><option value="3">Falling In Reverse</option><option value="4">Saosin</option><option value="5">Pennywise</option><option value="6">The Killers</option><option value="7">Thrice</option><option value="8">Four Year Strong</option>
I want to have the foreach loop print out the code that will be the options for my listbox lst1 but it is currently not working. Any ideas as to why?
<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"]; ?>
<option value = "<?php echo $id; ?>"><?php echo $name; ?></option>
<?php } ?>
Try this code. In your code '<' and '>' makes html entity format. So html does't accept it as a tag.
<select id="lst1" name="lst1" tabindex="281" size="1">
<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"];
$options .= '<option value="' . $id . '>' . $name . '</option>';
}
echo $options;
?>
</select>
I am currently working with a dynamic dropdown menu(dependable select boxes). I am pulling the values straight from MySQL DB(if your curious here is how i have the DB SETUP). I am able to get the values of each table and display them accordingly. The problem I am having is echoing the SELECTED value of each selct box. I have a created JS function that will request postfile.php which will then echo the SELECTED value of each box. I am not getting anything echoed. I have checked with firebug but nothing is being posted.
How can I make this work? or Am I approaching this wrong? or Is there a better way? EXAMPLE
Working HTML/PHP
<?php
include ('includes/dbConnect.php');
try {
$pdo = get_database_connection();
$sql = "SELECT *
FROM `categories`
WHERE `master_id` = 0";
$statement = $pdo->query($sql);
$list = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo 'There was a problem';
}
?>
<select name="main" id="main" size="7" class="update">
<option value="">Select one</option>
<?php if (!empty($list)) { ?>
<?php foreach($list as $row) { ?>
<option value="<?php echo $row['id']; ?>">
<?php echo $row['name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
<select name="subc1" id="subc1" size="7" class="update" disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc2" id="subc2" size="7" class="update" disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc3" id="subc3" size="7" class="update" disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
JS
<script type="text/javascript">
$(document).ready(function () {
$('#main).change(function() {
if ($(this).val()!='
') {
$("#subc1").load("postfile.php",{main_id: $(this).val()});
//$("#subc1").removeAttr('
disabled hidden ');
}
});
//code on change of sel_source
$('#subc1 ').change(function() {
if ($(this).val()!='
') {
$("#subc2").load("postfile.php",{subc1_id: $(this).val()});
//$("#colour").removeAttr('
disabled ');
}
});
$('#subc2 ').change(function() {
if ($(this).val()!='
') {
$("#subc3").load("postfile.php",{subc2_id: $(this).val()});
//$("#colour").removeAttr('
disabled ');
}
});
});
</script>
PHP- postfile.php
if(isset($_REQUEST['main_id']) && !empty($_REQUEST['main_id'])) {
try {
include ('../includes/dbConnect.php');
$pdo = get_database_connection();
$sql = ("select * from `categories` where id='".$_REQUEST['main_id']."' ");
$result = $con->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();
}catch(PDOException $e) {
echo 'There was a problem';
}
if($number_of_rows > 0) {
$output = '<option value="">Select</option>';
while($row = mysql_fetch_assoc($result)) {
$output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
} else {
$output = '<option value="">Select</option>';
}
echo $output;
}
if(isset($_REQUEST['subc1_id']) && !empty($_REQUEST['subc1_id'])) {
$result = mysql_query("select * from table where id='".$_REQUEST['subc1_id']."' ");
if($number_of_rows > 0) {
$output = '<option value="">Select</option>';
while($row = mysql_fetch_assoc($result)) {
$output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
} else {
$output = '<option value="">Select</option>';
}
echo $output;
}
if(isset($_REQUEST['subc2_id']) && !empty($_REQUEST['subc2_id'])) {
$result = mysql_query("select * from table where id='".$_REQUEST['subc2_id']."' ");
if($number_of_rows > 0) {
$output = '<option value="">Select</option>';
while($row = mysql_fetch_assoc($result)) {
$output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
} else {
$output = '<option value="">Select</option>';
}
echo $output;
}
Maybe you would like to use jQuery UI auto-complete. It is easier to use and less code. It has a remote data source also. Try this one, maybe this can solve your problem. http://jqueryui.com/demos/autocomplete/
Add on submit action to the form. In the function use the following:
var Myvar = $('#subc3 :selected').text();