dynamic html select boxes need to update mysql - php

Ok, so this one is a little confusing. I have select dropdowns that are produced by PHP. It can be 4 selects, or 30 select dropdowns. Then there's option values. Here's what I have so far
<?php while($row = mysql_fetch_assoc($notes)){ ?>
<select name="milestone" id="milestone[<?php echo $row['id']; ?>]">
<option value="Enrollment Date">Enrollment Date</option>
<option value="Discharge Date">Discharge Date</option>
<option value="A/C Start">A/C Start</option>
<option value="Completion Date">Completion Date</option>
</select>
<?php } ?>
If I have 4 select boxes, I might have arrays as follows: milestone[2134], milestone[2222], milestone[225], and milestone[1022]
The array number is the id of the mysql table entry I need to update with the value of that specific select dropdown. I was thinking maybe to use milestone[][id] and loop through that?
Any ideas since there might be 20 select dropdowns?
Thanks!

do you want to fetch the values of your options from the database as well as the id ? Then you should to mysql_fetch_array or mysql_fetch_object function instead of mysql_fetch_assoc. This function only returns the number of the results while the two above returns the number as well as values.

Got it!
First, I had to apply the array to the name of the select like so and set the id value:
<select name="milestone[]" class="mstones" id="<?php echo $row['session_id']; ?>">
Then, with jquery, looped through the class and created created an array with the value being something I can "explode" in php:
var milestoneVal = [];
$("select.mstones").each(function(i, selected){
milestoneVal[i] = this.getAttribute('id') + ':' + $(selected).val();
});
Then, simple PHP
foreach($_POST['milestone'] as $v){
$m=explode(':',$v);
//db insert
}
It was tricky, and I'm sure there's a cleaner solution, but it works for me! Sorry for the poor initial explanation, and hope this helps someone else.

Related

Limiting number of options displayed by a php populated dropdown menu

I have a drop-down menu as following:
<select onchange="random_function()" id='choice' >
<option selected="selected" >
Make a choice
</option>
</select>
I populate it through a php request inside the <select> tag as following:
<?php
include 'config.php';
$query = $pdo->query('
SELECT choices
FROM allTheChoices'
);
while ($row = $query->fetch()){
echo "<option>".$row['choices']."</option>";
}
?>
For the moment, my database is uncomplete and I have only 2 options to display but there will be plenty in the future (100+).
My goal is to find a way to let the select only show 10 options and it should display a scroll bar to see all the remaining options.
I already tried using <select size="10"> but the design of the dropdown is completely changed and awful.
Do you have a simple way to do this without altering the design?
Used this bit of code to achieve what I wanted :
<select style="position:absolute;" onmousedown="if(this.options.length>8)
{this.size=8;}" onchange='this.size=0;' onblur="this.size=0;" >
Make sure to use the following to avoid container div to expand when choosing an option :
style="position:absolute;"
and edit the following code with the number of choices you want to display (10 is used in this example) :
onmousedown="if(this.options.length>10){this.size=10;}"
So the whole html code I used is :
<select style="position:absolute;" onmousedown="if(this.options.length>8)
{this.size=8;}" onchange='this.size=0;' onblur="this.size=0;" >
<option selected="selected" >
Make a choice
</option>
</select>
Try using a limit in your SQL statement, like this: "LIMIT 10".
So this is how the code might look:
<?php
include 'config.php';
$query = $pdo->query('
SELECT choices
FROM allTheChoices
LIMIT 10'
);
while ($row = $query->fetch()){
echo "<option>".$row['choices']."</option>";
} ?>

Distinguishing multiple items in a select multiple with PHP

I have a form on my website where one can select multiple other editors for a post.
The list of qualified editors is echoed by PHP, which works. But when multiple are selected HTML makes it into one list of the ids. F.E. If Jay (id = 4) and Sam (id = 9) are selected. The received value will be $_POST['editors'] = 49.
My code:
<select multiple class="editors" name="editors" id="editors">
<?php
//Gebruikers ophalen
$editorArr = getEditors();
foreach($editorArr as $editor){
echo "<option value='".$editor['id']."'>".$editor['email'].' - '.$editor['type']."</option>";
}
?>
</select>
and the handling PHP
$editors = htmlentities($_POST['editors']);
Okay! Try this:
<select class="editors" name="editors[]" id="editors" multiple>
<?php
//Gebruikers ophalen
$editorArr = getEditors();
foreach ($editorArr as $editor) {
echo "<option value='".$editor['id']."'>".$editor['email'].' - '.$editor['type']."</option>";
}
?>
</select>
The name should be an array so it can treat all selected values separately.
Hope it helps

Post multiple values from database, in one Option

I am looking to populate a drop down field with a list of names from a database, and when an option is selected from that drop down, it will post all its values in the row, belonging to that chosen option.
This is probably hard to describe/understand, so to help illustrate, this is my table row:
I then proceed to populate the dropdown, and associate its value to whatever the selected option is posted
//////db_conx is db connection //////main_meal is table name
<form action="#" method="post">
<?php
$dropdown = $db_conx->query("SELECT * FROM main_meal") or die ("somethings broken");
while($array[] = $dropdown->fetch_object());
//echo '<option value ="'.$record['Mname'].'">'.$record['Mname'].'"</option>';
array_pop($array);
?>
<select name="changeCal">
<?php foreach($array as $option) :?>
<!--//get chosen value in drop down, and get its calories-->
<option value="<?php echo $option->calories;?>"><?php echo $option->Mname; ?></option>
<?php endforeach; ?>
</select>
This works great for one value, such as calories, in the above code, but I need more values.
For example, if choose Healthy egg and chips, the value will post 218, as the loop only associates calories and names at the moment.
I attempted various things, like this post:How to get multiple values from a single <select> variable in HTML/PHP?
But the foreach errors.
How can I something similar to what I have done, but store multiple values from one chosen option?
Thank you
Well, I think I understood your problem, but I think that the way you want to use is not usable, maybe I recommend that you put the id in the value of the option in the < select> and then with php you can get all the data from the data base.
For me that is the best way, but if you want do it like the example that you show, you can make an string, for example:
<option value="id:5_calories:258_protein:11g">Healthy eggs & Chips</option>
with your php should look like:
echo '<option value="id:'.$option->id.'_calories:'.$option->calories.'_protein:'.$option->protein.'">'.$option->Mname.'</option>';
you can make bigger the string with others values that you want to put.
In the backend when you send the select you can catch the data with a:
$myArray = explode("_", $_POST["changeCal"]);
Then you will get an array with values like:
$myArray[0]; // id:5
$myArray[1]; // calories:258
$myArray[2]; // protein:11g
Then if you need for example the calories you can make an explode like:
$calories = explode(":",$myArray[1]);
And you will have:
$calories[0]; //calories
$calories[1]; //258 <= Here are the calories.
Maybe if you want to do it of that way, this can be the easiest way, but I recommend send the ID.
Let me know if you need more help. Regards, Have a nice day.

issue with IF statement returning unexpected result

I am trying to achieve the following...
Each 'element' should have the option to choose one 'sibling' from a select dropdown, the siblings are chosen from all of the other elements, the sibling cannot be itself, but a sibling doesn't have to be assigned. If one is assigned it should be selected by default in the select list.
<?php echo $sibling; ?>
<select name="sibling">
<option value="none">none</option>
<?php
while($row = $result->fetch_array()) {
if($row['element']!=$element){
echo '<option value="'.$row['element'].'" ';
if($row['sibling']==$sibling){
echo 'selected="selected"';
}
echo '>'.$row['element'].'</option>';
}
}
?>
</select>
I have almost achieved what I want but for some reason the code doesn't add selected="selected" to the right option.
I'd appreciate any guidance anyone could give in where I may be going wrong? I'm fairly new to PHP so I may be going about this in the wrong way.
$element and $sibling are equal to $row['element'] and $row['sibling'] for the corresponding $id which is an index that is passed to the page on a query string.
UPDATE: FOUND ANSWER
So with a little investigation this was glaringly obvious.
The line containing...
if($row['sibling']==$sibling)
should have been...
if($row['element']==$sibling){
Thanks to #RyanS for suggesting the var dump.

Choosing a value in a drop down box based on database query

I basically need to create a value by default for a drop down menu on a PHP page that will use the value previously selected and stored in the database.
For example, lets just say I have the value '3' stored in a database column. I want to use this number as the default value for a drop down menu where the <option value = "3">Good</option>. Is there a simple solution to this problem?
Or do i literally need to loop through the values until it makes a value?
Thanks.
I usually do this
<?php
$sel = 'selected="selected"';
$current_whatever = 5;
?>
<option name="whatever">
<?php foreach($list as $listItem): ?>
<option value="<?=$listItem->id?>" <?=($listItem->id == $current_whatever)?$sel:''?>><?=$listItem->name?></option>
<?php endforeach; ?>
</option>
I'm using an inline if statement to check each one :) Looks reasonably tidy.
Assuming you're using database objects, you get the idea if you're not though :)
<?php foreach($options as $key=>$option){?>
<option value='<?=$key?>' <? echo $key==$selected?"SELECTED":"";?> ><?=$option?></option>
<? }?>

Categories