Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 04/10/2022 in all areas

  1. For completeness, my processing would be if ($_SERVER['REQUEST_METHOD']=='POST') { try { $db->beginTransaction(); $stmt1 = $db->prepare("DELETE FROM user_transport WHERE user_id = ? "); $stmt1->execute( [ $_POST['uid'] ] ); if (isset($_POST['trans'])) { $stmt2 = $db->prepare("INSERT INTO user_transport (user_id, trans_type) VALUES (?, ?) "); foreach ($_POST['trans'] as $tid) { $stmt2->execute( [ $_POST['uid'], $tid ] ); } } $db->commit(); } catch(PDOException $e) { $db->rollBack(); throw $e; } header("Refresh: 0"); exit; } $res = $db->query("SELECT u.user_id , u.fname , u.lname , tt.description , tt.trans_type , CASE WHEN ut.user_id IS NULL THEN 0 ELSE 1 END AS checked FROM user_demo u CROSS JOIN transport_type tt LEFT JOIN user_transport ut ON u.user_id = ut.user_id AND tt.trans_type = ut.trans_type ORDER BY lname, trans_type "); $data = []; // store results in a conveniently structured array foreach ($res as $r) { if (!isset($data[$r['user_id']])) { $data[$r['user_id']] = [ 'name' => "{$r['fname']} {$r['lname']}", 'trans' => [] ]; } $data[$r['user_id']]['trans'][$r['trans_type']] = ['desc' => $r['description'], 'check' => $r['checked']]; } // echo '<pre>' . print_r($data, 1) . '</pre>'; # view array structure ?> <!DOCTYPE html> <html lang='en'> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="generator" content="PhpED 19.5 (Build 19523, 64bit)"> <title>Example</title> <meta name="author" content="Barand"> <meta name="creation-date" content="04/09/2022"> <style type='text/css'> table { border-collapse: collapse; margin: 20px auto; width: 60%; } td, th { padding: 8px; } </style> </head> <body> <table border='1'> <tr> <th>Name</th> <th>Transport types</th> <th>&nbsp;</th> </tr> <?php // output the array foreach ($data as $uid => $udata) { echo "<tr> <form method='post'> <td>{$udata['name']}</td> <td>"; foreach ($udata['trans'] as $ttype => $tdata) { $chk = $tdata['check'] ? 'checked' : ''; echo "<label><input type='checkbox' name='trans[]' value='$ttype' $chk>{$tdata['desc']}</label><br>"; } echo "</td> <td> <input type='hidden' name='uid' value='$uid'> <input type='submit' value='Update'> </td> </form> </tr> "; } ?> </table> </body> </html> Results
    1 point
This leaderboard is set to New York/GMT-04:00
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.