php - Dynamically change selected state of dropdown(s) for each row of data -
i'd show dropdown menu(s) contain selected day based on recorded on database.
is there efficient way dynamically change selected state of dropdown menu based on recorded data?
thank you
note:
there many dropdown menu(s) if recorded day of following clinicid more 1 row
the $day integer, 1 sunday, 2 monday , on
here mycode
// check if row existed if ($count>0) { // if row existed start printing while($row = mysql_fetch_assoc($retval)) { $day = $row['day']; $starthour = $row['starthour']; $startmin = $row['startmin']; $endhour = $row['endhour']; $endmin = $row['endmin']; echo "<span>" . "<select name='day[]'>" . "<option value='1' selected='selected'>sunday</option>" . "<option value='2'>monday</option>" . "<option value='3'>tuesday</option>" . "<option value='4'>wednesday</option>" . "<option value='5'>thursday</option>" . "<option value='6'>friday</option>" . "<option value='7'>saturday</option>" . "<option value='0'>everyday</option>" . "</select>" //please ignore below "<br>start : " . $starthour . "." . $startmin . "<br>end : " . $endhour . "." . $endmin . "<br><br>"; } } else { }
if new code, please use pdo or mysqli. mysql depreciated , should not used on new code php.net/manual/en/function.mysql-query.php try link, helped me lot: phpdelusions.net/pdo
change code (this code includes pdo implementation):
<?php $db = new pdo('mysql:host=yourhost;dbname=dbname', 'username', 'password', array(pdo::mysql_attr_init_command => "set names 'utf8'")); $db->setattribute(pdo::attr_emulate_prepares, false); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception); //printing schedule in database $getbusinesshours = "select * businesshours clinicid = $clinicid"; $stmt = $db->prepare($getbusinesshours); $stmt->execute(); $count = $stmt->rowcount(); // check if row existed if ($count>0){ // if row existed start printing foreach ($stmt $row){ { $day = $row['day']; $starthour = $row['starthour']; $startmin = $row['startmin']; $endhour = $row['endhour']; $endmin = $row['endmin']; $i = 0; $days = array('everyday', 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'); $select = '<span><select name="day[]">'; while($i <= 7){ if($i == $day){ $selected = 'selected="selected"'; } else{ $selected = ''; } $select = $select. '<option value="'.$i.'"'.$selected.'>'.$days[$i].'</option'> $i++; } $select = $select.'</select>'; echo $select; //please ignore below "<br>start : " . $starthour . "." . $startmin . "<br>end : " . $endhour . "." . $endmin . "<br><br>"; } } else { } ?>
i hope helps.
Comments
Post a Comment