unemployment Posted January 29, 2011 Share Posted January 29, 2011 I cannot get this to echo out the day from mysql. Basically I need it to say if the field is empty show Day as selected=selected with the list of days, but if it isn't empty, make the day in mysql set to selected=selected. I have also check the query in mysql so I know the query is correct. <?php if (empty($r['dayofbirth'])) { $isset = isset($_POST['date_of_birth']); for ($day = 1; $day <= 31; ++$day) { echo '<option'; if ($isset && $_POST['date_of_birth'] === $day) { echo 'selected="selected"'; } echo ">${day}</option>"; } else { echo "${r['dayofbirth']}"; } ?> Quote Link to comment Share on other sites More sharing options...
ldb358 Posted January 29, 2011 Share Posted January 29, 2011 when you echo variables you can do it two ways either: echo ">$day</option>"; // or echo "{$r['dayofbirth']}"; Quote Link to comment Share on other sites More sharing options...
unemployment Posted January 29, 2011 Author Share Posted January 29, 2011 when you echo variables you can do it two ways either: echo ">$day</option>"; // or echo "{$r['dayofbirth']}"; That unfortunately did not help. Quote Link to comment Share on other sites More sharing options...
dragon_sa Posted January 29, 2011 Share Posted January 29, 2011 I believe ++$day should be $day++ also to set something as selected you only need to do this echo " selected"; also is $_POST['date_of_birth'] an actual number from 1-31 or is it the date of birth dd/mm/yyyy in which case it will never be true and also $isset = isset($_POST['date_of_birth']); should be if (isset($_POST['date_of_birth'])) { $isset=$_POST['date_of_birth']; } Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 29, 2011 Share Posted January 29, 2011 You're going to need to post more of the code for this. The database query you use to compare it against, and the rest of the date section of the form to start with. Also, what do you intend for this to do: $isset = isset($_POST['date_of_birth']); ? Quote Link to comment Share on other sites More sharing options...
unemployment Posted January 29, 2011 Author Share Posted January 29, 2011 You're going to need to post more of the code for this. The database query you use to compare it against, and the rest of the date section of the form to start with. Also, what do you intend for this to do: $isset = isset($_POST['date_of_birth']); ? I don't know. Maybe I'm being crazy. All I want to do is to display the date they have previously "saved" in the dropdown. I'm building a settings page and I need the date to be display and I also need the user to have the option to change this. This is my current code without the db even being fetched. if(isset($_POST['update'])) { $firstname = mres(strip_tags($_POST["firstname"])); $lastname = mres(strip_tags($_POST["lastname"])); $dateofbirth = mres(strip_tags("${_POST['yearofbirth']}-${_POST['monthofbirth']}-${_POST['date_of_birth']}")); $city = mres(strip_tags($_POST["city"])); $state = mres(strip_tags($_POST["state"])); $country = mres(strip_tags($_POST["country"])); $personalweb = mres(strip_tags($_POST["personalweb"])); $credentials = mres(strip_tags($_POST["credentials"])); $specialties = mres(strip_tags($_POST["specialties"])); mysql_query("UPDATE users SET firstname='$firstname', lastname='$lastname', city='$city', state='$state', country='$country', personalweb='$personalweb', credentials='$credentials', specialties='$specialties' WHERE username = '${username}'") or die(mysql_error()); echo "Update succesful!"; } $q = "SELECT `id`, `firstname`, `lastname`, `email`, `accounttype`, `personalweb`, `country`, `state`, `city`, `phonenumber`, `credentials`, `specialties`, `facebookurl`, `twitterurl`, `linkedinurl`, DATE_FORMAT(`signupdate`,'%D %M %Y') AS `signupdate`, DAY(`dateofbirth`) AS `dayofbirth`, MONTH(`dateofbirth`) AS `monthofbirth`, YEAR(`dateofbirth`) AS `yearofbirth` FROM `users` WHERE `username` = '${username}'"; $res = mysql_query($q); $r = mysql_fetch_assoc($res); <div class="formElement"> <div class="formFieldLabel"> <label for="date_of_birth" >Date of birth</label> </div> <div class="formField center"> <select name="date_of_birth" id="date_of_birth"> <option value="">Day</option> <?php $isset = isset($_POST['date_of_birth']); for ($day = 1; $day <= 31; ++$day) { echo '<option'; if ($isset && $_POST['date_of_birth'] === $day) { echo "selected"; } echo ">${day}</option>"; } ?> </select> <select name="monthofbirth"> <option value="">Month</option> <?php $isset = isset($_POST['monthofbirth']); for($i = 1; $i < 13; ++$i) { if ($isset && $_POST['date_of_birth'] === $i) { echo "<option value=\"${i}\" selected=\"selected\">${months[$i]}</option>"; } else { echo "<option value=\"${i}\">${months[$i]}</option>"; } } ?> </select> <select name="yearofbirth"> <option value="">Year</option> <?php $isset = isset($_POST['yearofbirth']); $start = $currentyear - 13; $end = $currentyear - 80; for($year = $start; $year >= $end; --$year) { if ($isset && $r['date_of_birth'] === $year) { echo "<option value=\"${year}\" selected=\"selected\">${year}</option>"; } else { echo "<option value=\"${year}\">${year}</option>"; } } ?> </select> </div> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 29, 2011 Share Posted January 29, 2011 There's no need to enclose a simple variable in curly braces to echo it. striptags() is unnecessary when inserting into a database, it should be used when you retrieve and display the data. This should work, as long as the data from the database query is present and in the anticipated format. <?php if(isset($_POST['update'])) { $firstname = mres(strip_tags($_POST["firstname"])); $lastname = mres(strip_tags($_POST["lastname"])); $dateofbirth = mres(strip_tags("{$_POST['yearofbirth']}-{$_POST['monthofbirth']}-{$_POST['date_of_birth']}")); $city = mres(strip_tags($_POST["city"])); $state = mres(strip_tags($_POST["state"])); $country = mres(strip_tags($_POST["country"])); $personalweb = mres(strip_tags($_POST["personalweb"])); $credentials = mres(strip_tags($_POST["credentials"])); $specialties = mres(strip_tags($_POST["specialties"])); mysql_query("UPDATE users SET firstname='$firstname', lastname='$lastname', city='$city', state='$state', country='$country', personalweb='$personalweb', credentials='$credentials', specialties='$specialties' WHERE username = '$username'") or die(mysql_error()); echo "Update succesful!"; } $q = "SELECT `id`, `firstname`, `lastname`, `email`, `accounttype`, `personalweb`, `country`, `state`, `city`, `phonenumber`, `credentials`, `specialties`, `facebookurl`, `twitterurl`, `linkedinurl`, DATE_FORMAT(`signupdate`,'%D %M %Y') AS `signupdate`, DAY(`dateofbirth`) AS `dayofbirth`, MONTH(`dateofbirth`) AS `monthofbirth`, YEAR(`dateofbirth`) AS `yearofbirth` FROM `users` WHERE `username` = '$username'"; $res = mysql_query($q); $r = mysql_fetch_assoc($res); ?> <div class="formElement"> <div class="formFieldLabel"> <label for="date_of_birth" >Date of birth</label> </div> <div class="formField center"> <select name="date_of_birth" id="date_of_birth"> <option value="">Day</option> <?php $day = range(1, 31); foreach( $day as $v ) { echo "<option value=\"$v\""; echo $r['dayofbirth'] == $v ? 'selected="selected"' : ''; echo ">$v</option>\n"; } ?> </select> <select name="monthofbirth"> <option value="">Month</option> <?php $months = array( 1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); foreach( $months as $k => $v ) { echo "<option value=\"$k\""; echo $r['monthofbirth'] == $k ? 'selected="selected"' : ''; echo ">$v</option>\n"; } ?> </select> <select name="yearofbirth"> <option value="">Year</option> <?php $start = date('Y', time() ) - 13; $end = date('Y', time() ) - 80; $year = range($start, $end); foreach( $year as $v ) { echo "<option value=\"$v\""; echo $r['yearofbirth'] == $v ? 'selected="selected"' : ''; echo ">$v</option>\n"; } ?> </select> </div> 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.