html - PHP issue with checkbox value -
html - PHP issue with checkbox value -
i have php code:
class="lang-php prettyprint-override">function maketable($data){ $ordered = array_map(function($day) { homecoming array_keys($day); }, $data); $headers = array_map(function($a) { homecoming "<th>$a</th>"; }, array_keys($ordered)); $max_indice = max( array_map(function($day) { homecoming count($day) - 1; }, $ordered) ); $rows = array_map(function($i) utilize ($ordered) { homecoming '<tr>' . join( array_map(function($day) utilize ($i) { homecoming '<td><input type="checkbox" name="datetime" id="datetime" value="' . $i . '" />' . $day[$i] . '</td>'; }, $ordered)) . '</tr>'; }, range(0, $max_indice)); echo "<table border='1'>".join($headers)."<tbody>".join($rows)."</tbody></table>"; } maketable($datesavailablearray); what function does, takes array of $datesavailablearray:
array ( [saturday] => array ( [11:00am] => 0 [12:00pm] => 0 [1:00pm] => 0 [2:00pm] => 0 [3:00pm] => 0 [4:00pm] => 0 [5:00pm] => 0 ) [sunday] => array ( [11:00am] => 0 [12:00pm] => 0 [1:00pm] => 0 [2:00pm] => 0 [3:00pm] => 0 [4:00pm] => 0 [5:00pm] => 0 ) [monday] => array ( [11:00am] => 0 [12:00pm] => 0 [1:00pm] => 0 [2:00pm] => 0 [3:00pm] => 0 [4:00pm] => 0 [5:00pm] => 0 ) [tuesday] => array ( [11:00am] => 0 [12:00pm] => 0 [1:00pm] => 0 [2:00pm] => 0 [3:00pm] => 0 [4:00pm] => 0 [5:00pm] => 0 ) [wednesday] => array ( [11:00am] => 0 [12:00pm] => 0 [1:00pm] => 0 [2:00pm] => 0 [3:00pm] => 0 [4:00pm] => 0 [5:00pm] => 0 ) [thursday] => array ( [11:00am] => 0 [12:00pm] => 0 [1:00pm] => 0 [2:00pm] => 0 [3:00pm] => 0 [4:00pm] => 0 [5:00pm] => 0 ) [friday] => array ( [11:00am] => 0 [12:00pm] => 0 [1:00pm] => 0 [2:00pm] => 0 [3:00pm] => 0 [4:00pm] => 0 [5:00pm] => 0 ) ) and makes html table:
class="lang-html prettyprint-override"><table border="1"> <tbody> <tr> <th> saturday nov 15th </th> <th> sunday nov 16th </th> <th> mon nov 17th </th> <th> tuesday nov 18th </th> <th> quarta-feira nov 19th </th> <th> th nov 20th </th> <th> fri nov 21st </th> </tr> </tbody> <tbody> <tr> <td> <input type="checkbox" name="datetime" id="datetime" value="0"> 11:00am </td> <td> <input type="checkbox" name="datetime" id="datetime" value="0"> 11:00am </td> <td> <input type="checkbox" name="datetime" id="datetime" value="0"> 11:00am </td> <td> <input type="checkbox" name="datetime" id="datetime" value="0"> 11:00am </td> <td> <input type="checkbox" name="datetime" id="datetime" value="0"> 11:00am </td> <td> <input type="checkbox" name="datetime" id="datetime" value="0"> 11:00am </td> <td> <input type="checkbox" name="datetime" id="datetime" value="0"> 11:00am </td> </tr> <tr> <td> <input type="checkbox" name="datetime" id="datetime" value="1"> 12:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="1"> 12:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="1"> 12:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="1"> 12:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="1"> 12:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="1"> 12:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="1"> 12:00pm </td> </tr> <tr> <td> <input type="checkbox" name="datetime" id="datetime" value="2"> 1:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="2"> 1:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="2"> 1:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="2"> 1:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="2"> 1:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="2"> 1:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="2"> 1:00pm </td> </tr> <tr> <td> <input type="checkbox" name="datetime" id="datetime" value="3"> 2:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="3"> 2:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="3"> 2:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="3"> 2:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="3"> 2:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="3"> 2:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="3"> 2:00pm </td> </tr> <tr> <td> <input type="checkbox" name="datetime" id="datetime" value="4"> 3:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="4"> 3:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="4"> 3:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="4"> 3:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="4"> 3:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="4"> 3:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="4"> 3:00pm </td> </tr> <tr> <td> <input type="checkbox" name="datetime" id="datetime" value="5"> 4:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="5"> 4:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="5"> 4:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="5"> 4:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="5"> 4:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="5"> 4:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="5"> 4:00pm </td> </tr> <tr> <td> <input type="checkbox" name="datetime" id="datetime" value="6"> 5:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="6"> 5:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="6"> 5:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="6"> 5:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="6"> 5:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="6"> 5:00pm </td> <td> <input type="checkbox" name="datetime" id="datetime" value="6"> 5:00pm </td> </tr> </tbody> </table> what trying set heading date checkbox value. possible code?
first of going want alter $datesavailablearray contains actual dates. iso 8601 strings first-class format.
$datesavailablearray = array( 'saturday' => array( '2014-11-14t11:00:00z', '2014-11-14t12:00:00z', '2014-11-14t13:00:00z', '2014-11-14t14:00:00z' ), 'sunday' => array( '2014-11-15t11:00:00z', '2014-11-15t12:00:00z', '2014-11-15t13:00:00z', '2014-11-15t14:00:00z' ) ); now have date strings can create datetime objects utilize format date:
$saturday_1100 = datetimeimmutable::createfromformat(datetime::iso8601, '2014-11-14t11:00:00z'); $saturday_1100->format('g:ia'); // 11:00am in our case using datetimeimmutable class since don't want alter dates after created.
so lets alter our maketable function utilize our new friend datetimeimmutable:
function maketable($data) { $headers = array_map(function($a) { homecoming "<th>$a</th>"; }, array_keys($data)); $max_indice = max( array_map(function($day) { homecoming count($day) - 1; }, $data) ); // create new array grouping hr $rows = array_map(function($i) utilize ($data) { // create each hr homecoming '<tr>' . join( array_map(function($day) utilize ($i){ $ts = isset($day[$i]) ? datetimeimmutable::createfromformat(datetime::iso8601, $day[$i]) : null; if ($ts){ $label = '<label for="datetime">'.$ts->format('g:ia').'</label>'; $input = vsprintf ( '<input type="checkbox" name="datetime" id="%s" value="%s" />', array( 'datetime_'. $ts->gettimestamp(), // id $ts->format('c') // value ) ); homecoming '<td>' . $label . $input . '<td>'; } else { homecoming '<td></td>'; } }, $data)) . '</tr>'; }, range(0, $max_indice)); // don´t rid of thead element. // tells browser table real table containing info , not crappy layout. homecoming " <table> <caption>my table</caption> <thead>".join($headers,'\n')."</thead> <tbody>".join($rows, '\n')."</tbody> </table>"; } echo maketable( $datesavailablearray ); give this:
<table> <caption> table </caption> <thead> <tr> <th>saturday</th> <th>sunday</th> </tr> </thead> <tbody> <tr> <td><label for="datetime">11:00am</label><input type="checkbox" name="datetime" id="datetime_1415962800" value="2014-11-14t11:00:00+00:00" /></td> <td><label for="datetime">11:00am</label><input type="checkbox" name="datetime" id="datetime_1416049200" value="2014-11-15t11:00:00+00:00" /></td> </tr> <tr> <td><label for="datetime">12:00pm</label><input type="checkbox" name="datetime" id="datetime_1415966400" value="2014-11-14t12:00:00+00:00" /></td> <td><label for="datetime">12:00pm</label><input type="checkbox" name="datetime" id="datetime_1416052800" value="2014-11-15t12:00:00+00:00" /></td> </tr> <!-- more ... --> </tbody> </table> summary:
we have unique values input id. value send when posting form iso-8601 date string. have wrapped our hr text in <label> tags attribute accessibility.
php html
Comments
Post a Comment