HTML to PHP dropdown menu - html

I have this html part for a dropdown menu:
<form name="navList" onsubmit="return submitForm();">
<select name="subMenu">
<option value> </option>
<option value> CR_ID </option>
<option value> CR_HEADLINE </option>
<option value> CRI_CLOSEDATE </option>
<option value> CRI_ASSIGNEE </option>
</select>
How can I change this into php, using this function?
foreach($array as $key=>$value)
{
$html .= "<option value='$key'>$value</key>";
}
echo "<select name="process">$html</select>";

You forget to add escape sequences in your output . Please refer this demonstration:-
$optionArray = array(
0 => 'CR_ID',1=> 'CR_HEADLINE',
2 => 'CRI_CLOSEDATE',3 => 'CRI_ASSIGNEE'
);
foreach($optionArray as $key=>$value)
{
$html .= "<option value='$key'>$value</key>";
}
echo "<select name=\"process\">".$html."</select>";
OR
echo '<select name="process">'.$html.'</select>';

echo "<select name=\"process\">".$html."</select>";
You needed to escape your quotes.

<?php
$html ='';
$myArray = array(0 => '',1 => 'CR_ID',2=> 'CR_HEADLINE', 3 => 'CRI_CLOSEDATE',4 => 'CRI_ASSIGNEE');
foreach($myArray as $key=>$value)
{
$html .= "<option value='$key'>$value</key>";
}
echo "<select name=\"process\">".$html."</select>";
Link - http://codepad.viper-7.com/YpccFw

Related

MySql Dropdown filter select with NULL

I'm trying to build up a drop down filter system for a custom table. I have the following code but I can't seem to get it to work such that if one of the drops is not selected it still returns results based on the the selection of the second drop down:
<form method="POST" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<select name="bt_nbs_intervention">
<option value=''>NbS intervention type</option>
<option value="3">Protection</option>
<option value="143">Restoration</option>
</select>
<select name="climate_change_impacts">
<option value=''>Climate change impacts</option>
<option value="15">Drought</option>
<option value="12">Flood</option>
<option value="25">Mangrove</option>
</select>
<input type='submit' value='Search'>
</form>
<?php
if ($_POST['bt_nbs_intervention'] === '') {
$_POST['bt_nbs_intervention'] = null;
}
if ($_POST['climate_change_impacts'] === '') {
$_POST['climate_change_impacts'] = null;
}
$bt_nbs_intervention = (int)$_POST['bt_nbs_intervention'];
$cc_impact = (int)$_POST['climate_change_impacts'];
$interventions = $wpdb->get_results($wpdb->prepare("
SELECT ID, intervention_post_title
FROM wp_cpt_combined
WHERE (bt_nbs_intervention IS NULL OR bt_nbs_intervention = %d) AND
(climate_change_impacts IS NULL OR climate_change_impacts = %d)",
$bt_nbs_intervention, $cc_impact));
if($interventions)
foreach ( $interventions as $intervention )
{
echo $intervention->intervention_post_title;
}
else {
echo "no results";
}
Essentially what I'm trying to do allow the user to select one or more of the select boxes to produce results. Such that if they only select Intervention type they get results or only Climate change impacts OR if they select both.
Hope that makes sense.
Thanks
D
Can you check the following please:
<form method="POST" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<select name="bt_nbs_intervention">
<option value=''>NbS intervention type</option>
<option value="3">Protection</option>
<option value="143">Restoration</option>
</select>
<select name="climate_change_impacts">
<option value=''>Climate change impacts</option>
<option value="15">Drought</option>
<option value="12">Flood</option>
<option value="25">Mangrove</option>
</select>
<input type='submit' value='Search'>
</form>
<?php
$where = [];
$placeholders = [];
if(!empty($_POST['bt_nbs_intervention'])){
$where[] = 'bt_nbs_intervention = %d';
$placeholders[] = (int)$_POST['bt_nbs_intervention'];
}
if(!empty($_POST['climate_change_impacts'])){
$where[] = 'climate_change_impacts = %d';
$placeholders[] = (int)$_POST['climate_change_impacts'];
}
$sql = "SELECT ID, intervention_post_title FROM wp_cpt_combined";
if(!empty($where)){
$sql .= ' WHERE ';
$i = 0;
foreach($where as $clause){
$sql .= $i == 0 ? $clause : ' AND ' . $clause;
$i++;
}
$sql = $wpdb->prepare($sql, $placeholders);
}
$interventions = $wpdb->get_results($sql);

Two action forms and two submit buttons, how to keep the values?

I have 2 action forms in the page and 2 submit buttons.
1 is zoom-level
1 is refresh rate in seconds
When I hit submit of zoom, the refresh rate is reset
When I hit submit of refresh rate, the zoom level is reset
What to do to keep the value of the other when one dropdown is set to a certain value?
I think this is a common problem where noobs struggle with, so it would be nice to have some good explanation from experts with nice guidance here...
code is:
<?php
$submittedValue = "";
$value0 = 1;
$value1 = 1.2;
$value2 = 1.5;
$value3 = 2;
if (isset($_POST["FruitList"])) {
$submittedValue = $_POST["FruitList"];
}
?>
<form action="example3b2vandaag.php" name="fruits" method="post">
<select project="FruitList" id="FruitList" name="FruitList">
<option value = "<?php echo $value0; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>><?php echo "off"; ?></option>
<option value = "<?php echo $value1; ?>"<?php echo ($value1 == $submittedValue)?" SELECTED":""?>><?php echo $value1; ?></option>
<option value = "<?php echo $value2; ?>"<?php echo ($value2 == $submittedValue)?" SELECTED":""?>><?php echo $value2; ?></option>
<option value = "<?php echo $value3; ?>"<?php echo ($value3 == $submittedValue)?" SELECTED":""?>><?php echo $value3; ?></option>
</select>
<input type="submit" name="submit" id="submit" value="Set zoom level" />
</form>
<?php
$submittedValue = "";
$value0 = 1000;
$value1 = 20000;
$value2 = 30000;
$value3 = 90000;
if (isset($_POST["FruitList2"])) {
$submittedValue = $_POST["FruitList2"];
}
?>
<form action="example3b2vandaag.php" name="fruits2" method="post">
<select project="FruitList2" id="FruitList2" name="FruitList2">
<option value = "<?php echo $value0; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>><?php echo $value0; ?></option>
<option value = "<?php echo $value1; ?>"<?php echo ($value1 == $submittedValue)?" SELECTED":""?>><?php echo $value1; ?></option>
<option value = "<?php echo $value2; ?>"<?php echo ($value2 == $submittedValue)?" SELECTED":""?>><?php echo $value2; ?></option>
<option value = "<?php echo $value3; ?>"<?php echo ($value3 == $submittedValue)?" SELECTED":""?>><?php echo $value3; ?></option>
</select>
<input type="submit" name="submit" id="submit" value="Set refresh milliseconds" />
</form>
I don't see any reason why you would want two forms, you can put the two select boxes into one form.
And please don't use unintelligent variable names such as $value0, $submittedValue and $Fruitlist, because this isn't about fruits.
Here are the two select boxes into one form:
<?php
$zoom = "";
$zoom0 = 1;
$zoom1 = 1.2;
$zoom2 = 1.5;
$zoom3 = 2;
$refresh = "";
$refresh0 = 1000;
$refresh1 = 20000;
$refresh2 = 30000;
$refresh3 = 90000;
if (isset($_POST["zoom"])) {
$zoom = $_POST["zoom"];
}
if (isset($_POST["refresh"])) {
$refresh = $_POST["refresh"];
}
?>
<form action="testForm.php" name="controlpanel" method="post">
ZoomLevel:<br />
<select project="ControlPanel" id="ControlPanel" name="zoom">
<option value = "<?php echo $zoom0; ?>"<?php echo ($zoom0 == $zoom)?" SELECTED":""?>><?php echo "off"; ?></option>
<option value = "<?php echo $zoom1; ?>"<?php echo ($zoom1 == $zoom)?" SELECTED":""?>><?php echo $zoom1; ?></option>
<option value = "<?php echo $zoom2; ?>"<?php echo ($zoom2 == $zoom)?" SELECTED":""?>><?php echo $zoom2; ?></option>
<option value = "<?php echo $zoom3; ?>"<?php echo ($zoom3 == $zoom)?" SELECTED":""?>><?php echo $zoom3; ?></option>
</select>
<br />
Refresh rate:<br />
<select project="ControlPanel" id="ControlPanel" name="refresh">
<option value = "<?php echo $refresh0; ?>"<?php echo ($refresh0 == $refresh)?" SELECTED":""?>><?php echo $refresh0; ?></option>
<option value = "<?php echo $refresh1; ?>"<?php echo ($refresh1 == $refresh)?" SELECTED":""?>><?php echo $refresh1; ?></option>
<option value = "<?php echo $refresh2; ?>"<?php echo ($refresh2 == $refresh)?" SELECTED":""?>><?php echo $refresh2; ?></option>
<option value = "<?php echo $refresh3; ?>"<?php echo ($refresh3 == $refresh)?" SELECTED":""?>><?php echo $refresh3; ?></option>
</select><br />
<input type="submit" name="submit" id="submit" value="Set configurations" />
</form>
To use the data you get from the form you can use $_POST["zoom"] and $_POST["refresh"], because that's what the name properties of the select boxes are.
See http://www.w3schools.com/php/php_form_complete.asp for more information on html forms and how to use the $_POST data.

mysql_fetch_array and while loop accidentally returning every other record

Question title says it all: I want to loop through every row, but I get only every other row. Funny enough, when I use mysql_num_rows as can be seen below, it gets the right count. Whats up?
$query = "SELECT * FROM `staff`";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($result);
echo '<form action="" method="post"><ul>';
while ($row = mysql_fetch_array($result)){
echo '<li>' . $row['name'] . '
<select>
';
$i = 1;
while ($i <= $count) {
echo '<option value="' . $i . '">' . $i . '</option>';
$i++;
}'
</select>
</li>';
}
Can it be because I am counting as well? I need to count as well. This is a reordering app so I need to be able to change the position of every person, but the options can only be the total number of positions available. Maybe there is a more logical way to do this anyway?
You have are not outputting the string that closes the </select> tag:
while ($i <= $count) {
echo '<option value="' . $i . '">' . $i . '</option>';
$i++;
}'
'// ^--- NB missing echo
As it stands, PHP currently parses the string as a literal expression that performs no operation.
Consequently, your resulting HTML will look like:
<li>foo
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<li>bar
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<li>baz
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
How such broken HTML is rendered will depend upon one's browser. It would appear that your browser is only producing every other select box?

HTML/CSS Form (Table) DOB line showing on two lines

On my websites form - my date of birth day, month and year menus are showing on two lines, I'd like them to all show on the same line instead.
When I test the code it show's on one line so I believe there must be a width issue.
You can see the issue on the right hand form (incomeprotectionstore.co.uk).
Any help would be much appreciated.
The date of birth css can be seen below:
<tr>
<td nowrap=""><label><div align="left">Date of birth</div></label></td>
<td><select name="x_C1DOB_YYYY" id="dd_dob_year" class="input-dob">
<?
for($i=1995; $i >= 1900; $i--){
?>
<OPTION value="<? echo $i; ?>" <? formSelected( $x_C1DOB_YYYY, $i ); ?>><? echo $i; ?></OPTION>
<?
}
?>
</select>
<select name="x_C1DOB_MM" id="dd_dob_month" class="input-dob">
<?
for($i=1; $i<=12; $i++){
if($i < 10) $x = "0".$i; else $x = $i;
$month = $month_name[$i-1];
?>
<OPTION value="<? echo $x; ?>" <? formSelected( $x_C1DOB_MM, $x ); ?>><? echo $month; ?></OPTION>
<?
}
?>
</select>
<select name="x_C1DOB_DD" id="dd_dob_day" class="input-dob">
<?
for($i=1; $i<=31; $i++){
if($i < 10) $x = "0".$i; else $x = $i;
?>
<OPTION value="<? echo $x; ?>" <? formSelected( $x_C1DOB_DD, $x ); ?>><? echo $x; ?></OPTION>
<?
}
?> </select>
</td>
</tr>
Thanks,
Sam
You could try wrapping your selects in an element that prevents line-wrapping like this:
<div style="white-space:nowrap;">
<select>
<select>
<select>
</div>
Set width:200px; on the <td> containing the DOB dropdowns (like it is set on the <input>s in the rest of the form)
for instance <td class = "dob-wrapper"> in the HTML and .dob-wrapper { width:200px; } in the CSS
Final answer:
Change the first <td> to <td style="width:99px;"> Then change the second <td> element to <td style="width:200px;">
If you want the boxes on the right instead of left change use 160px instead of 99px
this is the working code of birthday. try this
<div id="birthday">
<p>Birthday</p>
<table id="birthday">
<tr>
<th>
<select name="month">
<option value="">Month</option>
<option value="01" <?php if(isset($_POST['month']) && $_POST['month'] == '01') { echo 'selected="selected"'; } ?>>Jan</option>
<option value="02" <?php if(isset($_POST['month']) && $_POST['month'] == '02') { echo 'selected="selected"'; } ?>>Feb</option>
<option value="03" <?php if(isset($_POST['month']) && $_POST['month'] == '03') { echo 'selected="selected"'; } ?>>Mar</option>
<option value="04" <?php if(isset($_POST['month']) && $_POST['month'] == '04') { echo 'selected="selected"'; } ?>>Apr</option>
<option value="05" <?php if(isset($_POST['month']) && $_POST['month'] == '05') { echo 'selected="selected"'; } ?>>May</option>
<option value="06" <?php if(isset($_POST['month']) && $_POST['month'] == '06') { echo 'selected="selected"'; } ?>>Jun</option>
<option value="07" <?php if(isset($_POST['month']) && $_POST['month'] == '07') { echo 'selected="selected"'; } ?>>Jul</option>
<option value="08" <?php if(isset($_POST['month']) && $_POST['month'] == '08') { echo 'selected="selected"'; } ?>>Aug</option>
<option value="09" <?php if(isset($_POST['month']) && $_POST['month'] == '09') { echo 'selected="selected"'; } ?>>Sep</option>
<option value="10" <?php if(isset($_POST['month']) && $_POST['month'] == '10') { echo 'selected="selected"'; } ?>>Oct</option>
<option value="11" <?php if(isset($_POST['month']) && $_POST['month'] == '11') { echo 'selected="selected"'; } ?>>Nov</option>
<option value="12" <?php if(isset($_POST['month']) && $_POST['month'] == '12') { echo 'selected="selected"'; } ?>>Dec</option>
</select>
</th>
<th>
<select id="day" name="day">
<option value="">Day</option>
<?php
for($i=1; $i<=31; $i++)
{
echo '<option value="' . $i . '"';
if(isset($_POST['day']) && $_POST['day'] == $i)
{
echo ' selected="selected"';
}
echo '>' . $i . '</option>';
}
?>
</select>
</th>
<th>
<select id="year" name="year">
<option value="">Year</option>
<?php
for($i=2014; $i>=1905; $i--)
{
echo '<option value="' . $i . '"';
if(isset($_POST['year']) && $_POST['year'] == $i)
{
echo ' selected="selected"';
}
echo '>' . $i . '</option>';
}
?>
</select>
</th>
</tr>
</table>
</div>

html select list selected item and maximum value can be selected

HELLO,
i have an html select list, and i want: the selected number to be a given value $content->number, and the maximum value to be $content->product_type->stock_2 if it is less than 5, or 5 if it is greater than 5.
now i have:
<select class="number" name="number">
<? $max = $content->product_type->stock_2 > 5 ? 5 : $content->product_type->sale_stock; ?>
<option value="<?= $content->number ?>"><?= $content->number; ?> </option>
<? for ($i = 1; $i <= $max; $i++):?>
<option <?php if($content->product_type->stock_2 == $i) echo 'selected="selected"' ;?> value="<?= $i ?>"><?= $i; ?></option>
<? endfor; ?>
</select>
but it shows me twice the selected value $content->number. i'm sure i am mistaking somewhere.
Any suggestions?
thank you!
I do not know whether I correct understood what you want to do, but try this:
<select class="number" name="number">
<? $max = $content->product_type->stock_2 > 5 ? 5 : $content->product_type->sale_stock; ?>
<option value="<?= $content->number ?>"><?= $content->number; ?> </option>
<? for ($i = 1; $i <= $max; $i++):?>
<? if ($i != $content->number): ?>
<option <?php if($content->product_type->stock_2 == $i) echo 'selected="selected"' ;?> value="<?= $i ?>"><?= $i; ?></option>
<? endif; ?>
<? endfor; ?>
</select>