Lisa23 Posted September 25, 2010 Share Posted September 25, 2010 Hi i have this drop down list for date which contain 3 selects DAY MONTH YEAR hwo can i make so that when update form select keeps the same value has before help please <?php $months = array('','January','February','March','April','May','June','July','August','September','October','November','December'); echo '<select name="month_of_birth">'; for ($i=1;$i<13;++$i) { echo '<option value="' . sprintf("%02d",$i) . '">' . $months[$i] . '</option>'; } echo '</select>'; echo '<select name="day_of_birth">'; for ($i=1;$i<32;++$i) { echo '<option value="' . sprintf("%02d",$i) . '">' . $i . '</option>'; } echo '</select>'; echo '<select name="year_of_birth">'; $year = date("Y"); for ($i = $year;$i > $year-50;$i--) { $s = ($i == $year)?' selected':''; echo '<option value="' . $i . '" ' . $s . '>' . $i . '</option>'; } echo '</select>'; ?> Quote Link to comment Share on other sites More sharing options...
s0c0 Posted September 25, 2010 Share Posted September 25, 2010 I will want to programatically input selected for a given option. Here's some quick example code: <? $monthArr = array(1,2,3,4,5,6,7,8,9,10,11,12); echo '<select name="month">' foreach($monthArr as $i){ echo '<option value="'.$i.'" '.(($_POST['month']==$i)?'selected':'').'>'.$i.'</option>'; } echo '</select>'; ?> Quote Link to comment Share on other sites More sharing options...
Lisa23 Posted September 25, 2010 Author Share Posted September 25, 2010 didnt work Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted September 25, 2010 Share Posted September 25, 2010 Hey Lisa. Are you using the 3 select fields in a form and posting the form to another page, or the same page as the form? This will help us give you a better solution. Regards, Paul. Quote Link to comment Share on other sites More sharing options...
Lisa23 Posted September 25, 2010 Author Share Posted September 25, 2010 another page :'( Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted September 25, 2010 Share Posted September 25, 2010 Sorry I forgot to ask When you have submitted the form and you've processed form data, are you re-drected to the form again? If so you will want to look into $_SESSION (URL: http://www.tizag.com/phpT/phpsessions.php) Have a look over that site and then have a little go yourself, if you still need help post back Regards, Paul. Quote Link to comment Share on other sites More sharing options...
Lisa23 Posted September 25, 2010 Author Share Posted September 25, 2010 well when i submit the from it takes u to the action page which has the script to post the data and gives an echo form submited what i have is an edit form that allow admin to modify the data only problem that option changes the value evrytime to first option of the select which i want to make keep the one that comes from the database Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted September 25, 2010 Share Posted September 25, 2010 Hmm a little confuzzled here... Are you getting the data from a database? Regards, Paul. Quote Link to comment Share on other sites More sharing options...
Lisa23 Posted September 25, 2010 Author Share Posted September 25, 2010 yes its a edit form wher the admin uses the modify the data so all the data are already on the database the admin can just edit bk in case he wants to modify i did it with the inputs but now with this options script i dnt know how?? whenevr edit it sets the date back to first option which user has to change again i dnt want that i want to bring the values already from the databse Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted September 25, 2010 Share Posted September 25, 2010 Ohh right I see, it's simple when explained better huh? Try this... $thisMonth = 'mysql month'; $thisDay = 'mysql day'; $thisYear = 'mysql year'; $months = array('','January','February','March','April','May','June','July','August','September','October','November','December'); echo '<select name="month_of_birth">',"\n"; for ($i=1;$i<13;++$i) { if($i == $thisMonth) { $s = ' selected'; } else { $s=''; } echo '<option value="' ,$i, '"',$s,'>' ,$months[$i], '</option>',"\n"; } echo '</select>',"\n"; echo '<select name="day_of_birth">',"\n"; for ($i=1;$i<32;++$i) { if($i == $thisDay) { $s = ' selected'; } else { $s=''; } echo '<option value="' ,$i, '"',$s,'>' ,$i, '</option>',"\n"; } echo '</select>',"\n"; echo '<select name="year_of_birth">',"\n"; $year = date("Y"); for ($i = $year;$i > $year-50;$i--) { if($i == $thisYear) { $s = ' selected'; } else { $s=''; } echo '<option value="' ,$i, '"',$s,'>' ,$i, '</option>',"\n"; } echo '</select>',"\n"; Just change the variables at the top to the data that comes from your database and it should be good to go. Tell me how it goes for you Regards, Paul. Quote Link to comment Share on other sites More sharing options...
Lisa23 Posted September 25, 2010 Author Share Posted September 25, 2010 Just change the variables at the top to the data that comes from your database change what?? from where? sorry didnt get it Quote Link to comment Share on other sites More sharing options...
Lisa23 Posted September 25, 2010 Author Share Posted September 25, 2010 sorry didnt understand change what variables and to what?? Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted September 26, 2010 Share Posted September 26, 2010 The data that comes from you database E.G. month, day and year $thisMonth = 'mysql month'; $thisDay = 'mysql day'; $thisYear = 'mysql year'; Change the above values to your database values something like.. $thisMonth = $row['month']; $thisDay = $row['day']; $thisYear = $row['year']; OR whatever your mysql_query result is. Post back if you still need help Regards, Paul. Quote Link to comment Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 i tried like this because all the date is coming from the same colum name ( date_of_birth) it only retrived the year from the database the rest set bk to first option $thisMonth = $row['date_of_birth']; $thisDay = $row['date_of_birth']; $thisYear = $row['date_of_birth']; Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted September 26, 2010 Share Posted September 26, 2010 Is $row['date_of_birth'] in the following format 12-25-1996 or something similar? Tell me what $row['date_of_birth'] outputs and I'll sort it for you Regards, Paul. Quote Link to comment Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 its in this format 0000-00-00 and i think i need to change somthing on the mysql update current is like this $date_of_birth = $_POST['year_of_birth'] . '-' . $_POST['month_of_birth'] . '-' . $_POST['day_of_birth']; $query = "UPDATE driversnew SET name = '$name', location = '$location', date_of_birth='$date_of_birth', car_number='$car_number', favourite_track='$favourite_track', least_favourite_track='$least_favourite_track', achievements='$achievements', sponsors='$sponsors', email='$email', display='$display'"; on the insert which works fine the mysl insert is like this just to show u this id fine $date_of_birth = $_POST['year_of_birth'] . '-' . $_POST['month_of_birth'] . '-' . $_POST['day_of_birth']; $q = "INSERT INTO driversnew (id, name, location, date_of_birth, car_number, favourite_track, least_favourite_track, achievements, sponsors, email, image, display) VALUES ('$_POST[id]', '$_POST[name]', '$_POST[location]','$date_of_birth','$_POST[car_number]','$_POST[favourite_track]', '$_POST[least_favourite_track]','$_POST[achievements]', '$_POST[sponsors]','$_POST[email]', '$image_name','0')"; Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted September 26, 2010 Share Posted September 26, 2010 Nah you don't need to change anything Lisa, it can be done on the PHP side of things, I'll quickly update the script I gave you earlier Regards, Paul. Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted September 26, 2010 Share Posted September 26, 2010 Try the following Lisa. <?php $getDate = $row['date_of_birth']; $splitDate = explode('-',$getDate); $thisMonth = $splitDate[1]; $thisDay = $splitDate[2]; $thisYear = $splitDate[0]; $months = array('','January','February','March','April','May','June','July','August','September','October','November','December'); echo '<select name="month_of_birth">',"\n"; for ($i=1;$i<13;++$i) { if($i == $thisMonth) { $s = ' selected'; } else { $s=''; } echo '<option value="' ,$i, '"',$s,'>' ,$months[$i], '</option>',"\n"; } echo '</select>',"\n"; echo '<select name="day_of_birth">',"\n"; for ($i=1;$i<32;++$i) { if($i == $thisDay) { $s = ' selected'; } else { $s=''; } echo '<option value="' ,$i, '"',$s,'>' ,$i, '</option>',"\n"; } echo '</select>',"\n"; echo '<select name="year_of_birth">',"\n"; $year = date("Y"); for ($i = $year;$i > $year-50;$i--) { if($i == $thisYear) { $s = ' selected'; } else { $s=''; } echo '<option value="' ,$i, '"',$s,'>' ,$i, '</option>',"\n"; } echo '</select>',"\n"; ?> Regards, Paul. Quote Link to comment Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 Paul your a genious its working one last thing how do i do the same thing on this one i tried like like belllow bt no sucess once again i want the data to cum from the datbase which is value 1 or 2?? sorry for the ben a bug <select name="display"> <option selected="selected" value ="1">1</option> <option selected="selected" value ="0">1</option> </select> Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted September 26, 2010 Share Posted September 26, 2010 What is the fieldname called that holds this data in the database? ++You want the option selected that the variable equals? For example $row['display'] will display 1 or 2 you want the correct one selected... Try something like... <select name="display"> <option <?php if($row['display'] == '1') { echo 'selected'; } ?> value="1">1</option> <option <?php if($row['display'] == '2') { echo 'selected'; } ?> value="2">2</option> </select> Regards, Paul. Quote Link to comment Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 column name (display) Quote Link to comment Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 no that way did not work and also when u look on the form it display with ( value ="1">1 ) is meant to be just 1 Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted September 26, 2010 Share Posted September 26, 2010 Ohh right, erm could you show me your processing page source? It will make it alot easier for me to help you Lisa Regards, Paul. Quote Link to comment Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 here it is is the same as the u fixed earlier on $date_of_birth = $_POST['year_of_birth'] . '-' . $_POST['month_of_birth'] . '-' . $_POST['day_of_birth']; $query = "UPDATE driversnew SET name = '$name', location = '$location', date_of_birth='$date_of_birth', car_number='$car_number', favourite_track='$favourite_track', least_favourite_track='$least_favourite_track', achievements='$achievements', sponsors='$sponsors', email='$email', display='$display'"; Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted September 26, 2010 Share Posted September 26, 2010 So $row['display'] returns either 1 or 2... Using the 1 or 2 you want to select the correct option... which one does 1 select and which one does 2 select? For example if $row['display'] equals 1 you want <select name="display"> <optionvalue ="1" selected>1</option> <option value ="0">0</option> </select> or if $row['display'] equals 2 you want <select name="display"> <optionvalue ="1">1</option> <option value ="0" selected>0</option> </select> Regards, a slightly confused Paul. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.