I have an option box that filters the data to be displayed on the page. My data table has information that pertains to a cycle. $row_Recordset2 is just a way for me to view the unique cycles in my table (e.g. Spring, Fall, Winter, Spring) and is the filter in the option box.
SELECT DISTINCT gpa.`Cycle'
FROM gpa
ORDER BY gpa.ID DESC
So my option box would give me my Cycle and only show that data.
<select name="selCycle" id="selCycle" onchange="formFilter.submit()">
<option value="%">all cycles</option>
<?php
do {
?>
<option value="<?php echo $row_Recordset2['Cycle']?>"<?php if
($varCycle_Recordset3 == $row_Recordset2['Cycle']) {echo 'selected';} ?>><?
php echo $row_Recordset2['Cycle']?></option>
<?php
} while ($row_Recordset2 = mysql_fetch_assoc($Recordset2));
$rows = mysql_num_rows($Recordset2);
if($rows > 0) {
mysql_data_seek($Recordset2, 0);
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
}
?>
</select>
Right now the data is filtered correctly but I always have to display all the data (All Cycles) and then select the Cycle I would like to view.
$varCycle_Recordset3 = "%";
if (isset($_POST['selCycle'])) {
$varCycle_Recordset3 = $_POST['selCycle'];
}
mysql_select_db($database_ss, $ss);
$query_Recordset3 = sprintf("SELECT gpa.ID, gpa.Data,
FROM gpa
WHERE gpa.Cycle LIKE %s
GetSQLValueString($varCycle_Recordset3, "text")
I would prefer that the latest ID be the default for the page with the user being able to switch Cycles. I'm sure it has to do with the default value set to % or maybe I am using the Where statement incorrectly with gpa.Cycle LIKE %s. I've tried gpa.Cycle = %s instead of LIKE and then no data displays on page load. I of course would like the option box to display the current Cycle and match the Cycle in the option box.
Related
I am currently trying to achieve an assignable "bin" system for managing parts within a small warehouse. I am trying to achieve the following;
There is availability of 100 bins I want to list in a select drop-down, and I want to list all 100 - as well as wanting to show already assigned bins - but have them disabled by utilising a select box with options The list of already assigned bins has to be achieved by first checking the database, but this is where i am having issues.
I have nearly got there, but feel like i'm probably approaching the wrong way, and can't seem to get my head around the checking against the database once within the loop.
I have tried the below code to try and achieve this; but the issue i am having is checking against the database for the value to disable the selectables. $partbin is assigned earlier in the code.
<select name="part_bin">
<option value="0">No Bin</option>
<?php
// Assign Part Bin
// Start 1-100
for ($i=1; $i<=100; $i++) {
// First Select from database
$sql = "SELECT * FROM parts WHERE part_bin != NULL OR part_bin != 0";
$result = $conn->query($sql);
$nlcheck = $result->fetch_assoc();
$nlbin = $nlcheck["part_bin"];
// Start Option loop
?>
<option value="<?php echo $i;?>"<?php if($partbin == $i) { echo " selected"; } ?><?php if ($nlbin == $i) { echo " disabled"; }?>>Part Bin <?php echo $i;?></option>
<?php
}
?>
</select>
Part bins already assigned within the parts table, should be visible but not selectable via being disabled in the dropdown.
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.
I have a list box and have 2 questions.
1st how can i still have the text to say Select a country?
2nd on my update page it displays the proper country but will not allow it to be change for it will not list any of the other countries in edit page
<?php
$sql = "SELECT countries.country_id, countries.country_name, users.user_country FROM countries, users
WHERE users.user_country = countries.country_id";
$result = query($sql);
?>
<select class="form-control input-lg box" id="country" name="country">
<?php
$i = 0;
while (($row = mysqli_fetch_assoc($result)) != false) {
?>
<option value="<?=$row["country_id"];?>"><?=$row["country_name"];?></option>
<?php
$i ++;
}
?>
Well, to have a default option not provided by your database call, you just need an HTML statement such as
<option value="">Select a country</option>
right below your <select> statement.
And we can't see your table definitions, but your SQL code seems to be picking the specific country that the user is associated with, so that there is only one country in the list. You probably need two queries: One that obtains the user's current country, and a second that obtains the entire list of countries.
After the first query, save the user's current countryid in a variable - say, $usercountryID.
Then, in your loop through the countries, compare each countryID to the user's country, like this:
while (($row = mysqli_fetch_assoc($result)) != false) {
if ($usercountryID == $row["country_id"]) $selected = " selected";
else $selected = "";
echo "<option value='{$row['country_id']}$selected>{$row['country_name']}</option>\n";
}
$selected could be defined many ways, of course, including in a conditional assignment statement. I omitted the $i counter, which isn't needed here, but, of course, you might need it later.
I have a simple list-type php page, which lists items according to a mysql query, like:
mysql_select_db($database_connBHN, $connBHN);
$query_rsMarket = "SELECT * FROM my_items WHERE active=1 ORDER BY name asc";
$rsMarket = mysql_query($query_rsMarket, $connBHN) or die(mysql_error());
$row_rsMarket = mysql_fetch_assoc($rsMarket);
$totalRows_rsMarket = mysql_num_rows($rsMarket);
then the page list these items and their description in separate tables.
Initially this page lists these items in alphabetical order. Now I would like to put a drop down box on the top of the page, where the user could choose another two or three more sorting options, like date, or itemId, etc, which values are stored in the database.
How could I solve this in a simple way, without leaving that page? (i.e. I do not want to create separate pages for each different result set)
No, it's easier to keep this as a single script and just allow for the sorting variable to be switched. For security's sake, it's best to limit the user input to a per-defined set of options in the PHP script:
$sort_options = array('name asc','name desc','dateadded asc','dateadded desc');
if(!isset($_GET['field'])){
$_GET['field'] = 'name';
}
if (!isset($_GET['order'])){
$_GET['order'] = 'asc';
}
$full_query_sort = $_GET['field'].' '.$_GET['order'];
if (!in_array($full_query_sort,$sort_options)){
die('invalid selection');
}
mysql_select_db($database_connBHN, $connBHN);
$query_rsMarket = "SELECT * FROM my_items WHERE active=1 ORDER BY ".$full_query_sort;
$rsMarket = mysql_query($query_rsMarket, $connBHN) or die(mysql_error());
$row_rsMarket = mysql_fetch_assoc($rsMarket);
$totalRows_rsMarket = mysql_num_rows($rsMarket);
Now you can just have the order set with _GET variables: http://example.com/page.php?field=name&order=desc etc. This can be set with javascript (or on form submission) using dropdowns:
<select id='field_select'
name='field'
onchange="window.location='?field='+this.value+'&order='+document.getElementById('order_select').value;">
<option value='name' <?php if(!isset($_GET['field']) || $_GET['field']=='name'){echo "selected";} ?>>Sort by Name</option>
<option value='dateadded' <?php if(isset($_GET['field']) && $_GET['field']=='dateadded'){echo "selected";} ?>>Sort by Date Added</option>
</select>
<select id='order_select'
name='order'
onchange="window.location='?field='+document.getElementById('field_select').value+'&order='+this.value;">
<option value='asc' <?php if(!isset($_GET['order']) || $_GET['order']=='asc'){echo "selected";} ?>>Ascending/option>
<option value='desc' <?php if(isset($_GET['order']) && $_GET['order']=='desc'){echo "selected";} ?>>Decending</option>
</select>
I am writing a reservation system. On the main page I would give a choice of category, viewed the equipment available for booking.
For example I have code like this:
<select>
<option value = "a">A</option>
<option value = "b">B</option>
<option value = "c">C</option>
<option value = "d">D</option>
<option value = "e">E</option>
</select>
I wish that each choice was associated with a separate query to the database, and that was the result of a query dynamically displayed on the screen.
It would be great if you could show me some sample code.
Regards
$query = mysql_query("SELECT * FROM choices");
while($row=mysql_fetch_assoc($query)) {
echo '<option value="'.$row['value'].'">'.$row['value'].'</option>';
}
If you need separate query for each choice the code doesns't change much:
$query = mysql_query("(SELECT * FROM choices) UNION (SELECT * FROM choices1) [etc]");
while($row=mysql_fetch_assoc($query)) {
echo '<option value="'.$row['value'].'">'.$row['value'].'</option>';
}
There are two parts to your question; 1 - Detecting which query to run and 2 - Displaying the results dynamically.
Part 1: Detecting which query to run:
Given hard-coded choices and no parameters for the query, using your above code, you can determine which query to run using the following:
For the HTML part, as part of a form, create the select as you did above (but with a name)
<select name="querySelect">
<option value="a">A</option>
<option value="b">B</option>
</select>
And in the PHP:
$querySelect = $_GET['querySelect'];
switch($querySelect)
{
case 'a':
$sql = "SELECT * FROM TableA";
break;
case 'b':
$sql = "SELECT * FROM TableB";
break;
}
$results = mysql_query($sql);
Part 2: Displaying the results dynamically
With the $results, what you do with the data very much depends on what you want to achieve. At a very basic level, you can do the following to dynamically display a table of the results:
if(mysql_num_rows($results) > 0)
{
$header = false;
print "<table>"
while($row = mysql_fetch_assoc($results))
{
if(!$header)
{
$headings = array_keys($row);
print "<tr>";
for($i=0;$i<count($headings);$i++)
{
print "<th>".htmlspecialchars($headings[$i])."</th>";
}
print "</tr>";
$header = true;
}
print "<tr>";
foreach($row as $value)
{
print "<td>".htmlspecialchars($value)."</td>";
}
print "</tr>";
}
print "</table>"
}
else print "<h1>No Results Found!</h1>";
mysql_free_result($results);
There is still alot not covered in my answer because I can't say what level of detail is required. You will also need to cover things like your connection to MySQL, error handling, formatting of the table...
Update
Hmmm, very interested to know why this has been downvoted. If someone can please explain in comments where I have misinterpreted the question or misguided the user, I would appreciate it.
If you use jQuery the code might look like
<select id="select_id">
<option value = "a">A</option>
<option value = "b">B</option>
<option value = "c">C</option>
<option value = "d">D</option>
<option value = "e">E</option>
</select>
<script type="text/javascript">
$('#select_id').change(function(e) {
// this code send selected value to the server
$.post('url', {selected_value: this.value}, function(response) {
// handle the server's response
});
});
</script>
On server side take the value from $_POST and make a query. And remember - do not trust to data from client-side. You never know who is over there. Always check incoming data and DO NOT use such constructions
$sql = "SELECT * FROM table_name WHERE name = '{$_POST['selected_value']}'";
because there might be any string including those can drop all databases, clear data and so forth.