Search drop down list from mysql - php

I have the following in my data
Year Week
2011 19
2011 18
2011 17
2012 1
I have created a drop down list form for display the info
<script type="text/javascript">
function getWeek()
{
if (document.getElementById('y').value != '')
{
document.getElementById('d').disabled = ''; // this will enable the select
}
}
</script>
<form name="myform" action="http://www.website.com/displaybook.php" method="get">
<select size="1" name="y" id ="y" onchange="getWeek()">
<?
$sql=mysql_query("SELECT DISTINCT (Year) FROM data ORDER BY Year Desc");
while($row = mysql_fetch_array($sql))
{
echo "<option value='". $row['Year']."'>Season - ". $row['Year']."</option>";
}
?>
</select>
<select size="1" name="d" id="d" disabled="disabled">
<?
$sql=mysql_query("SELECT DISTINCT (Week) FROM data ORDER BY Week ASC");
while($row = mysql_fetch_array($sql))
{
echo "<option value='". $row['Week']."'>Week - ". $row['Week']."</option>";
}
?>
</select>
<input type="submit" value="Get data">
</form>
My problem is that i want it to display for example only week of 2012 when selected 2012 and week 2011 when selected 2011 etc...

Try removing
while($row = mysql_fetch_array($sql))
{
echo "<option value='". $row['Year']."'>Season - ". $row['Year']."</option>";
}

Related

Can't view records based on selected drop down values from two drop downs

I want to show a record from database based on the selected drop down value. I have a page which shows a drop down having column names and another drop down which shows the rows of that selected column from first drop down. After that there is a search button which shows all the records based on those two drop down menus i.e. the selected column name and selected row of that column. Then the search button shows a table having all the columns and shows that specific result.
Problem is I don't know how to use that drop down ids in the select query. Here is my code:
<form id="searchform" action="view.php" method="post" enctype="multipart/form-data" >
<select id="select1" onChange="check(this);">
<option selected value="" disabled="disabled">Select an Option</option>
<option value="all">Select All</option>
<option value="names" >Names</option>
<option value="courses" >Courses</option>
</select>
<select id="select2" disabled="disabled">
<option>Select an option</option>
<?php
$sql="SELECT DISTINCT names FROM table ";
$result = mysqli_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option class='names' value=' " . $row['names'] ."'>" . $row['names'] ."</option>";
}
?>
<?php
$sql="SELECT DISTINCT courses FROM table ";
$result = mysqli_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option class='courses' value=' " . $row['courses'] ."'>" . $row['courses'] ."</option>";
}
?>
</select>
<input type="submit" name="submit" value="Search" />
</form>
Now on view.php page
<body>
<?php
$count=1;
include("connection.php");
$select1=isset($_POST['select1']);
$select2=isset($_POST['select2']);
$result=mysql_query("SELECT names,courses FROM table WHERE names='.$_REQUEST['select2'].'");
echo "<table border='1' cellpadding='10'>";
echo "<tr><th>Names</th> <th>Courses</th></tr>";
while ($row=mysql_fetch_array($result)){?>
<tr>
<td align="center" ><?php echo $row["names"]; ?></td>
<td align="center" ><?php echo $row["courses"]; ?></td>
</tr>
<?php $count++; } ?>
</table>
</body>
</html>

Dynamically updating db with SELECT menu

Thanks for all advice! I am pulling a list of months that are not set to "current month" into a drop down/select menu in order to set some other month to be available for action. I get that list but and it populates the dropdown but then doesn't pass that value to the update. I'm either super confused or missing something very small.
Here are the queries:
if (isset($_POST['addMonth'])) {
$addmonth = $_POST['addmonth'];
//open additional months
$query_open_additional = "UPDATE reimbmonths SET reimb_open = 1 WHERE monthname = '$addmonth' ";
$query_current = "SELECT * FROM reimbmonths WHERE reimb_open = 1";
$result_current = mysql_query($query_current) or die ('Query failed: ' . mysql_error() . "<br />\n$sql");
$num_current = mysql_num_rows($result_current);
echo "Added another month: " . $addmonth;
}
And this is the HTML/PHP:
<h3>Open additional months</h3>
<form id="add-months" action="" method="post" onsubmit="window.location.reload() onchange="addMonth();"">
<select name="addmonth" id="addmonth">
<option <? if (empty($_POST["addmonth"])) { echo 'selected';} ?> disabled> --- Choose a Month --- </option>
<?
$i = 0;
while ($i < $num_months) {
$month_to_add = mysql_result($result_select_closed,$i,"monthname");
?>
<option value="<? $month_to_add ?>" <? if($_POST["addmonth"] === '$month_to_add') { echo 'selected';} ?>><? echo ucwords($month_to_add) ?></option>
<?
$i++;
}
?>
</select>
<input class="submit_btn" type="submit" name="addMonth" value="Set Month"/>
</form>

using multiple list menu and checkbox php

Am retrieving some data from the DB and am allowing users to make multiple selection via check box and also selecting a level for each selected check box. when saving, i only get to see the selected check boxes in the DB but not the level selected.
Code for making selection
include ('mysql_connect.php');
$sql = mysql_query("SELECT * FROM competency WHERE department = '$department'");
while($row = mysql_fetch_array($sql))
{
echo "<tr>";
echo "<td>";
echo"<input type='checkbox' name='comp[]' value= ".$row['id']." /> ".$row['competency']." <br /> </td>";
echo"<td> <select name='level[]'value= ".$row['id']." >
<option></option>
<option>level 1</option>
<option>level 2</option>
<option>level 3</option>
<option>level 4</option>
<option>level 5</option> </select> </td> ";
}
echo "</tr>";
?>
<input name="submit" type="submit" value="submit" />
</form>
<?php
echo" </table>";
?>
..
Code for saving into the DB
session_start();
$id = $_SESSION['user_id'];
$hobb = $_POST['comp'];
$level = $_POST['level'];
include ('mysql_connect.php');
$N = count($hobb);
for($i=0; $i < $N; $i++)
{
$var1=$hobb[$i];
$var2 = $level[$i];
//include ('connect.php');
include ('mysql_connect.php');
$table = "INSERT INTO competency_result (user_id,competency_id,level) ".
"VALUES ('$id', '$var1', '$var2')";
mysql_query($table) or die(mysql_error());
$inserted_fid = mysql_insert_id();
mysql_close();
}
Set the key the same for comp[] and level[], ie. $row['id'] -
while($row = mysql_fetch_array($sql))
{
...[your code]...
echo "<input type='checkbox' name='comp[".$row['id']."]' value= ".$row['id']." /> ".$row['competency']." <br /> </td>";
echo "<td> <select name='level[".$row['id']."]' >
...[your code]...
}
(note: the <select> does not have a value attribute. That is defined by the <option>)
Then you can easily get the corresponding values using
foreach($hobb as $key=>$val)
{
$var1=$hobb[$key];
$var2=$level[$key];
...[your code]...
}
(also, it looks like you have <tr> inside your loop, but </tr> after. I would assume you would want to move the closing tag inside the loop to properly close each row)

Refine Results with dropdown box

I would like to refine the results of mysql query using dropdown boxes... live site is here:
http://www.halfwayenterprises.com/search/tyrell.php
<form name="doublecombo" action="" method="POST">
<label for="status">Status:</label>
<label for="current">
<input class="radio_style" id="current" checked="checked" name="status" type="radio" value="current">
Current
</label>
<label for="obsolete">
<input class="radio_style" id="obsolete" name="status" type="radio" value="obsolete">
Obsolete
</label>
<label for="both">
<input class="radio_style" id="both" name="status" type="radio" value="both">
ALL
</label>
<br /><br />
<select name="category" size="1" onChange="redirect(this.options.selectedIndex)">
<option value="null">Category</option>
<option value="asset management">Asset Management</option>
<option value="budget">Budget/Finance</option>
<option value="central office">Central Office</option>
<option value="disposal">Disposal</option>
</select>
<select name="subcategory">
<option value="null">Sub-Category</option>
<option value="Portfolio">Portfolio</option>
<option value="Pricing">Pricing</option>
<option value="Valuation">Valuation</option>
<option value="Disposal">Disposal</option>
</select>
<input name="submitted" type="submit" value="GO">
<br />
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option id="title" VALUE="title">Title</option>
<Option id="poc" VALUE="poc">POC</option>
<Option id="purpose" VALUE="purpose">Purpose</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input name="submitted" type="submit" value="GO">
</form>
<select name="filter1">
<option value="az">Sort by A-Z</option>
<option value="date">Sort by Date</option>
</select>
<select name="filter2">
<option value="office">Sort by Office</option>
<option value="p">P</option>
<option value="pt">PT</option>
<option value="pf">PF</option>
</select>
<select name="filter3">
<option value="mandatory">Mandatory</option>
<option value="nonmandatory">Non-Mandatory</option>
<option value="combined">Combined</option>
</select>
</form>
</p>
<script>
<!--
/*
Double Combo Script Credit
By JavaScript Kit (www.javascriptkit.com)
Over 200+ free JavaScripts here!
*/
var groups=document.doublecombo.category.options.length
var group=new Array(groups)
for (i=0; i<groups; i++)
group[i]=new Array()
group[0][0]=new Option("Sub Category")
group[1][0]=new Option("Portfolio")
group[1][1]=new Option("Pricing")
group[1][2]=new Option("Valuation")
group[3][0]=new Option("Central Office")
group[4][0]=new Option("Disposal")
var temp=document.doublecombo.subcategory
function redirect(x){
for (m=temp.options.length-1;m>0;m--)
temp.options[m]=null
for (i=0;i<group[x].length;i++){
temp.options[i]=new Option(group[x][i].text,group[x][i].value)
}
temp.options[0].selected=true
}
function go(){
location=temp.options[temp.selectedIndex].value
}
//-->
</script>
<?
mysql_connect("localhost", "name", "pasword") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
if(isset($_POST["submitted"])){
$status = $_POST['status'];
$category = $_POST['category'];
$subcategory = $_POST['subcategory'];
echo '<div class="status_div">';
if($status=='current')
{
echo "<h2>Results</h2><p>";
$res = mysql_query("SELECT * FROM material WHERE status='$status' AND category='$category' AND subcategory='$subcategory' ORDER BY `documentid` ASC");
while ($row = mysql_fetch_assoc($res)) {
echo '<div class="current">';
echo $row ['documentid'].' - '.$row['category'].' - '.$row['title'].' - '.$row['status'];
echo '</div>';
echo '<br>';
}
} if ($status=='obsolete')
{
echo "<h2>Results</h2><p>";
$res = mysql_query("SELECT * FROM material WHERE status='$status' AND category='$category' ORDER BY `documentid` ASC ");
while ($row = mysql_fetch_assoc($res)) {
echo $row ['documentid'].' - '.$row['category'].' - '.$row['title'].' - '.$row['status'];
echo '<br>';
}
} if ($status=='both')
{
echo "<h2>Results</h2><p>";
$res = mysql_query("SELECT * FROM material WHERE status1='both' AND category='$category' ORDER BY `documentid` ASC");
while ($row = mysql_fetch_assoc($res)) {
echo '<div class="">';
echo $row ['documentid'].' - '.$row['category'].' - '.$row['title'].' - '.$row['status'];
echo '</div>';
echo '<br>';
}
}
echo '</div>';
}
$field = #$_POST['field'] ;
$find = #$_POST['find'] ;
$searching = #$_POST['searching'] ;
$status = $_POST['status'];
//This is only displayed if they have submitted the form
if ($searching =="yes")
{
echo "<h2></h2><p>";
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<a href='tyrell.htm'>Return</a>";
exit;
}
// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
//Now we search for our search term, in the field the user specified
if($status=='current'){
$data = mysql_query("SELECT * FROM material WHERE status='$status' AND lower($field) LIKE'%$find%' LIMIT 0,30");
while ($row = mysql_fetch_assoc($data)) {
echo '<div class="current">';
echo $row ['documentid'].' - '.$row['category'].' - '.$row['title'].' - '.$row['status'];
echo '</div>';
echo '<br>';
}
}
if ($status=='obsolete'){
$data = mysql_query("SELECT * FROM material WHERE status='$status' AND lower($field) LIKE'%$find%' LIMIT 0,30");
while ($row = mysql_fetch_assoc($data)) {
echo '<div class="obsolete">';
echo $row ['documentid'].' - '.$row['category'].' - '.$row['title'].' - '.$row['status'];
echo '</div>';
echo '<br>';
}
} if ($status=='both'){
$data = mysql_query("SELECT * FROM material WHERE status1='both' AND lower($field) LIKE'%$find%' LIMIT 0,30");
while ($row = mysql_fetch_assoc($data)) {
echo '<div class="">';
echo $row ['documentid'].' - '.$row['category'].' - '.$row['title'].' - '.$row['status'];
echo '</div>';
echo '<br>';
}
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
<p align="center"><font face="arial" size="-2">This free script provided by</font><br>
<font face="arial, helvetica" size="-2"><a href="http://javascriptkit.com">JavaScript
Kit</a></font></p>'
</body>
</html>
I have can't think of how to use the drop downs without having a submit button
I think I know what you need...
Use jquery to submit form on you page.
Something like this...
$("submitted").click(function() {
var url = "file.php";
$.ajax({
type: "POST",
url: url,
data: $("doublecombo").serialize(), // serializes the form's elements.
success: function(data)
{
// populate box or div with your result
}
});
return false; // avoid reload.
});
//PHP part
if($_POST['someField']){
$result = //Do query and staff and return any type of resposne you will handle in ajax success
echo $result;
exit;
}
You can use JavaScript to manipulate the content of the dropdown list. All you need is to do is bind the click even on the radio button and when one of them gets clicked, use an Ajax call to retrieve the information from your SQL database and modify the content of your selectbox.
EDIT
Here's some code you can add to your page:
$('form[name=doublecombo] input').change(function() {
$('form[name=doublecombo]').submit();
});
Note that change might not work. If it doesn't, try using click instead.

Search form with php

I get the following form my database which contain for year 2011 week 1 to week 20 and for year 2012 currently week 1. I want my user to choose 2011 first and then call the week to choose for example so that they dont choose for example week 10 and year 2012( which are not yet available).Any help most welcome.
<form name="myform" action="http://www.website.com/displaybook.php" method="get">
<select size="1" name="d">
<?
$sql=mysql_query("SELECT DISTINCT (Week) FROM data ORDER BY Week ASC");
while($row = mysql_fetch_array($sql))
{
echo "<option value='". $row['Week']."'>Week - ". $row['Week']."</option>";
}
?>
</select>
<select size="1" name="y">
<?
$sql=mysql_query("SELECT DISTINCT (Year) FROM data ORDER BY Year Desc");
while($row = mysql_fetch_array($sql))
{
echo "<option value='". $row['Year']."'>Season - ". $row['Year']."</option>";
}
?>
</select>
<input type="submit" value="Get data">
</form>
You need to put the year select before the day select... It will ajust the GET string automaticaly... Then, you create a little javascript to enable the second select when the first is selected, like this:
<script type="text/javascript">
function getWeek()
{
if (document.getElementById('y').value != '')
{
document.getElementById('d').disabled = ''; // this will enable the select
}
}
</script>
And in your HTML:
<form name="myform" action="http://www.website.com/displaybook.php" method="get">
<select size="1" name="y" onchange="getWeek()">
<?
$sql=mysql_query("SELECT DISTINCT (Year) FROM data ORDER BY Year Desc");
while($row = mysql_fetch_array($sql))
{
echo "<option value='". $row['Year']."'>Season - ". $row['Year']."</option>";
}
?>
</select>
<select size="1" name="d" id="d" disabled="disabled">
<?
$sql=mysql_query("SELECT DISTINCT (Week) FROM data ORDER BY Week ASC");
while($row = mysql_fetch_array($sql))
{
echo "<option value='". $row['Week']."'>Week - ". $row['Week']."</option>";
}
?>
</select>
<input type="submit" value="Get data">
</form>
Something like this should work great!

Categories