Load select millions options dynamically php mysql - php

I have created html form with one select dropdown and one is textfield.
<select id="single" name="drug_id" class="form-control select2">
<option></option>
<?php
$all_drugs= "SELECT drug_id, drug_name FROM drugs";
$result = $conn->query($all_drugs);
$number_of_row = $result->num_rows;
if ($number_of_row > 0) {
while ($obj = $result->fetch_object()) {
?>
<option value="<?php echo $obj->drug_id ; ?>"><?php echo $obj->drug_name ; ?></option>
<?php
}}
else { echo "No Medicine Found."; }
?>
</select>
Its loading the data from database but its taking 2.8m because drugs table has 1 Million data.
Please help me out to load data in fastest way...

You should use AJAX based Autocomplete instead of showing 1 million of options in dropdown.
View this link for Autocomplete example.
To get the Autocomplete result fast set indexing on MySQL column on which you perform WHERE condition to find out result.

Related

PHP dropdown menu that concatenates 2 rows from MySQL data table

Not experienced with creating forms in PHP.
I can get my form to produce a dropdown list that has one of my rows listed as an option, but as soon as I try to concatenate 2 rows together (from the same table) for option output...
a) It just doesn't work and I get errors
b) I get the first row as a single option, then my next row as a separate option.
I know there is a simple solution to this, but I am an online student just learning, and I can't seem to find a good example of the code to write it. I'm pretty sure it's an issue of quotes not being placed correctly.
MySQLTable Data:
Table Name: courses
Table Rows: course_id, course_name, max_enrolment
Sample Data: LO-COMP-8001, Intro to HTML, 20
function select_course(){
global $open;
$select = "SELECT * FROM courses";
$result = mysqli_query($open, $select);
return $result;
}
<form action="insert.php" method="post">
<dl>
<dt>Select Course</dt>
<dd><select name="course_id">
<?php // CREATE dropdown menu
$result = select_course();
while ($row = mysqli_fetch_assoc($result)) {
foreach ($row as $selection) {
echo "<option value=\"$selection\">$selection</option>";
}}
?>
</select>
</dd>
</dl>
Then there are a few more form fields such as student name and student id afterwards...
Goal Output:
course_id course-name
"LO-COMP-8001 Intro to HTML" ... as a single connected dropdown option and other remaining courses in a dropdown menu
Current Output:
LO-COMP-8001 (as an option)
Intro to HTML (as another option! ... No good)
20 (must be hidden, I need to check if course is full in another function and either allow or deny a student to enrolled etc.)
I have tried:
// output is the one mentioned above..
echo "<option value=\"$selection\">$selection</option>";
// or alternatively...
echo '<option value="'.$row['course_id'].'">'.$row['course_id'].'</option>';
But the second option creates all kinds of weird results.
This is what I am experimenting with right now...
echo '<option value="'.$row['course_id'] $row['course_name']'">'.$row['course_id'] $row['course_name'].'</option>';
But there is a bunch of issues with quotes and square brackets, and I just don't know how to format it correctly for the output.
Any assistance is appreciated.
$row holds the entire row as an associative array therefore no need for the 'foreach' loop.
function select_course(){
global $open;
$select = "SELECT * FROM courses";
$result = mysqli_query($open, $select);
return $result;
}
<form action="insert.php" method="post">
<dl>
<dt>Select Course</dt>
<dd><select name="course_id">
<?php // CREATE dropdown menu
$result = select_course();
while ($row = mysqli_fetch_assoc($result)) {?>
<option value="<?php echo $row["course_id"]; ?>"><?php echo $row["course_name"]; ?></option>
<?php }
?>
</select>
</dd>
</dl>
</form>
I was able to come up with another solution as well:
Once the foreach loop was removed, I tried cleaning up the code some... I'm not sure if this is uncommon or 'bad' style, but it does work.
$result = select_course();
while ($row = mysqli_fetch_assoc($result)) {
$course_id = $row['course_id'];
$course_name = $row['course_name'];
echo "<option value=\"$course_id\">$course_id $course_name</option>";
Results in: LO-COMP-8001 Intro to HTML as a single option, plus all my other courses in the database.

How to data from tabe to combobox dynamically

how can i put the $CATEGORY dynamically so that whatever i click on the table it will retrieved in the combo box? (without settng its id to any number like 5 )
<?php
$CATEGORY = 5; //from DB table, consider 5 as category id for sample
$sql="SELECT tblcourse.id as id, tblcourse.course as course FROM tblcourse";
$result=mysql_query($sql) or die(mysql_error());
$options="";
while ($row=mysql_fetch_assoc($result)) {
$id=$row["id"];
$thing=$row["course"];
$isSel = ($CATEGORY == $id)?"selected":'';
$options.= " <OPTION VALUE='$id' $isSel>$thing</option>";
}
?>
My Combobox form code below :
<select name="cbocourse" style="height:35px; width:280px; background-color:#923227; box- shadow:1px 1px #FFF;color:#C90;" onClick="submitCATEGORY();">
<option value="<?php echo $CATEGORY; ?>">
<?php echo $options;?></option></select>
Your combobox code is wrong : you cannot parse a list of option into another super option, it means nothing. Just parse your $options between your select tags.
Just be sure to reload your page (or page fragment with AJAX for example) each time you call submitCATEGORY(), in order to regenerate your html combobox.
Your PHP code seems good.

How to populate dependant select menus dynamically using jQuery, PHP and MySQL?

I have searched quite a bit on here about this topic. But I could not find a solution for my problem. I'd appreciate it a lot if you could help me, this is for a school project I am working on.
I have a database with a table ("Main_table") and columns including "sector" and "sub_sector". I want to have two select boxes, first one will load all the records from database in "sector" column and the second one will load all the records from database in "sub_sector" column depending on the selection value of the first select box. (For example: If I select "plastics" on the first select box, then second select box should be updated with sub_sector values where sector value is equal to "plastics").
I have managed to load the options values from database for the first select box but when I click on any selection, it does not load any option to the second select box. You can find the codes below. I did not put "sector_options.php" below, as it seems to work just fine.
index.html shown below:
<script>
$(document).ready(function() {
$('#filter_sector')
.load('/php/sector_options.php'); //This part works fine - uploads options to the first select box
$('#filter_sector').change(function() {
$('#filter_subsector').load('/php/subsector_options.php?filter_sector=' + $("#filter_sector").val()
} //This part does not work - no options on the second select box
);
});
</script>
<body>
<div id="sectors"><p>Sector:</p>
<select id="filter_sector" name="select_sector" multiple="multiple" size="5"> </select>
</div>
<div id="subsectors"><p>Sub Sector:</p>
<select id="filter_subsector" name="select_subsector" multiple="multiple" size="5"> <option value="" data-filter-type="" selected="selected">
-- Make a choice --</option>
</select>
</div>
</body>
</html>
sector_options.php shown below:
<?php
$link = mysqli_connect("*******", "*******","******","********") or die (mysql_error());
$query = "SELECT sector FROM Main_table ";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_assoc($result)) {
$options .= "<option value=\"".$row['sector']."\">".$row['sector']."</option>\n ";
}
echo $options;
?>
subsector_options.php shown below:
<?php
$link = mysqli_connect("********", "*****,"*******", "********") or die (mysql_error());
$Sectors = $_REQUEST['filter_sector'];
$query = "SELECT sub_sector FROM Main_table WHERE sector='$Sectors'";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_assoc($result)) {$options .= "<option value=\"".$row['sub_sector']."\">".$row['sub_sector']."</option>\n ";
}
echo $options;
?>
For completeness, the solutions were:
Check how AJAX operations are doing using a browser network monitor
Load AJAX fetcher scripts in a browser tag - in many cases they will render quite happily there, allowing them to be more easily debugged
AJAX scripts that return HTML for injection should only return that HTML, and not a full HTML document.

an option already selected on page load in drop down list and also changeble [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP - PRE-select drop down option
I have a situation in my php project where a user can edit his account.
In editing, there is a city field which is a drop down list of cities.
I want to already select one city which is in profile before editing(the one which user enter at the time of registration).
Also he is able to change his city by choosing fron drop down list.
Html code:
<div class="search_bar1_txt">State:</div>
<div class="search_bar1">
<select class="styled" name="state_trainer">
<option>-select-</option>
<option>washington</option>
<option>perth</option>
<option>delhi</option>
<option>london</option>
</select></div>
</div>
on editing I am using this code to fetch current data(city) of user:
<?php
if(isset($_GET['userid']))
{
$sql = "select city from `wp_pelleresuser` where userId =".$_GET['userid'];
$result = mysql_query($sql);
$value = mysql_fetch_assoc($result);
}?>
please tell me how can I get already selected one option which is fetch from database.
And also it is changeable.
<div class="search_bar1_txt">State:</div>
<div class="search_bar1">
<select class="styled" name="state_trainer">
<option <?if($value['city']=='-select-') echo "selected";?>>-select-</option>
<option <?if($value['city']=='washington') echo "selected";?>>washington</option>
<option <?if($value['city']=='perth') echo "selected";?>>perth</option>
<option <?if($value['city']=='delhi') echo "selected";?>>delhi</option>
<option <?if($value['city']=='london') echo "selected";?>>london</option>
</select>
</div>
</div>
You can do it in a number of ways, the simplest (and probably least elegant) is to do something like this:
<select class="styled" name="state_trainer">
<?php
$myCity='london'; // assumed to be data from database...
echo '<option'.($myCity=='-select-') ? ' selected ' : ' ' .'-select-</option>';
echo '<option'.($myCity=='washington') ? ' selected ' : ' ' .'washington</option>';
?>
This is of course horrid.
I would rather suggest that you check the data as you are pulling your information out of the database and putting your initial dropdown list together.
If you are making an array of data to create the drop down list (for example) check it right there and then. If the city matches what you want, do it inside your loop right off the bat.
$usersCity="london";
$myCityList=array();
while( ... ) // Database loop that is pulling the data from the database.
{
$selected='';
if($userCity==$row['city'])
{
$selected=' selected ';
}
$myCityList[]='<option'.$selected.'>'.$row['city'].'</option>';
}
Then to display the drop down list, you can simply do this:
$cityCount=count($myCityList);
for($i=0;$i<$cityCount;$i++)
{
echo $myCityList[$i].'\n';
}
The users city will already be selected.
I'm not sure if it is clear way but you can put code inside every option cell like this:
<option <?php if($value['city'] == "washington") echo "selected=selected"; ?> >washington</option>
This way you can get what u want.
And better use mysqli function for database interactions.
<option
<?php if ($value['city'] == delhi)
{
echo "selected = true";
} ?>
>delhi
</option>
Just het the selected value form db like
$choosen = $some value form db
then
$options = array(1 => 'data1', 2 => 'data2', 3 => 'data3');
foreach ($options as $key => $value)
{
echo '' . $value . '';
}
to get your selection box in php.I thnk you understand,it works for me

Multiple initially selected values in a dynamic select list with PHP

Thanks in advance for your help. This list is to update an existing record, so it's populated from a database with $rs_fullcat then checked with another recordset $rs_cat for the initially selected values. I can only get it to initially select one value, the first one in $rs_cat, but I need it to select all of the existing options from the database. I feel like I'm close but I can't find the answer anywhere. Here's the code:
<select name="category" multiple="multiple" id="category">
<?php
do {
?>
<option value="<?php echo $row_rs_fullcat['categoryID']?>"<?php if (!(strcmp($row_rs_fullcat['categoryID'], $row_rs_cat['categoryID']))) {echo "selected=\"selected\"";} ?>><?php echo $row_rs_fullcat['category_name']?></option>
<?php
} while ($row_rs_fullcat = mysql_fetch_assoc($rs_fullcat));
$rows = mysql_num_rows($rs_fullcat);
if($rows > 0) {
mysql_data_seek($rs_fullcat, 0);
$row_rs_fullcat = mysql_fetch_assoc($rs_fullcat);
}
?>
</select>
What you want to do, is first select (and fetch) all the selected ones from the database, and put them in a variable ($rs_cat in your case). Then, in your while loop it's a simple matter of doing an in_array() check to see if the value is selected.

Categories