Dropdown menu php with next/previous button - php

I have :
<select name="department" id="department" onchange="this.form.submit()">
<?php
do {
?>
<option value="<?php echo $row_department['dept_Name'] ?>"><?php echo $row_department['dept_Name'] == $department ? 'selected' : ''?><?php echo $row_department['dept_Name']
?></option>
<?php
} while ($row_department = mysql_fetch_assoc($department));
$rows = mysql_num_rows($department);
if($rows > 0) {
mysql_data_seek($department, 0);
$row_department = mysql_fetch_assoc($department);
}
?>
</select>
For the dropdown from the database named department.
Once dropdown selected, multiple file sets will viewed from database such as id, name, dept_name with next/previous button.
The problem is, whenever i clicked next/previous button, there no files viewed.
Dropdown must be selected for the 2nd time for view 2nd file set.
What should I do and how? I'm using Dreamweaver CS6.

Try this. You canno't search because you don't have a query to do that.
$deptName = $row['dept_Name'];
$sql = mysql_query("SELECT * FROM (table_name) WHERE (column_department_name) = '$deptName'");
while ($row_department = mysql_fetch_assoc($sql));
$rows = mysql_num_rows($row_department);
if($rows > 0) {
echo $rows['(your_column_name)'];
}else{
echo 'nothing';
}

Related

Populate HTML form drop down from database

I have been trying to populate a drop down menu by pulling the needed information from a database through php and mysql however I haven't been able to achieve this and so far when I run it I just get a drop down box with nothing inside of it. Any help on this would be great as I am really stuck as to where I am going wrong.
<select id="dung-name-select" name="dung-select">
<option name="" disabled selected hidden>Name</option>
<option value="*">All</option>
<?php
//Database Query
$query = "SELECT Name FROM dungeon";
$result = mysqli_query($connection, $query);
if (!$result) {
die("Database query failed.");
}
var_dump($result);
while($row = mysqli_fetch_assoc($result)) {
?>
<option value="<?= $row['Name']; ?>"><?= $row['Name']; ?></option>;
<?php
}
?>
</select>
I have connected to the database fine and can pull information down from it to populate a table for example however the drop down menu just isn't working.
NB: I need the value to be set to the name that I am also populating the drop down menu with.
your code works for me, i tried something different hope it works for you.
<select id="dung-name-select" name="dung-select">
<option name="" disabled selected hidden>Name</option>
<option value="*">All</option>
<?php
$result = $connection->query("SELECT Name FROM dungeon");
if ($result) {
while($row = $result->fetch_assoc()) {
?>
<option value="<?= $row['Name']; ?>"> <?= $row['Name']; ?></option>;
<?php
}
}
else {
echo "No dungeons found";
}
?>
</select>

listbox value goes 0 in database if not selected on edit mode in php

I have three tables
City: city_id, city_name
State: State_id, State_name
News: City_id, State_id, Headline, Story, Author etc.
I am Displaying data in textbox and listbox on edit button click Of Selected ID.
Data is displaying in listbox properly of selected id but when i click save button without change of listbox value it goes 0 in database.
If I don't change the listbox value and click save then it inserting 0 from listbox.
<?php
$data = 0;
if(isset($_GET['edit']))
{
$id = clean($_GET['edit']);
mysql_set_charset('utf8');
$sql="SELECT city_name,state_name,category_name,headline,author,story,source,photo,date from news left join
city on news.city_id=city.city_id left join state on news.state_id=state.state_id left join category on news.cat_id=category.category_id where id = '$id'";
$result=mysql_query($sql);
$data=mysql_fetch_array($result);
}
?>
<?php
$cat = $Admin->select($Admin->cat_table,'','','');
?>
<select name="cat_id" class="select" required="">
<option value="<?php if(isset($_GET['edit'])){ echo $data['category_name']; }?>"><?php if(isset($_GET['edit'])){echo $data['category_name'];}?></option>
<?php
foreach($cat as $load_category)
{
?>
<option value="<?php echo $load_category["category_id"]; ?>"><?php echo $load_category["category_name"]; ?></option>
<?php }?>
</select>
<?php
$errors = array();
$Admin = new admins;
if(isset($_POST['save']))
{
$table = $Admin->news_table;
if(isset ($_GET['edit']))
{
$id = clean($_GET['edit']);
$cond = "id = '$id'";
if($Admin->save_advance($table,$_POST,'',$cond))
{
$_SESSION['message'] = "News Updated Successfully";
header("Location:add_news.php");
exit;
}
}
}
?>
What am I doing wrong?
Tell me if I'm wrong but this line
<option value="<?php if(isset($_GET['edit'])){ echo $data['category_name']; }?>">
<?php if(isset($_GET['edit'])){echo $data['category_name'];}?>
</option>
seems strange to me, it sets the category name as value for first option in the select. Basically, if you don't change the listbox, you are trying to update a row where the category_id is a category_name.
This should be:
<option value="0">
<?php if(isset($_GET['edit'])){echo $data['category_name'];}?>
</option>
But if you have to update something you can't do that without a value. So add some kind of validation that forbids to click the save button without setting a value in the select, or just do something like this (adds a default category to the listbox)
<select name="cat_id" class="select" required>
<?php
$counter = 0;
foreach($cat as $load_category){ ?>
<option <?php echo ($counter == 0) ? 'selected' : ''; ?> value="<?php echo $load_category["category_id"]; ?>"><?php echo $load_category["category_name"]; ?></option>
<?php
//increments counter
$counter++; }?>
</select>

combo box posting selection not value not other page submit

I have a Select/Combo Box that I use to select the number of records to display on a page. In order to cover the 'All' option I pass the ID and the Number Selected in the value field.
<?php //get info for results per page combo box //
$stmt = $db->prepare("SELECT resultspp.ID, resultspp.NumberResults FROM resultspp");
$stmt->execute();
$rows2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows2 as $row) {
if ($pageID !=$row['ID']) { ?>
<option value="<?php echo $row['ID']; ?>|<?php echo $row['NumberResults']; ?>"><?php echo $row['NumberResults']?></option>
<?php } else { ?>
<option value"<?php echo $row['ID'];?>|<?php echo $row['NumberResults']; ?>" selected><?php echo $row['NumberResults']?></option>
<?php }
}
?>
This all works fine and the correct values are posted, checked using print_r($_POST). However when another buttom is submitted on the page it posts the option displayed to the user, not the value in the value tag. I have checked this by change the selected option to a constant.
The values are unpacked like so after the post.
<?php
if($_POST['resultsPP']){
$pageresult = explode('|', $_POST['resultsPP']);
$pageID = $pageresult[0];
if ($pageresult[1] == 'All'){
$number_select = $totalRecords;
} else {
$number_select = $pageresult[1];
}
}
?>
Thanks in advance
You have missed "=" sign in your code. Below is the updated code. This causing the posting of option instead of posting of value.
.........
else { ?>
<option value="<?php echo $row['ID'];?>|<?php echo $row['NumberResults']; ?>" selected><?php echo $row['NumberResults']?></option>
<?php }
.......

PHP : Get the value of Dropdown and pass it to another dropdown

I have a problem right now [PHP]. I have a dropdown and its loading my database for the first page, when I proceed to the next page it also have a dropdown where its also loading the my database and also I can get the value of my dropdown in the first page using an echo only.
This is the scenario:
I choose in the dropdown first page the "Letter A" and when I click the button it will proceed to the next page. The dropdown in the second page loaded the items in the database but instead of "-select-" is the first index in the dropdown I want is "Letter A" will be the first index.
This is my code in first page for drowdown:
<select name="id">
<option value="" >- select -</option>
<?php
include 'connect.php';
$q = mysql_query("select fldNetname from tblnetwork");
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1[fldNetname]."'>".$row1[fldNetname]."</option>";
}
?>
</select>
and this is my code in second page for dropdown:
if ($get_ID != "")
{
echo "<br/>";
echo $get_ID;
//echo "show()";
}
else
{
echo "No Network Selected";
echo "<br/>";
//echo "hide()";
}
?>
<option value="">- select -</option>
<?php
include 'connect.php';
$q = mysql_query("select fldNetname from tblnetwork");
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1[fldNetname]."'>".$row1[fldNetname]."</option>";
}
?>
</select>
Thanks in advance!
On your second page, you need to check whether the value is the same as the one you received from the first page:
echo "<option value='".$row1[fldNetname]."' " . (($row1[fldNetname] == $get_id)?"selected":"") . ">".$row1[fldNetname]."</option>";
if $get_ID, is actually your $_GET['id'] value, then just do...
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1['fldNetname']."'";
if($row1['fldNetname']==$get_ID){echo "selected='selected'";}
echo ">".$row1['fldNetname']."</option>";
}
Or as one line...
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1['fldNetname']."' " . (($row1['fldNetname'] == $get_ID)?"selected='selected'":"") . ">".$row1'[fldNetname']."</option>"
}
Have you tried?
changing (on the second page)
<option value="">- select -</option>
to
<option value="<?php echo $yourVar; ?>"><?php echo $yourVar; ?></option>
Then in the while loop, skip the value selected to prevent a duplicate choice.
Something like:
while ($row1 = mysql_fetch_array($q))
{
if($yourVar != $row1[fldNetname]){
echo "<option value='".$row1[fldNetname]."'>".$row1[fldNetname]."</option>";
}
}

How to show selected value of dropdown list from database in php

How do I show the selected value of a dropdown list from my mysql database. The dropdown list is dependent to my Category dropdown list. These are the codes:
<?php $id = $_GET["id"];
if(!$pupcon){
die(mysql_error());
}
mysql_select_db($database_pupcon, $pupcon);
$getDropdown2 = mysql_query("select * from tblitemname where CategoryID = $id");
while($row = mysql_fetch_array($getDropdown2)){
echo "<option value=\"".$row["ItemID"]."\">".$row["Item_Name"]."</option>";
} ?>
Here are the codes for the first dropdown list (Category) which populates the Item Name dropdown.
<select name="Category" id="Category" class="field select large" onChange="loadXMLDoc(this.value);">
<?php do { ?>
<option value="<?php echo $row_Recordset1['CategoryID']?>"<?php if (!(strcmp($row_Recordset1['CategoryID'], $row_editRecordset['CategoryID']))) {echo "selected=\"selected\"";} ?>><?php echo $row_Recordset1['CategoryName']?></option>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); $rows = mysql_num_rows($Recordset1); if($rows > 0) {
mysql_data_seek($Recordset1, 0);
$row_Recordset1 = mysql_fetch_assoc($Recordset1);}?>
</select>
While you're listing out the drop down options, you can check to see if the current ItemID matches the passed id value. If it matches, throw a selected="selected" in there. Try:
$selected = ($row['ItemID'] == $id);
echo "<option value=\"".$row["ItemID"]."\" ".($selected ? " selected=\"selected\"":"").">".$row["Item_Name"]."</option>";
EDIT
I tried to clean up the code some...not sure why you're using a do...while because $row_Recordset1 wouldn't be available on the first iteration.
<select name="Category" id="Category" class="field select large" onChange="loadXMLDoc(this.value);">
<?php
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) {
?>
<option value="<?php echo $row_Recordset1['CategoryID']; ?>"<?php if (!(strcmp($row_Recordset1['CategoryID'], $row_editRecordset['CategoryID']))) { echo " selected=\"selected\""; } ?>>
<?php echo $row_Recordset1['CategoryName']; ?>
</option>
<?php
}
?>
</select>
you can use this code inside while
$selected=$row["ItemID"]==$id ?'Selected':'';
echo "<option value=\"".$row["ItemID"]."\" {$selected} >".$row["Item_Name"]."</option>";;

Categories