I want to make multiple dropdowns from data out of my mysql database. I want 4 dropdowns to be exact. This is what I have at this moment:
<?php
mysql_connect('#', '#', '#');
mysql_select_db('test');
$sql = "SELECT wie, waar, metwie, voeruig FROM data";
$result = mysql_query($sql);
echo "<select name='test'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['wie'] . "'>" . $row['wie'] . "</option>";
}
echo "</select>";
?>
For instance, this:
mysql_connect('#', '#', '#');
mysql_select_db('test');
$sql = "SELECT wie FROM data";
$result = mysql_query($sql);
echo "<select name='test1'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['wie'] . "'>" . $row['wie'] . "</option>";
}
echo "</select>";
$sql = "SELECT waar FROM data";
$result = mysql_query($sql);
echo "<select name='test1'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['waar'] . "'>" . $row['waar'] . "</option>";
}
echo "</select>";
$sql = "SELECT metwie FROM data";
$result = mysql_query($sql);
echo "<select name='test2'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['metwie'] . "'>" . $row['metwie'] . "</option>";
}
echo "</select>";
$sql = "SELECT voeruig FROM data";
$result = mysql_query($sql);
echo "<select name='test3'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['voeruig'] . "'>" . $row['voeruig'] . "</option>";
}
echo "</select>";
?>
As stated by HawasKaPujaari, avoid using mysql. Use mysqli. You could use a conditional switch statement like this:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT wie, waar, metwie, voeruig FROM data")) {
printf("Select returned %d rows.\n", $result->num_rows);
while ($row = mysql_fetch_array($result)) {
switch ($row) {
case "wie":
echo "<select name='wie'>";
echo "<option value='" . $row['wie'] . "'>" . $row['wie'] . "</option>";
echo "</select>";
break;
case "waar":
echo "<select name='waar'>";
echo "<option value='" . $row['waar'] . "'>" . $row['waar'] . "</option>";
echo "</select>";
break;
case "metwie":
echo "<select name='metwie'>";
echo "<option value='" . $row['metwie'] . "'>" . $row['metwie'] . "</option>";
echo "</select>";
break;
}
case "voeruig":
echo "<select name='voeruig'>";
echo "<option value='" . $row['voeruig'] . "'>" . $row['voeruig'] . "</option>";
echo "</select>";
break;
}
/* free result set */
$result->close();
}
?>
php and mysql are different softwares. what you are doing here is connecting php with mysql using mysql_*() functions
in your case what you get is an array in php. you can use this array for whatever you want. if you want to print array as is as for debugging use:
echo "<pre>";
print_r($row);
echo "</pre>";
From this you will get array structure
then you ca use different dropdowns for array elements
Note: mysql_*() is not safe. Use mysqli_* or PDO.
Try code below. hope it help you
<?php
$wie=array();
$waar =array();
$metwie =array();
$voeruig =array();
while ($row = mysql_fetch_array($result)){
$wie[]=$row["wie"];
$waar[]=$row["waar"];
$metwie[]=$row["metwie"];
$voeruig[]=$row["voeruig"];
}
?>
<select name="wie">
<?php
foreach($wie as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?>
<php
}
?>
</option>
<select name="waar">
<?php
foreach($waar as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?> </option>
<php
}
?>
</select>
<select name="metwie">
<?php
foreach($metwie as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?></option>
<php
}
?>
</select>
<select name="voeruig">
<?php
foreach($voeruig as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?> </option>
<php
}
?>
</select>
Related
I have some code that builds a select box. For the first select box; The selected option has a value of 1 then display it as selected then list everything else that doesn't hold the same value. Works good except the second box doesn't work. It obviously doesn't like me duplicating then changing the values. The second select box is looking for any values with 2. A third one would be a value of 3 etc.
Any way I can get the two of them working together?
<select name="first-box">
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if($row['my_value']==1) {
echo "<option value='1' selected>" . $row["title"] . "</option>";
} else if ($row['my_value']!==1) {
echo "<option value='1'>" . $row["title"] . "</option>";
}
}
}
?>
</select>
<select name="second-box">
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if($row['my_value']==2) {
echo "<option value='2' selected>" . $row["title"] . "</option>";
} else if ($row['my_value']!==2) {
echo "<option value='2'>" . $row["title"] . "</option>";
}
}
}
?>
</select>
This might help
<?php
if ($result->num_rows > 0) {
mysqli_data_seek($result,0);
while($row = $result->fetch_assoc()) {
if($row['my_value']==2) {
echo "<option value='2' selected>" . $row["title"] . "</option>";
} else if ($row['my_value']!==2) {
echo "<option value='2'>" . $row["title"] . "</option>";
}
}
}
?>
If you are going to have more than one or two, then reading the data into an array and looping through that as needed will be better/easier
<?php
if ($result->num_rows > 0) {
$data=$result->fetch_all(MYSQLI_ASSOC);
}
// stop php and do the <select> tag here or print() it
foreach($data as $row) {
if($row['my_value']==1) {
echo "<option value='1' selected>" . $row["title"] . "</option>";
} else if ($row['my_value']!==1) {
echo "<option value='1'>" . $row["title"] . "</option>";
}
}
// stop php or print() a </select>
// stop php and do the <select> tag here or print() it
foreach($data as $row) {
if($row['my_value']==2) {
echo "<option value='2' selected>" . $row["title"] . "</option>";
} else if ($row['my_value']!==1) {
echo "<option value='2'>" . $row["title"] . "</option>";
}
}
// stop php or print() a </select>
Though I have researched, I couldn't find any solution for this. I need to get the database value ("Default") as the pre-selected value of the drop down list.
<select name="listCustomer" id="listCustomer">
<?php
$sql = mysqli_query($connection,"SELECT customer_name FROM customers");
while ($row = mysqli_fetch_array($sql,MYSQLI_ASSOC)){
echo "<option value=\"" . $row['customer_name'] . "\">" . $row['customer_name'] . "</option>";}
?>
</select>
Can you please help me on this?
Just create a variable before the echo, something like:
$selected = ((strtolower($row['customer_name']) == 'default') ? 'selected' : '');
then change the echo to this:
echo '<option '.$selected.' value="'.$row['customer_name'].'">'.$row['customer_name'].'</option>';
This can be accomplished using an if statement on the customer_name
$sql = mysqli_query($connection,"SELECT customer_name FROM customers");
while ($row = mysqli_fetch_array($sql,MYSQLI_ASSOC)){
if($row["customer_name"] === "Default"){
echo "<option value=\"" . $row['customer_name'] . "\" selected>" . $row['customer_name'] . "</option>";
} else {
echo "<option value=\"" . $row['customer_name'] . "\">" . $row['customer_name'] . "</option>";
}
}
?>
Note the selected tag on the first echo.
$query="SELECT customer_name FROM customers";
$result = #mysql_query ($query);
echo "<select name=customer_name value=' '>";
while($drop=#mysql_fetch_array($result)){
echo "<option value=$drop[customer_name]>$drop[customer_name]</option>";
}
echo "</select>";
I have a dropdown in php the value is coming from database. In the edit mode i need to show selected value in to drop down. My php is below:
<?php
echo "<td width='5%'>";
$sql_currency = "SELECT * FROM currency1";
$result_currency = mysql_query($sql_currency);
echo "<select id='currency_change$i' >";
while ($row_currency = mysql_fetch_array($result_currency)) {
echo "<option value=" . $row_currency['currency'] . " data-price=" . $row_currency['rate'] .">" . $row_currency['currency'] ."</option>";
}
echo "</select>";
echo "</td>";
?>
Try this code...
<?php
echo "<td width='5%'>";
$sql_currency = "SELECT * FROM currency1";
$result_currency = mysql_query($sql_currency);
echo "<select id='currency_change$i' >";
/*
* selected value
*/
$selectedValue = ""; // assign that value to this variable
while ($row_currency = mysql_fetch_array($result_currency)) {
$selected = "";
if($row_currency['currency'] == $selectedValue){
$selected = ' selected="selected" ';
}
echo "<option ".$selected." value=" . $row_currency['currency'] . " data-price=" . $row_currency['rate'] . ">" . $row_currency['currency'] . "</option>";
}
echo "</select>";
echo "</td>";
?>
I have the following code which provides a drop down list of all the rows in that specific table, this works fine. The code is below:
<?php
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT ID, NAME FROM b_sonet_group ORDER BY ID DESC");
echo "<select>";
echo "<option value=''>Select Your Project</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID'] . "'>" . $row['NAME'] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>
I now want a second drop down list that is determined by what ever is selected in the one above based on the ID. So, I want something like:
$result2 = mysqli_query($con,"SELECT ID, ALBUM_NAME FROM a_different_table WHERE ID=ID_FROM_QUERY_ABOVE");
echo "<select>";
echo "<option value=''>Select Your Album</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID'] . "'>" . $row['ALBUM_NAME'] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>
I basically want to get the ID from the first drop down to provide the results in the second dropdown. Can this be done?
You can't "only" do this with Ajax, but you should do it with Ajax.
PHP way (not suggested, and untested). Basically use isset and if it is, more will be added to the form. The POST from the select, is the select name. So change the plain select tag which I did in the example below. This also requires them to submit it.
$result = mysqli_query($con,"SELECT ID, NAME FROM b_sonet_group ORDER BY ID DESC");
echo '<form id="project_form" method="post">';
echo "<select id='select_your_project' name = 'select_your_project'>";
echo "<option value=''>Select Your Project</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID'] . "'>" . $row['NAME'] . "</option>";
}
echo "</select>";
if(isset($_POST['select_your_project'])){
$result2 = mysqli_query($con,"SELECT ID, ALBUM_NAME FROM a_different_table WHERE ID='".$_POST['select_your_project']."'");
echo "<select id='select_your_album' name = 'select_your_album'>";
echo "<option value=''>Select Your Album</option>";
while($row = mysqli_fetch_array($result2))
{
echo "<option value='" . $row['ID'] . "'>" . $row['ALBUM_NAME'] . "</option>";
}
echo "</select>";
}
echo '<input type="submit" value="Submit">';
echo '</form>';
if(isset($_POST['select_your_album'])){
//do form submitted stuff here
}
Ajax way (two separate files, untested but gives you the idea)
//Main page (view) START
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
//this will trigger automatically when they change the first select box
$('#select_your_project').on('change', function(event){
if($(this).val() == 'select_your_project'){
$("#ajax_reply_div").empty()
}else{
var values = $(this).serialize();
$.ajax({
url: "php_data_file.php",
type: "post",
data: values,
success: function(data){
$("#ajax_reply_div").empty().append(data);
},
error:function(){
$("#ajax_reply_div").empty().append('something went wrong');
}
});
}
});
</script>
<form id="id_of_form">
<?php
echo "<select id='select_your_project' name='select_your_project'>";
echo "<option value='select_your_project'>Select Your Project</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID'] . "'>" . $row['NAME'] . "</option>";
}
echo "</select>";
?>
</select>
<div id="ajax_reply_div">
</div>
<input type="submit" value="Submit">
</form>
//Main page (view) END
//php_data_file.php START
if(isset($_POST['select_your_project'])){
$result2 = mysqli_query($con,"SELECT ID, ALBUM_NAME FROM a_different_table WHERE ID='".$_POST['select_your_project']."'");
//as a note it is better to only send an array back then build the HTML with jQuery, but this way is easier if you are new to jQuery/Ajax
echo "<select id='select_your_album' name = 'select_your_album'>";
echo "<option value=''>Select Your Album</option>";
while($row = mysqli_fetch_array($result2)){
echo "<option value='" . $row['ID'] . "'>" . $row['ALBUM_NAME'] . "</option>";
}
echo "</select>";
}
//php_data_file.php END
Ive got a property website im working on for a school project http://www.holidayaviemore.com
I have three dropdown lists which have all been generated from a mysql database.
I have added a submit button so that when the options have been selected the database info will display..this is where I am really stuck!
Im not sure what code of functions are needed to diplay the content on the page or on another page..any ideas? This is the code for the dropdownn boxes and form I have so far..
EDIT
Basically I want the options selected to be displayed on a webpage http://www.holidayaviemore.com shows the dropdown lists
<p>Select Options From Below to find property</p>
<form action="#" name="form" id="form" method="post">
<?php
$sql = "SELECT DISTINCT availability FROM properties";
$result = mysql_query($sql);
echo "<select name='availability'><option value=''>- Availability?--</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='". $row['availability']. "'>" . $row['availability'] ."</option>";
}
echo "</select>";
?>
<?php
$sql = "SELECT bedrooms FROM properties LIMIT 10";
$result = mysql_query($sql);
echo "<select name='bedrooms'><option value=''>--Please Choose Bedrooms Bedrooms--</option>";
$count = 1;
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['bedrooms'] . "'> " . $count . "</option>";
$count++;
}
echo "</select>";
?>
<?php
$sql = "SELECT sleeps_min FROM properties LIMIT 12";
$result = mysql_query($sql);
echo "<select name='sleeps_min'><option value=''>--Please Choose Guests--</option>";
$count = 1;
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['sleeps_min'] . "'> " . $count . "</option>";
$count++;
}
echo "</select>";
?>
<input type="submit" name="" value="GO" />
</form>
I think you need this. You need to see if form already submitted and add selected in option tag where it needed.
<p>Select Options From Below to find property</p>
<form action="#" name="form" id="form" method="post">
<?php
$sql = "SELECT DISTINCT availability FROM properties";
$result = mysql_query($sql);
echo "<select name='availability'><option value=''>- Availability?--</option>";
while ($row = mysql_fetch_array($result)) {
if(isset($_POST['availability']) and $row['availability'] == $_POST['availability']){
echo "<option value='". $row['availability']. "' selected>" . $row['availability'] ."</option>";
}
else{
echo "<option value='". $row['availability']. "'>" . $row['availability'] ."</option>";
}
}
echo "</select>";
?>
<?php
$sql = "SELECT bedrooms FROM properties LIMIT 10";
$result = mysql_query($sql);
echo "<select name='bedrooms'><option value=''>--Please Choose Bedrooms Bedrooms--</option>";
$count = 1;
while ($row = mysql_fetch_array($result)) {
if(isset($_POST['bedrooms']) and $row['bedrooms'] == $_POST['bedrooms']){
echo "<option value='" . $row['bedrooms'] . "' selected> " . $count . "</option>";
}
else{
echo "<option value='" . $row['bedrooms'] . "'> " . $count . "</option>";
}
$count++;
}
echo "</select>";
?>
<?php
$sql = "SELECT sleeps_min FROM properties LIMIT 12";
$result = mysql_query($sql);
echo "<select name='sleeps_min'><option value=''>--Please Choose Guests--</option>";
$count = 1;
while ($row = mysql_fetch_array($result)) {
if(isset($_POST['sleeps_min']) and $row['sleeps_min'] == $_POST['sleeps_min']){
echo "<option value='" . $row['sleeps_min'] . "' selected> " . $count . "</option>";
}
else{
echo "<option value='" . $row['sleeps_min'] . "'> " . $count . "</option>";
}
$count++;
}
echo "</select>";
?>
<input type="submit" name="" value="GO" />
</form>