Well, I am writing class that creates a DOB selection dropdown.
I am having figure out the dropdown(), it seems working but not exactly. Code just creates one drop down, and under this dropdown all day, month and year data are in one selection. like:
<label>
<sup>*</sup>DOB</label>
<select name="form_bod_year">
<option value=""/>
<option selected="" value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="4">5</option>
<option value="5">6</option>
..
<option value="29">30</option>
<option value="30">31</option>
<option value="1">January</option>
<option value="2">February</option>
..
<option value="11">November</option>
<option value="12">December</option>
<option selected="" value="0">1910</option>
<option value="1">1911</option>
..
<option value="98">2008</option>
<option value="99">2009</option>
<option value="100">2010</option>
</select>
Here is my code, I wonder that why all datas are in one selection. It has to be tree selction - Day:Month:Year.
//dropdown connector
class DropDownConnector
{
var $dropDownsDatas;
var $field_label;
var $field_name;
var $locale;
function __construct($dropDownsDatas, $field_label, $field_name, $locale)
{
$this->dropDownsDatas = $dropDownsDatas;
$this->field_label = $field_label;
$this->field_name = $field_name;
$this->locale = $locale;
}
function getValue(){
return $_POST[$this->field_name];
}
function dropdown(){
$selectedVal = $this->getValue($this->field_name);
foreach($this->dropDownsDatas as $keys=>$values){
foreach ($values as $key=>$value){
$selected = ($key == $selectedVal ? "selected" : "" );
$options .= sprintf('%s',$key,$value);
};
};
return $select_start = "$this->field_desc".$options."";
}
function getLabel(){
$non_req = $this->getNotRequiredData();
$req = in_array($this->field_name, $non_req) ? '' : '*';
return $this->field_label ? $req . $this->field_label : '';
}
function __toString()
{
$id = $this->field_name;
$label = $this->getLabel();
$field = $this->dropdown();
return 'field_name.'">'.$label.''.$field.'';
}
}
function generateForm ($lang,$country_list,$states_list,$days_of_month,$month_list,$years){
$xx = array(
'form_bod_day' => $days_of_month,
'form_bod_month' => $month_list,
'form_bod_year' => $years);
echo $dropDownConnector = new DropDownConnector($xx,'DOB','bod','en-US');
}
// Call php class to use class on external functionss.
$avInq = new formGenerator;
$lang='en-US';
echo generateForm ($lang,$country_list,$states_list,$days_of_month,$month_list,$years);
While this might or might not work for you, I would suggest taking a look at the Jquery UI Datepicker. It provides a very nice and highly customizable calendar that might be able to save you a lot of time.
Related
Update:
It looks like it is very complicated as I have had many views but no comments, could someone tell me how to check through the array {NAME} to find where it loses it's content please?
Original question:
I have been debugging an older php script and this is the last bit where I am struggling. The date does not show up when I have the drop down list:
This is what the output is in HTML:
<select name="trans_date2_month" style="width: {WIDTH}" >
<option value="{VALUE}" {SELECTED}>{NAME}</option>
<option value="{VALUE}" {SELECTED}>{NAME}</option>
<option value="{VALUE}" {SELECTED}>{NAME}</option>
<option value="{VALUE}" {SELECTED}>{NAME}</option>
<option value="{VALUE}" {SELECTED}>{NAME}</option>
<option value="{VALUE}" {SELECTED}>{NAME}</option>
<option value="{VALUE}" {SELECTED}>{NAME}</option>
<option value="{VALUE}" {SELECTED}>{NAME}</option>
<option value="{VALUE}" {SELECTED}>{NAME}</option>
<option value="{VALUE}" {SELECTED}>{NAME}</option>
<option value="{VALUE}" {SELECTED}>{NAME}</option>
<option value="{VALUE}" {SELECTED}>{NAME}</option>
</select>
Could someone help to find out how to track back in php please? I am more than happy to give more code's but I didn't want to overload the first page.
This was generated by the following code:
<!--S:DateSelectOption-->
<option value="{VALUE}" {SELECTED}>{NAME}</option>
<!--E:DateSelectOption-->
How to find {value} and {name}? - (please don't say that the search bar will help - I have done it:)
Update:
case "year":
if ($field["fields"]["year"]["empty"] == "true") {
$years[0] = "--" ;
}
for ($i = $field["fields"]["year"]["from"] ; $i <= $field["fields"]["year"]["to"] ; $i++ )
$years[$i] = $i;
$current_field .= $html->FormSelect(
$field["name"]."_year" ,
$years ,
$this->templates ,
"DateSelect",
$year_selected ,
array() ,
array("DISABLED" => ($field["fields"]["year"]["editable"] == "false") ? "DISABLED" : "")
);
$current_field .= $val["separator"];
break;
Update II:
A little bit more code, after searching for name... as a bad habit...
function HNDCData($parser,$cdata) {
$cdata = str_replace("[amp]","&",$cdata);
//echo "<br>" . $cdata;
// create the proper tree node if NAME attribute is set
if ($this->attr["NAME"] != "")
$this->tags[count($this->tags) - 1] = $this->attr["NAME"];
// cleanup cdata
$cdata = trim($cdata);
//$cdata = preg_replace("/(\015\012)|(\015)|(\012)/","",$cdata);
// only parse if cdata not void
if ($cdata != "") {
//print_r($this->attr);
//echo "<br>" . $cdata;
// prepare dynamic code
foreach ($this->tags as $key => $val)
$code[] = "\"" . strtolower($val) . "\"";
// build code
$code = "\$this->vars[" . implode("][",$code) . "] = \"" . $cdata . "\";";
// and finally execute
eval($code);
}
}
Update:
I have changed the for loop to numbers rather than variables but no difference.
for ($i = 2004 ; $i <= 2050 ; $i++ )
<select name="year"><?=options(date('Y'),1900)?></select>
<select name="month"><?php echo date('m');?><?=options(1,12,'callback_month')?></select>
<select name="day"><?php echo date('d');?><?=options(1,31)?></select>
PHP
function options($from,$to,$callback=false)
{
$reverse=false;
if($from>$to)
{
$tmp=$from;
$from=$to;
$to=$tmp;
$reverse=true;
}
$return_string=array();
for($i=$from;$i<=$to;$i++)
{
$return_string[]='<option value="'.$i.'">'.($callback?$callback($i):$i).'</option>';
}
if($reverse)
{
$return_string=array_reverse($return_string);
}
return join('',$return_string);
}
function callback_month($month)
{
return date('m',mktime(0,0,0,$month,1));
}
What should i make to get the current month and date in the corresponding dropdowns as starting values, as it is the case with years (2013).
Try like following:
Added new param type to options() function
<select name="year"><?=options('year' ,date('Y'),1900)?></select>
<select name="month"><?php echo date('m');?><?=options('month', 1,12,'callback_month')?></select>
<select name="day"><?php echo date('d');?><?=options('day', 1,31)?></select>
<?php
function options($type, $from,$to,$callback=false)
{
$reverse=false;
switch ($type)
{
case "year":
$current = date('Y');
break;
case "month":
$current = date('m');
break;
case "day":
$current = date('d');
break;
}
if($from>$to)
{
$tmp=$from;
$from=$to;
$to=$tmp;
$reverse=true;
}
$return_string=array();
for($i=$from;$i<=$to;$i++)
{
$selected = ($i == $current ? " selected" : "");
$return_string[]='<option value="'.$i.'" '.$selected.'>'.($callback?$callback($i):$i).'</option>';
}
if($reverse)
{
$return_string=array_reverse($return_string);
}
return join('',$return_string);
}
function callback_month($month)
{
return date('m',mktime(0,0,0,$month,1));
}
?>
just retreive the current date FIRST and use a for loop to subtract each year
Use selected Attribute:
e.g.
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi" selected>Audi</option>
</select>
Fiddle here: http://jsfiddle.net/dd3FU/
Just put it inside an if statement.
Enjoy!
Example... this query is displayed even though the ">" Greater Than option was selected
SELECT *
FROM sr_rounds
WHERE cir1 LIKE '%2.4%'
ORDER BY sr_id asc
Any ideas?
<form action="search_new.php" method="post" name="exchanges" id="exchanges">
<select name="circuits" id="circuits" onclick="searchIt()" onblur="searchIt()">
<option value="select" selected="selected">Select Search Field</option>
<option value="cir1">Circuit 1</option>
<option value="cir2">Circuit 2</option>
<option value="cir3">Circuit 3</option>
<option value="cir4">Circuit 4</option>
<option value="vch">VCH-1-5</option>
</select>
<select name="sorting" id="sorting" onclick="searchIt()" onblur="searchIt()">
<option value="select1" selected="selected">Sort By</option>
<option value="sr_id">Sector</option>
<option value="date">Date</option>
//This select always returns a "like" clause regardless of which option you select
</select>
<select name="clauses" id="clauses" onclick="searchIt()" onblur="searchIt()">
<option value="select2" selected="selected">Clause</option>
<option value="=">Equals</option>
<option value="like">Like</option>
<option value=">">Greater Than</option>
<option value="<">Less Than</option>
<option value="date">Date</option>
</select>
<select name="sortorder" id="sortorder" onclick="searchIt()" onblur="searchIt()">
<option value="select3" selected="selected">Order</option>
<option value="asc" name="1">Asc</option>
<option value="desc" name="2">Desc</option>
</select>
<?php
$select = $_POST['circuits'];
$searchdb = $_POST['searchdb'];
$select1 = $_POST['sorting'];
$select2 = $_POST['clauses'];
$select3 = $_POST['sortorder'];
//To display friendly field name instead of actual field name
if ($select == 'cir1')
{
$boxresult = 'Circuit 1';
}
if ($select == 'cir2')
{
$boxresult = 'Circuit 2';
}
if ($select == 'cir3')
{
$boxresult = 'Circuit 3';
}
if ($select == 'cir4')
{
$boxresult = 'Circuit 4';
}
if ($select == 'vch')
{
$boxresult = 'VCH-1-5';
}
//To use different queries while searching
if ($select2 == '=')
{
$queres = "SELECT * FROM sr_rounds WHERE $select = '$searchdb' ORDER BY $select1 $select3";
}
else if ($select2 == 'like')
{
$queres = "SELECT * FROM sr_rounds WHERE $select LIKE '$searchdb' ORDER BY $select1 $select3";
}
else if ($select2 == '>')
{
$queres = "SELECT * FROM sr_rounds WHERE $select > '$searchdb' ORDER BY $select1 $select3";
}
else if ($select2 == '<')
{
$queres = "SELECT * FROM sr_rounds WHERE $select < '$searchdb' ORDER BY $select1 $select3";
}
else if ($select2 == 'contain');
{
$queres = "SELECT * FROM sr_rounds WHERE $select LIKE '%$searchdb%' ORDER BY $select1 $select3";
}
$query = $queres;
$result = #mysql_query($query);
$num = #mysql_num_rows($result);
It is because you have a semi-colon in the line:
else if ($select2 == 'contain');
Make it
else if ($select2 == 'contain')
You have a typo in your code:
else if ($select2 == 'contain');
// ----------------------------^
This ends the if block. The next {} block executes always.
write this in your search_new.php:
var_dump($_POST); exit();
Check what clauses value is. I'd also go for another representation of the values. I tend to use numbers and reduce the if/else structure to a switch block, but anyway you can stick to whatever. Just an advice. Instead of >, < and =, use "greater", "lower" and "equals", or another word, as < and > are part of the xml structure, and there may be a problem for your browser to understand that. If you check the codeblock you wrote, you will see that the > is actually not acting well. It's painted black, instead of brown/red.
To sum up:
Check the value of $_POST
Change the option values to numbers or words, not symbols.
I have a list of items, each with a date/time option. I need for each list item to have the correct time displayed for it.
But this doesn't seem to work. It will return the correct 'am' or 'pm' value for $duedateA, but when I try to set the selected attribute value is gets wonky and will either show the wrong am or pm, or both.
Am I doing something wrong?
if(!empty($resInvoice[0]["due_date"])){
$duedateA = date("a",strtotime($resInvoice[0]['due_date']));
}
if($duedateA == "am"){
$selected_am = 'selected="selected"';
}
if($duedateA == "pm"){
$selected_pm = 'selected="selected"';
}
<select name="due_time[a]">
<option value="am" '.$selected_am.'>am</option>
<option value="pm" '.$selected_pm.'>pm</option>
</select>
date_default_timezone_set('America/New_York');
$resInvoice[0]["due_date"] = '2012-10-26 03:21:44';
$selected_am = '';
$selected_pm = '';
$duedateA = '';
if(!empty($resInvoice[0]["due_date"])){
$duedateA = date("a",strtotime($resInvoice[0]["due_date"]));
}
if($duedateA == "am"){
$selected_am = ' selected="selected"';
}
if($duedateA == "pm"){
$selected_pm = ' selected="selected"';
}
echo '<select name="due_date[a]">
<option value="am"'.$selected_am.'>AM</option>
<option value="pm"'.$selected_pm.'>PM</option>
</select>';
sorry for all the variables, i have my errors set to strict.
Using select and option HTML tags, I pass information through using $_POST.
When reloading the page however, the select resets back to the original values.
I am looking to get it to remember what has been passed through.
<?php
foreach($data as $value => $title)
{
foreach($ag as $first)
{
foreach($af as $second)
{
echo"<option value='$value-$first-$second'>$title - $first - $second</option>";
}
}
}
?>
As you can see, I use 3 foreach loops to populate whats in it.
How can I achieve my selection being remembered?
Thanks for reading.
You'll need to use the name of your select field in place of "your_select_field_name" in my change below:
<?php
foreach($data as $value => $title)
{
foreach($ag as $first)
{
foreach($af as $second)
{
echo "<option value='$value-$first-$second'";
if( $_POST['your_select_field_name'] == "$value-$first-$second" ) {
echo ' selected="selected"';
}
echo ">$title - $first - $second</option>";
}
}
}
?>
The output for the selected option on the new page needs to look like:
<option value='foo' selected>...<option>
HTML does not have memory. The item that's selected by default in a <select> form element is the one with the selected attribute (or the first one if none). Simply use the information contained in $_POST to generate the appropriate markup:
<select name="foo">
<option value="v1">Label 1</option>
<option value="v2">Label 2</option>
<option value="v2" selected="selected">Label 3</option>
<option value="v4">Label 4</option>
</select>
You need to set the selected tag on the correct option. Something like this:
<?php
$postedValue = $_POST['select_name'];
foreach($data as $value => $title)
{
foreach($ag as $first)
{
foreach($af as $second)
{
$computedValue = $value . '-' . $first . '-'. $second;
if ( $computedValue == $postedValue) {
$selected = "selected";
} else {
$selected = ''
}
echo "<option value='$computedValue' $selected>$title - $first - $second</option>";
}
}
}
?>
It could probably be written cleaner but this is the general idea.