fork download
  1. <?php
  2.  
  3. try {
  4.  
  5. $dbname = '';
  6. $dbuser = '';
  7. $dbpass = '';
  8. $sql = 'SELECT * FROM tableA JOIN tableB ON tableA.staff_id = tableB.staff_num WHERE s_date = %s';
  9.  
  10. switch (true) {
  11. case !isset($_REQUEST['date']):
  12. case !is_string($date = $_REQUEST['date']):
  13. throw new Exception('日付を指定してください');
  14. case !($link = @mysql_connect('localhost', $dbuser, $dbpass)):
  15. case !mysql_select_db($dbname, $link):
  16. case !mysql_set_charset('utf8', $link):
  17. case !($result = mysql_query(sprintf($sql, mysql_real_escape_string($date, $link)), $link)):
  18. throw new Exception(mysql_error($link));
  19. }
  20.  
  21. $rows = array();
  22. while ($tmp = mysql_fetch_assoc($result)) {
  23. $rows[] = $tmp;
  24. }
  25.  
  26. array_walk_recursive($rows, function (&$v, $k) {
  27. if (strpos($k, 'cell') !== 0) {
  28. return;
  29. }
  30. $values = explode(',', $v, 3);
  31. if (!isset($values[2])) {
  32. $values = array_fill(0, 3, '-');
  33. }
  34. $keys = array('time', 'place', 'flag');
  35. $v = array_combine($keys, $values);
  36. });
  37.  
  38. } catch (Exception $e) {
  39.  
  40. $error = htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8');
  41.  
  42. }
  43.  
  44. ?>
  45. <!DOCTYPE html>
  46. <html>
  47. <head>
  48. <meta charset="UTF-8">
  49. <title>Test</title>
  50. </head>
  51. <body>
  52. <?php if (isset($error)): ?>
  53. <p><?=$error?></p>
  54. <?php elseif (!$rows): ?>
  55. <p>データが見つかりませんでした</p>
  56. <?php else: ?>
  57. <?php foreach ($rows as $row): ?>
  58. <table border>
  59. <caption><?=$row['staff_name']?>(<?=$row['s_date']?>)</caption>
  60. <tr>
  61. <th>場所</th>
  62. <th>開始時間</th>
  63. </tr>
  64. <?php foreach (array_slice($row, 2, -3) as $r): ?>
  65. <?php if ($r['time'] === '-') { continue; }?>
  66. <tr class="style<?=$r['flag']?>">
  67. <td><?=$r['place']?></td>
  68. <td><?=$r['time']?></td>
  69. </tr>
  70. <?php endforeach; ?>
  71. </table>
  72. <?php endforeach; ?>
  73. <?php endif; ?>
  74. </body>
  75. </html>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
<p>日付を指定してください</p>
</body>
</html>