How to display my output in a dropdown list - php

I want my program to list my data from mysql table in a list in a dropdown menu on my page.
Here's my code:
<fieldset>
<legend> Selecteer uw Categorie </legend>
<label for ="Categorie"> Categorie </label>
<select name ="Categorie" id="Categorie">
<datalist id ="Categorie">
<Option Value="Router">Router</option>
<Option Value="Switch">Switch</option>
<Option Value="Toestel">Toestel</option>
<Option Value="Basisstation">Basisstation</option>
<Option Value="Repeaters">Repeaters</option>
<Option Value= <?php
$con=mysqli_connect("localhost","root","admin","inventarisdb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Categorien");
while($row = mysqli_fetch_array($result))
{
echo "<ul>";
echo "<li>" . $row['Categorieen1'] . "</li>";
echo "</ul>";
}
echo "</table>";
mysqli_close($con);
?>
</option>
</select>
</datalist>
</fieldset>
THis code works perfectly, It looks up the data i need and posts it in the dropdown list But it all gets posted in a single line..
I want it to be Listed underneath each other..
Please help me!

They are all on a single line because you put your result in a single <option> tag.
try this:
<Option Value="Basisstation">Basisstation</option>
<Option Value="Repeaters">Repeaters</option>
<?php
$con=mysqli_connect("localhost","root","admin","inventarisdb");
// Check connection
if (mysqli_connect_errno())
{
echo "<option>Failed to connect to MySQL: " . mysqli_connect_error()."</option>";
}
$result = mysqli_query($con,"SELECT * FROM Categorien");
while($row = mysqli_fetch_array($result))
{
echo "<option>".$row['Categorieen1'] . "</option>";
}
mysqli_close($con);
?>
</select>
</datalist>
</fieldset>
EDIT:
sorry for the mistake I forgot the second echo in the while loop XP!

Related

Output data from database with PHP into dropdown box

I have a form designed with bootstrap style and I want to retrieve data from a database into a drop-down list. I tried this code, but I get a list with no text.
<div class="form-group"><label class="col-sm-2 control-label">Location</label>
<div class="col-sm-8"><select class="form-control m-b" name=Location>
<?php
$mysqli = new mysqli( 'localhost', 'cshrnaf_user2', '=cXlIBsdMkdr', 'cshrnaf_mis_db' );
/* check connection */
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM adhoc";
$result = mysqli_query($mysqli,$query);
while($rows = mysqli_fetch_assoc($result))
{
echo "<option value=" . $rows['Aim'] . "></option>";
echo "<option value='{".$d['Aim']."}'>".$d['Aim']."</option>";
}
?>
$d is never mentioned before you try to echo it, so what is it's content?
echo "<option value=" . $rows['Aim'] . "></option>"; has two issues: firstly, the value is not encased in quotes, and secondly, there is no content in the tag, so no text will output.
Try changing your while to:
while($rows = mysqli_fetch_assoc($result)){
echo "<option value='{$rows['Aim']}'>{$rows['Aim']}</option>";
}
As long as $rows['Aim'] contains the content you wish to output, this should work.
// cat is array fetch from database
<select name="cat" id="cat_id" style="width:170px" value="<?php echo $_POST['cat']; ?>">
<option value="0"></option>
<?php
foreach ($cat as $row) {
?>
<option value="<?php echo $row->tag_cat_id ?>" ><?php echo $row->tag_cat_name ?></option>
<?php }
?>
</select>
</select>
You should select database in your file.Above you only connect to your localhost.And you should use variable name $rows for all fetch values
if (!mysqli_select_db("mydbname")) {
echo "Unable to select database: " . mysqli_error();
exit;
}

Drop list based on another drop list

hope you fine and well,
i have a drop down list that contains categories list as follows:
<div class='form-group'>
<br/>
<label class='control-label col-md-2 'for='id_date'>Category</label>
<div class='col-md-2' class='form-group' class='col-md-11'>
<select class="form-control " id="sel1" ng-model="category" ng-init="" >
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('my');
$sql = "SELECT category FROM categories";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['category'] . "'>" . $row['category'] . "</option>";
} ?>
</select>
</div>
</div>
below this select, i have another select which is to choose element from the category as follows :
<div class='form-group'>
<br/>
label class='control-label col-md-2 ' for='id_date'>element</label>
<div class='col-md-2' class='form-group' class='col-md-11'>
<select class="form-control" id="sel12" ng-model="elemnt" ng-init="" >
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('my');
$sql = "SELECT element FROM elements where category = ";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['element'] . "'>" . $row['element'] . "</option>";
} ?>
</select>
</div>
</div>
how i can make the content of the second drop list to be based on the first drop list ?! e.g how i can put the input of the first drop list in the second SQL statement ?!
regards.
You can't do that with only php.
You must use javascript/Jquery and ajax.
Make a php script who load data from a request.
After change your first select use ajax function who call your php script with the right value and update the second select.
<select class="form-control " id="sel1" ng-model="category" ng-init="" >
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('my');
$sql = "SELECT category FROM categories";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['category'] . "'>" . $row['category'] . "</option>";
} ?>
</select>
Jquery
$("#sel1").change(function(){
$.ajax({
method: "POST",
url: "yourscript.php",
data: {myval : $(this).val()};
})
.done(function( msg ) {
//Here append your result in your second select
});
});
PHP
<?php
if(isset($_POST['myval']))
{
//SQL query where id=myval
echo $result;//result of query
}
Using pure PHP with intermediate submit:
<?php
if(isset($_POST['submitForm']) && $_POST['submitForm'] == 1){
//form is submitted by button, proceed with DB stuffs
echo 'Great, you have submitted the form, will check VALUES and do INSERT.';
}
?>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" name="aForm">
<input type="hidden" name="submitForm" id="submitForm">
<select name="category" onchange="this.form.submit();">
<option value="">Choose...</option>
<option value="1" <?=($_POST['category']==1 && !$_POST['submitForm'])?'selected':'';?>>Cat 1</option>
<option value="2" <?=($_POST['category']==2 && !$_POST['submitForm'])?'selected':'';?>>Cat 2</option>
<option value="3" <?=($_POST['category']==3 && !$_POST['submitForm'])?'selected':'';?>>Cat 3</option>
</select>
<select name="element">
<?php
if($_POST['category'] && !$_POST['submitForm']){
// SELECT from DB based on passed category ID
echo '<option value="">Now choose element...</option>';
echo '<option value="1">Elem 1</option>';
echo '<option value="2">Elem 2</option>';
echo '<option value="3">Elem 3</option>';
}else{
echo '<option value="">Choose category first...</option>';
}
?>
</select>
<input type="button" name="btnSubmit" value="Submit" onclick="document.getElementById('submitForm').value = 1; this.form.submit();">
</form>
</body>
However this is not either the best or most efficient approach, the script only demostrates how can be done without JS as is requested.

PHP/ MySQL select box and input

I begin to learn PHP because VBA is not enough.
And today I ask you to help.
I have Select box, which takes data from MySQL.
I have Input to which I would like to enter the data from the second column, the same table with that take the data to a select box and do not know how I do it.
If you want to add value from variable to input field you can do next
//$var you get from query from this your mysql table
<input type="text" name="somename" value="<?php echo $var['second_column']; ?>" id="someid">
are you asking for how to put data into a selectbox from database ?? If so, you can use this..
<select name="country">
<option value="">Select</option>
<?php
$countriesQuery=mysqli_query($conn,"SELECT id,name FROM countries");
while($countries=mysqli_fetch_assoc($countriesQuery))
{
echo "<option value='$countries[id]'>$countries[name]</option>";
}
?>
</select>
<select name="country">
<option value="">Select</option>
<?php
$countriesQuery=mysqli_query($conn,"SELECT id,name FROM countries");
while($countries=mysqli_fetch_assoc($countriesQuery))
{
echo "<option value='".$countries[id]."'>".$countries[name]." </option>";
}
?>
</select>
Hope this helps
<?php
$mysqli= new mysqli("localhost", "root", "", "db");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$result=$mysqli->query("select name from table");
/* name is your second field*/
?>
<select name="">
<?php
while($res=$result->fetch_array())
{?>
<option value="<?php echo $res[0]; ?>"><?php echo $res[0]; ?></option>
<?php
}
?>
</select>

Populate html <select> with array data from mysql in PHP

I can see the query returning results, but I can't seem to be able to put them into a html dropdown box. Also, the dropdown box has just as many entries as the query returns, but THEY ARE ALL WHITE SPACES. HOWEVER, the page source shows correct option values such as
<option value="3 John"></option>
<option value="Jude"></option>
<option value="Revelation"></option>
Can somebody help me out? Why dont they actually show in the dropdown box?
<html>
<?php
//Connect to the database
$mysqli = new mysqli("localhost", "root", "", "bible");
//Return an error if we have a connection issue
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
//Query the database for the results we want
$query = $mysqli->query("select distinct bname as Name from kjv limit 1");
//Create an array of objects for each returned row
while($array[] = $query->fetch_object());
array_pop($array);
//Print out the array results
print_r($array);
?>
<h3>Dropdown Demo Starts Here</h3>
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option->Name; ?>"></option>
</select>
<?php endforeach; ?>
Try This
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option['Name']; ?>"><?php echo $option['Name']; ?></option>
<?php endforeach; ?>
</select>
After the query is executed use the while loop to add the options to select
$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
Not sure what the array_pop is doing in the code
AS TIM WAX SAID THIS IS THE SOLUTION
$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option->Name; ?>"></option>
<?php endforeach; ?>
</select>
You ended your loop in a way that it also create <select> tag again and again. Change it and try again. I don't know much about .php but it could be a problem in showing your dropdown box.
here is mine .. im a beginner but it works for me,
$query = $mysqli->query("SELECT * FROM `student_type_db`"); //table of student type
echo "<select>";
while($row = $query->fetch_array()){
echo "<option>";
echo $row['student_type'] . " - " . $row['student_description'];
echo "</option>";
}
echo "</select>";
// student type = 1 | student description = regular
// output : 1 - regular

PHP- Fetch from database and store in drop down menu html

I can't seem to get the following code to make a dropdown menu that contains data from a mysql database. The "include('connect.php');" connects to the mysql database and I know it works on separate pages. Any suggestions?
Below is the entire code.
listCustomer
<BODY>
<H1>Find Customer's Albums Page</H1>
From a dropdown list of customers, a user should be able to pick a customer and see a list of albums (all fields in the CD table) purchased by that customer.
<HR>
<FORM ACTION="listCustomer.php" METHOD="POST"/>
Customer:
<select name="mydropdownCust">
<option value="101">101</option>
<option value="102">102</option>
<option value="103">103</option>
<option value="104">104</option>
<option value="105">105</option>
<option value="106">106</option>
<option value="107">107</option>
<option value="108">108</option>
<option value="109">109</option>
<option value="110">110</option>
</select>
<BR>
<?php
include('connect.php');
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name=dropdown value=''>Dropdown</option>";
while($r = mysql_fetch_array($result))
{
echo "<option value=$r["Cnum"]>$r["CName"]</option>";
}
echo "</select>";
?>
<BR>
<INPUT TYPE="SUBMIT" Value="Submit"/>
</FORM>
<FORM ACTION="listMenu.html" METHOD="POST"/>
<INPUT TYPE="SUBMIT" Value="Main Menu"/>
</FORM>
</BODY>
</HTML>
<?php
include('connect.php');
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=".$r['Cnum'].">".$r['CName']."</option>";
}
echo "</select>";
?>
From the looks of things, you're missing an opening option tag, so it's just outputting "Dropdown" as a line of text.
Edit
Just to be completely transparent, because I did not have connect.php, I had to add my own DB connections. My whole page looked thusly:
<?
//Adding to display errors.
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<HTML>
<HEAD>
</HEAD>
<BODY>
<H1>Find Customer's Albums Page</H1>
From a dropdown list of customers, a user should be able to pick a customer and see a list of albums (all fields in the CD table) purchased by that customer.
<HR>
<FORM ACTION="listCustomer.php" METHOD="POST"/>
Customer:
<select name="mydropdownCust">
<option value="101">101</option>
<option value="102">102</option>
<option value="103">103</option>
<option value="104">104</option>
<option value="105">105</option>
<option value="106">106</option>
<option value="107">107</option>
<option value="108">108</option>
<option value="109">109</option>
<option value="110">110</option>
</select>
<BR />
<?php
// BEGIN ADDED CONNECTION HACKY GARBAGE
$con=mysql_connect("localhost","root","root");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$selected = mysql_select_db("sample",$con)
or die("Could not select examples");
// END ADDED CONNECTION HACKY GARBAGE
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=".$r['Cnum'].">".$r['CName']."</option>";
}
echo "</select>";
?>
<BR />
<INPUT TYPE="SUBMIT" Value="Submit"/>
</FORM>
<FORM ACTION="listMenu.html" METHOD="POST"/>
<INPUT TYPE="SUBMIT" Value="Main Menu"/>
</FORM>
</BODY>
</HTML>
First off, you are missing an option opening tag, as correctly mentioned by stslavik. But this is not causing the issue here as it seems (it's auto-corrected by the browser - in my tests atleast).
Secondly, this wont work (problem causer):
echo "<option value=$r["Cnum"]>$r["CName"]</option>";
You should use
echo "<option value=".$r["Cnum"].">".$r["CName"]."</option>";
or, as I always prefer single quotes to enclose echo or print output strings:
echo '<option value='.$r['Cnum'].'>'.$r['CName'].'</option>';
Third alternative (complex syntax: What does ${ } mean in PHP syntax?)
echo "<option value={$r["Cnum"]}>{$r["CName"]}</option>";
assuming you get data from the database try this
echo "<option value={$r['Cnum']}>{$r['CName']}</option>";
try,
echo "<option value=' . $r['Cnum'] . '>' . $r['CName'] . '</option>";
instead of
echo "<option value=$r[Cnum]>$r[CName]</option>";

Categories