1

我在这里完全被难住了,在此先感谢您提供的任何解决方案或指示,我非常感谢您提供的任何帮助。

我正在尝试制作一个备忘录系统。显示备忘录没问题,但我试图只显示未读备忘录。如您所见,我有两张表,一张有备忘录,另一张列出已读备忘录。当用户阅读了备忘录时,他/她在框和备忘录 id 上打勾,用户 id 被添加到表“memo_read”中。

所以基本上我试图检索已从“memo_read”表中读取的备忘录,然后在 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
}   }
?>
4

2 回答 2

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 '
                    )
于 2013-10-22T20:22:49.720 回答
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
}
?>
于 2013-10-22T20:25:15.510 回答