Hi

I am trying to allow users to update records in a MySql database through php forms. One problem I am having is with a drop down field for dates. I want to echo the date already in the database so that the drop down menus is set to that before they change the date, but I do not know how to do it. Does anyone have any suggestions?

Also what is the best way to echo the info in the database into html forms so that they can change it if they want?

Thanks in advance

Recommended Answers

All 6 Replies

A general answer:

  1. read the record that the user is about to change
  2. put each field value as a value of a html form field
  3. for select html elements (drop down as you call it) put a set of option in an array, loop through that array and on each loop compare a value of the element with the value of the field. If they are the same, set selected attribute for that option.

A simple example just to get the idea:

// a query to get the fields
$qry = "SELECT * FROM table WHERE record_id=$ome_id";

// run the query
$result = mysql_query($qry) or die('Some error occured.');

// an array with fields of the querried row
$row = mysql_fetch_array( $result );

// start the form
echo '<form method="post" action="some_page.php">';

// an example of input text element with the value from database field
echo '<input type="text" name="input_name" value="' . $row['field1'] . '" />';

// an example of select element with values from a $dates_array and a selected
// option from database field
$dates_array = array('1.4.2012','2.4.2012','3.4.2012','4.4.2012','5.4.2012',);

// begin the select element
echo '<select name="date_select">';
// loop through the dates_array and add options
foreach($dates_array as $one_date) {
    // start the option tag
    echo '<option value="' . $one_date . '"';
    // if current element of the date_array equals to the date, read from the
    // database, add selected attribute
    if($one_date == $row['date_field']) {
        echo ' selected="selected"';
    }
    // end the option, add the text value for the drop down, end the option tag
    echo '>$one_date</option>';
}
// end the select element
echo '</select>';

// end the form
echo '</form>';
commented: spot on +5

just to add a little - If you want a way of generating many dates without having to type them all out use strtotime:

$i = 0;
while($i <= 60){
    $date_Array[] = date("Y-m-d",strtotime("-$i day"));
    $i++;
}

strtotime()

I could not find any information on whether there are any restrictions for an option value attribute (<option value="any_restrictions_here?">) .I hope dots are permitted. I think browsers encode values to be safe for sending to a server.

Thanks for the help. I have done everything above by in the <select> part it is echoing $one_date, do you know why this might be?

Also I think I understand the code apart from the echo ' selected="selected"'; part, could you explain this to me?

Thanks for the help so far!

the <select> part it is echoing $one_date

This is due to my mistake. In line 32 double quotes should be used (to allow $one_date to be parsed):

echo ">$one_date</option>";

' selected="selected"'; part, could you explain this to me?

This is just a part of html syntax for select element. If an <option> has a selected="selected" attribute that option is selected. In the code above there is a foreach loop that creates <option> parts from an array (the $date_array in your case) for the select element. Within each iteration it checks whether the curent value of an array equals to the value read from the database. If it finds one the attribute selected="selected" is added to that option. Once it works OK have look at the HTML source in the browser.

See also http://www.w3schools.com/tags/tag_option.asp

Brilliant, it works now. Thank you so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.