我完全被困在这里,提前感谢任何解决方案或指示,我真的非常感谢您提供的任何帮助。
我正在尝试制作备忘录系统。显示备忘录是没问题的,但我试图只显示未读的备忘录。你可以看到我有2个表,一个有备忘录,另一个列出了阅读备忘录。 当用户阅读备忘录时,他/她勾选该框,并将备忘录ID和用户ID添加到表'memo_read'中。
所以基本上我试图检索从'memo_read'表中读取的备忘录,然后显示'memo'表中未读取user_id和公司正确的结果。 正如你所看到的,我在另一个页面上有一个函数的2个变量,这些变量正常工作'$ user_id,$ company_id',
我试图在显示所需变量的while循环中显示未读的备忘录。我一直在研究这个问题多年没有快乐,我已经尝试了我能想到的每一种方法,但我相信你可以说我不是最有经验的人,我确信我错过了一些东西或有采取了完全错误的方法!
<?php
$user_id = $user_data['user_id'] ;
$company_id = $user_data['company'];
$result1 = mysql_query ("SELECT `user`, `memo_id` FROM`memo_read` WHERE `user`= '$user_id '") or die(mysql_error());
while($row = mysql_fetch_array($result1)){
$memo_id = $row['memo_id'];
$result = mysql_query ("SELECT `id`, `link`, `author`, `time`, `title`, `company` FROM `memo` WHERE `worker`= 1 AND `company`= '$company_id' AND `id`!= '$memo_id'") or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$id = $row['id'];
$link = $row['link'];
$author = $row['author'];
$time = $row['time'];
$title = $row['title'];
$company = $row['company'];
?>
<ul>
<li><?php echo $title; ?></li>
<li><?php echo $author; ?></li>
<li><?php echo $company;?></li>
<li><?php echo $time;?></li>
<a href="<?php echo $link; ?>"> Read memo</a>
</ul><br>
<?php
} }
?>
答案 0 :(得分:2)
为什么不使用返回未读备忘录的查询? 类似的东西:
SELECT `id`,
`link`,
`author`,
`time`,
`title`,
`company`
FROM `memo`
WHERE `worker`= 1
AND `company`= '$company_id'
AND `id` NOT IN (SELECT `memo_id`
FROM `memo_read`
WHERE `user`= '$user_id '
)
答案 1 :(得分:1)
试试这个:
<?php
$user_id = $user_data['user_id'] ;
$company_id = $user_data['company'];
$result1 = mysql_query ("SELECT `user`, `memo_id` FROM`memo_read` WHERE `user`= '$user_id '") or die(mysql_error());
$memo_id_query = '';
while($row = mysql_fetch_array($result1)){
$memo_id_query .= " AND `id`!= '".$row['memo_id']."'";
}
$result = mysql_query ("SELECT `id`, `link`, `author`, `time`, `title`, `company` FROM `memo` WHERE `worker`= 1 AND `company`= '$company_id' ".$memo_id_query) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$id = $row['id'];
$link = $row['link'];
$author = $row['author'];
$time = $row['time'];
$title = $row['title'];
$company = $row['company'];
?>
<ul>
<li><?php echo $title; ?></li>
<li><?php echo $author; ?></li>
<li><?php echo $company;?></li>
<li><?php echo $time;?></li>
<a href="<?php echo $link; ?>"> Read memo</a>
</ul><br>
<?php
}
?>