php - Selecting the right data for a user -
i have 2 tables in data base
u_id of table tasks foreign key of id table users
my code
<?php require("common.php"); if(empty($_session['user'])) { header("location: login.php"); die("redirecting login.php"); } $query = " select id, username, eid users id <> '5' "; $query1 = " select t_id, task_tom, u_id tasks order t_id desc limit 1 "; try { $stmt = $db->prepare($query); $stmt->execute(); } catch(pdoexception $ex) { die("failed run query: " . $ex->getmessage()); } $rows = $stmt->fetchall(); try { $stmt1 = $db->prepare($query1); $stmt1->execute(); } catch(pdoexception $ex) { die("failed run query: " . $ex->getmessage()); } $rows1 = $stmt1->fetchall(); ?> <h1>memberlist</h1> <table border="1"> <tr> <th>username</th> <th>employee id</th> <th>task</th> </tr> <?php foreach($rows $row): foreach($rows1 $row1): ?> <tr> <td><?php echo htmlentities($row['username'], ent_quotes, 'utf-8'); ?></td> <td><?php echo htmlentities($row['eid'], ent_quotes, 'utf-8'); ?></td> <td><?php echo $row1['task_tom']; ?></td> <?php endforeach; endforeach;?> </table> <br/> <a href="edit_account.php">edit account</a><br /><a href="logout.php">logout</a>
my question how populate task column in table correspond right u_id.
code provide me answer
supposedly, ssis(data mining) task_tom assigned bassil , lawrence should have blank task because user didnt input task lawrence yet seems have flaw select statement or each wherein printing on table every user. question how solve kind of problem?
select u.id, u.username, u.eid, t.task_tom users u inner join ( select u_id, max(t_id) t_id tasks group u_id ) tmax on tmax.u_id = u.id inner join tasks t on t.t_id = tmax.t_id u.id <> '5'
this says "what latest task id each user, me info task".
Comments
Post a Comment