Dynamic Dropdown List PHP MYSQL - php

I am new to PHP and trying to make dynamic list box using SQL
<label> Select Sport</label>
<select name = "Sport">
<option value = "">Select Sport</option>
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"></option>
<?php
}
?>
</select>
But its printing BLANK space

Try this:
<select name="">
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"><?php echo $Sport['Sport']; ?></option> // The value between <option> value </option> is the value which is to be shown in the dropdown, without this it is showing blank
<?php
}
</select>

You probably shouldn't use SELECT *, but since you're learning, I digress. Also, I personally have a hard time reading a lot of opening and closing php tags scattered throughout plus they often give weird results than what you're expecting, so this is how I'd write it.
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $sport)
{
echo "<option value =" . $sport['Sport'] . ">" . $sport['Sport'] . "</option>";
}
?>
The blank spaces indicates that you are actually hitting the loop and iterating, but the values for that array item are null. Are you sure the field name isn't 'sport' instead of 'Sport'?

Related

Wordpress retain a selected value in a dropdown after form submit

I am a complete novice but trying to design my first site in Wordpress. I am trying to retain the value of a dropdown that pulls values from a database after a form is submitted. I have searched many questions and tried many different ways of trying to write selected="selected" in my code but none of them seem to work. Please help.
Here is my code that just pulls from the database but isn't trying to retain the selected value:
<select name = "box1" class="searchbox">
<option value = "">All Values</option>
<?php
global $wpdb;
$ddresult = $wpdb->get_results("SELECT Field1 FROM pc_table ORDER BY Field1 ASC");
foreach($ddresult as $ddrow) {
?>
<option value="<?php echo $ddrow->Field1; ?>"><?php echo $ddrow->Field1; ?> </option>
<?php
} ?>
</select>
How can I add the code to keep the value selected after form submit? Any help much appreciated, thank you.
Try this. It checks if box1 was submitted. Then it compares that value to the items in the options loop. A match will set $selected to the correct attribute, otherwise it will stay an empty string by default. (NB: if form is using get method, then change $POST to $_GET)
<select name = "box1" class="searchbox">
<option value = "">All Values</option>
<?php
global $wpdb;
$ddresult = $wpdb->get_results("SELECT Field1 FROM pc_table ORDER BY Field1 ASC");
foreach($ddresult as $ddrow) {
$selected = '';
if(isset($_POST['box1'])){
if($ddrow->Field1==$_POST['box1']){ $selected = 'selected="selected"'; }
}
?>
<option value="<?php echo $ddrow->Field1; ?>" <?php echo $selected; ?>><?php echo $ddrow->Field1; ?> </option>
<?php
} ?>
</select>
This example of course does not include if you want to save the submitted value to the database and then re introduce it in the output at a later time. If that's what you would like to know, then please leave a comment.

Is it possible to send an array in a POST, or assign multiple values in one input tag, and how?

I want to send more than one value in the POST but use only one type of input tag.
For example, is it possible to set the value of the option tag to both $data['username_id] and $data['name'] and send it via the POST
<select name="owner" id="owner">
<option value="NULL" selected="selected"></option>
<?php
$sql = 'SELECT * FROM users'
. ' ORDER BY name';
$query = mysqli_query($connect,$sql) or die (mysqli_error($connect));
while ($data = mysqli_fetch_array($query))
{
?>
<option value="<?php echo $data['user_id']; ?>"><?php echo $data['name']; ?></option>
<?php
}
?>
</select>
I can't seem to retrieve on the other end. I have tried:
<option value="<?php echo $data['user_id','name']; ?>"><?php echo $data['name']; ?></option>
But still no luck.
Any help would be useful.
For example, is it possible to set the value of the option tag to both $data['username_id]' and $data['name'] and send it via the POST
Yes, this can be done, carefully.
Working with your example:
<option value="<?php echo $data['user_id','name']; ?>"><?php echo $data['name']; ?></option>
Can be simply rewritten as:
<option value='<?php echo $data['user_id']."--".$data['name']."'>"
.$data['name']; ?></option>
At the other end:
On the form receiver PHP page:
$parts = explode("--",$_POST['owner']);
/***
* $parts[0] = user_id
* $parts[1] = name
***/
BUT as you are grabbing the data from a database row anyway, this is fairly pointless, you might as well JUST transport the user ID and simply use it on the receiving end to grab the 'name' data value from the Database.
There is also potential issues if the splitter (--) appears more than once, so be carefully to choose a splitter that does not apper in either of the values you are trying to send.
Working Freehand:
<input name="whatever[]" value="one">
<input name="whatever[]" value="two">
The square bracket means that the data passed in $_POST will be an array of values.
As pointed out in comments by Kainaw you can also simply use the multiple selection to reach the same effect in your <select> input.

set default value in dropdown when options in dropdown are dynamic

I have two table in database. in table their is my all divisions which are shown in dropdown options. In second table, i submit my division name in division field.
I want value in division field show as a default value in dropdown.here is my code..
here is my first query by which all division are comes
$all_customers = mysql_query("select * from `supp_customers`");
<select name="division" id="c_name" required class="form-control" onchange="my_function(this,<?php echo $users['id']; ?>)">
<option value="">All Divisions</option>
<?php while($customer = mysql_fetch_array($all_customers)){ ?>
<option value="<?php echo $customer['name']; ?>"><?php echo $customer['name']; ?></option>
<?php } ?>
</select>
and my second query
$pro_qry = mysql_query("SELECT * FROM `supp_average_notes` where `id`='$id'");
$div_query = mysql_fetch_array($pro_qry);
$dive_query['division'];
?>
i Want to set $div_query['division'] as a default in dropdown. How it can be possible
In your while loop, you should compare the division to the current division. If it's the same, select the option. I would recommend you to
do what #RiggsFolly says,
look into a PHP framework; it will take a lot of work out of your hands and add security, e.g. Laravel, Yii or Symfony,
use id's in your select option values instead of names,
use alternative code style in views like below,
improve your variable naming so the name states what it holds.
.
<?php foreach ($divisions as $division): ?>
<?php if ($division['id'] == $currentDivision['id']) $selected = ' selected="selected"'; else $selected = ''; ?>
<option <?= $selected ?> value="<?= $division['id'] ?>">
<?= $division['name'] ?>
</option>
<?php endforeach ?>

using php code within html to dynamically load a dropdown list

I am trying to use php code within html to grab content from a database to populate the values for a drop down menu (see below). When I run the php script on its own (test.php) I get all the expected values. When nested within html I get nothing but a single blank value under Select a Species. I would expect this to run through the while loop more than once as there are about 7 values returned and I would also expect the contents to include data derived from the table.
What I am attempting is possible correct??
Is it just an error with code (no errors popping up in the logs)
<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
<option id = "0">-- Select a Species -- </option>
<?php
$con = mysqli_connect("localhost","xxx","xxxx","databases");
$result = mysqli_query($con, "SELECT * FROM bc_species WHERE 1");
while ($row = $result->fetch_assoc()){
?>
<option><?php echo $row['species'];?></option>
<?php } ?>
</select>
<option><?php echo $row["species"]; ?></option>
<?php
$optionData = '<option id = "0">-- Select a Species -- </option>';
$con = mysqli_connect("localhost","xxx","xxxx","databases");
$result = mysqli_query($con, "SELECT * FROM bc_species WHERE 1");
while ($row = $result->fetch_assoc()){
$optionData .= "<option>".$row['species']."</option>" ;
}
?>
<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
<?php echo $optionData;?>
</select>
//Put your fetched data on an array then loop them like this
<?php
foreach ($list as $data => $item) {
?>
<option value="<?php echo $data?>">
<?php echo $data?>
</option>
<?php
}
?>
This should work:
<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
<option id = "0">-- Select a Species -- </option>
<?php
$con = mysqli_connect("localhost","xxx","xxxx","databases");
$result = mysqli_query($con, "SELECT * FROM bc_species");
while ($row = $result->fetch_assoc()){
echo '<option>'. $row["species"].'</option>
} ?>
</select>
You were not iterating through the values within the HTML <option> tags, so I moved those into the while loop. (notice the PHP section includes echoing those tags).
I also removed the unnecessary "SQL injection-looking" where 1 clause from your query.

Displaying a php mysql result with two values with a foreach loop in PHP

I have result that I want to display as a drop down menu. The query selects id and name from a table.
$usersQuery = "SELECT id, name
FROM users";
$usersResult = mysqli_query ($dbc, $usersQuery);
I want to use this result as a list in a drop down menu. This is what i have so far.
<select id="dropdown" name="dropdown">
<option value="select" selected="selected">Select</option>
<?php
while ($usersRow = mysqli_fetch_array($usersResult, MYSQLI_ASSOC)){
foreach ($usersRow as $value){
echo "<option value=\"$value\"";
echo ">$value</option>\n";
}
}
?>
</select>
this would work fine if I just wanted to display name as both the value and the display to the user. But what I want to do is use the selected id as "value" for the select option and I want to show the name selected to the user. I have tried this but it does not work.
<select id="dropdown" name="dropdown">
<option value="select" selected="selected">Select</option>
<?php
while ($usersRow = mysqli_fetch_array($usersResult, MYSQLI_ASSOC)){
foreach ($usersRow as $id=>$name){
echo "<option value=\"$id\"";
echo ">$name</option>\n";
}
}
?>
</select>
Any help would be great.
Thanks in advance.
mysqli_fetch_array() is a function which converts your query results into an array. which means you can display your values like you would with a normal array value.
<select id="dropdown" name="dropdown">
<option value="select" selected="selected">Select</option>
<?php
while ($usersRow = mysqli_fetch_array($usersResult, MYSQLI_ASSOC)){
echo "<option value=\"".$usersRow['id']."\"";
echo ">".$usersRow['name']."</option>\n";
}
?>
</select>
No need for the foreach iteration; mysqli_fetch_array() already provides an associative array. After each fetch do
// Assuming "id" is a numeric value
printf('<option value="%d">%s</option>', $usersRow['id'], $usersRow['name']);
The foreach is unnecessary - using a while loop on the mysqli_fetch_array command will return all the results with each row in an array - you can use it like so:
while ($usersRow = mysqli_fetch_array($usersResult, MYSQLI_ASSOC)){
echo "<option value=\"".$usersRow['id']."\"";
echo ">".$usersRow['name']."</option>\n";
}

Categories