GVKun编程网logo

php – 在线程中获取最新的消息(php获取信息)

11

针对php–在线程中获取最新的消息和php获取信息这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展asp.net–如何从复选框列表中获取最新的选定值?、C#中如何从线程中获取线程ID?、ja

针对php – 在线程中获取最新的消息php获取信息这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展asp.net – 如何从复选框列表中获取最新的选定值?、C#中如何从线程中获取线程ID?、java – 如何在servlet线程中获取新的有状态会话bean?、MySQL获取最新的对话消息等相关知识,希望可以帮助到你。

本文目录一览:

php – 在线程中获取最新的消息(php获取信息)

php – 在线程中获取最新的消息(php获取信息)

我有一个查询,获取消息系统的主要页面(包括未读消息计数等)所需的所有信息…但它当前正在检索原始的线程消息.我想增加以下查询,以获取每个线程中最新的消息.

这个查询非常接近,但是我的平庸的sql技能让我无法包装…

$messages = array();
$unread_messages_total = 0;

$messages_query = "
SELECT m.*,COUNT(r.id) AS num_replies,MAX(r.datetime) AS reply_datetime,(m.archived NOT LIKE '%,".$cms_user['id'].",%') AS message_archive,(m.viewed LIKE '%,%') AS message_viewed,SUM(r.viewed NOT LIKE '%,%') AS unread_replies,CASE
        WHEN MAX(r.datetime) >= m.datetime THEN MAX(r.datetime)
        ELSE m.datetime
        END AS last_datetime
FROM directus_messages AS m
LEFT JOIN directus_messages as r ON m.id = r.reply
WHERE m.active = '1'  
AND (m.to LIKE '%,%' OR m.to = 'all' OR m.from = '".$cms_user['id']."') 
GROUP BY m.id
HAVING m.reply = '0' 
ORDER BY last_datetime DESC";

foreach($dbh->query($messages_query) as $row_messages){
    $messages[] = $row_messages;
    $unread_messages_total += (strpos($row_messages['archived'],','.$cms_user['id'].',') === false && ( (strpos($row_messages['viewed'],') === false && $row_messages['unread_replies'] == NULL) || ($row_messages['unread_replies']>0 && $row_messages['unread_replies'] != NULL) ) )? 1 : 0;
}

提前感谢您提供的任何帮助!

编辑:(数据库)

CREATE TABLE `cms_messages` (
  `id` int(10) NOT NULL auto_increment,`active` tinyint(1) NOT NULL default '1',`subject` varchar(255) NOT NULL default '',`message` text NOT NULL,`datetime` datetime NOT NULL default '0000-00-00 00:00:00',`reply` int(10) NOT NULL default '0',`from` int(10) NOT NULL default '0',`to` varchar(255) NOT NULL default '',`viewed` varchar(255) NOT NULL default ',`archived` varchar(255) NOT NULL default ',PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

编辑2:(要求)

>返回特定user_id的所有父消息:$cms_user [‘id’]
>返回该父消息的回复数:num_replies
>返回该父消息的未读回复数:unread_replies
>返回父消息的日期或最近的回复:last_datetime
>返回消息是否在归档文件中:message_archive
>返回消息是否已被查看:message_viewed
>以DESC datetime顺序返回所有消息
>返回最新消息,从父或回复如果有一些(如gmail)

如果您只有两个级别的消息(即只有父消息和直接答复),则可以尝试此查询:
select
    root_message.id,root_message.active,root_message.subject,case
        when max_reply_id.max_id is null then 
            root_message.message
        else
            reply_message.message
    end as message,root_message.datetime,root_message.reply,root_message.from,root_message.to,root_message.viewed,root_message.archived
from
    -- basic data
    cms_messages as root_message
    -- ID of last reply for every root message
    left join (
        select 
            max(id) as max_id,reply as parent_id 
        from 
            cms_messages
        where
            reply <> 0 
        group by 
            reply
    ) as max_reply_id on max_reply_id.parent_id = root_message.id                                              
    left join cms_messages as reply_message on reply_message.id = max_reply_id.max_id
where
    root_message.reply = 0

它使用子查询max_reply_id作为数据源来选择最新答案的ID.如果存在(即,如果有答案),则使用reply_message.message.如果不存在(根消息未找到答案),则使用root_message.message.

你也应该考虑表的结构.例如,如果回应包含NULL(如果是父消息)或现有消息的ID,则会更有意义.目前,您将其设置为0(不存在的消息的ID),这是错误的.查看和归档的类型也很奇怪.

编辑:你也应该避免使用having子句.在可能的地方使用.

这是一个满足您的要求的新查询.如果它有任何问题(即如果它返回错误的数据),让我知道.

像第一个查询一样,它:

>使用子查询reply_summary来累积关于回复的数据(最后回复的ID,回复的数量和未读回复的数量);
>将此子查询加入到基表中;
>根据reply_summary.max_reply_id将cms_messages作为reply_message连接到子查询,以获取有关最后一个回复(消息,datetime)的数据.

我已经简化了你如何确定last_datetime的方式 – 它现在需要上次回复的时间(如果有任何回复)或原始帖子的时间(当没有发现回复时).

我没有过滤回复和来自字段.如果有必要,应该更新reply_summary子查询的where子句.

select
    parent_message.id,parent_message.subject,parent_message.message,parent_message.from,parent_message.to,coalesce(reply_summary.num_replies,0) as num_replies,last_reply_message.datetime as reply_datetime,(parent_message.archived NOT LIKE '%,{$cms_user['id']},(parent_message.viewed   LIKE     '%,reply_summary.unread_replies,coalesce(last_reply_message.message,parent_message.message) as last_message,coalesce(last_reply_message.datetime,parent_message.datetime) as last_datetime
from
    cms_messages as parent_message
    left join (
        select
            reply as parent_id,max(id) as last_reply_id,count(*) as num_replies,sum(viewed not like '%,%') as unread_replies
        from
            cms_messages
        where
            reply <> 0 and
            active = 1
        group by
            reply
    ) as reply_summary on reply_summary.parent_id = parent_message.id
    left join cms_messages as last_reply_message on last_reply_message.id = reply_summary.last_reply_id
where
    parent_message.reply = 0 and
    parent_message.active = 1 and
    (parent_message.to like '%,%' or parent_message.to = 'all' or parent_message.from = '{$cms_user['id']}')
order by
    last_datetime desc;

asp.net – 如何从复选框列表中获取最新的选定值?

asp.net – 如何从复选框列表中获取最新的选定值?

我目前面临一个问题.如何从asp.net复选框列表中获取最新的选定值?

从循环选中一个复选框列表的项目,我可以获得最高的选择索引及其值,但不期望用户从下到上依次选择复选框.那么,怎么处理呢?

是否有任何事件捕获系统,将帮助我识别产生事件的确切列表项目?

解决方法

如果我理解正确,这是我使用的代码:
protected void CheckBoxList1_SelectedindexChanged(object sender,EventArgs e)
{
    int lastSelectedindex = 0;
    string lastSelectedValue = string.Empty;

    foreach (ListItem listitem in CheckBoxList1.Items)
    {
        if (listitem.Selected)
        {
            int thisIndex = CheckBoxList1.Items.IndexOf(listitem);

            if (lastSelectedindex < thisIndex)
            {
                lastSelectedindex = thisIndex;
                lastSelectedValue = listitem.Value;
            }
        }
    }
}

是否有任何事件捕获系统,将帮助我识别产生事件的确切列表项目?

您使用CheckBoxList的事件CheckBoxList1_SelectedindexChanged.当单击列表的复选框时,会调用此事件,然后可以检查所需的任何条件.

编辑:

以下代码允许您获取用户选择的最后一个复选框索引.使用这些数据,您可以得到用户最后选择的值.

protected void CheckBoxList1_SelectedindexChanged(object sender,EventArgs e)
{
    string value = string.Empty;

    string result = Request.Form["__EVENTTARGET"];

    string[] checkedBox = result.Split('$'); ;

    int index = int.Parse(checkedBox[checkedBox.Length - 1]);

    if (CheckBoxList1.Items[index].Selected)
    {
        value = CheckBoxList1.Items[index].Value;
    }
    else
    {

    }
}

C#中如何从线程中获取线程ID?

C#中如何从线程中获取线程ID?

c#中如何从线程中获取线程id?

线程被定义为程序的执行路径。每个线程定义一个唯一的 流程控制。如果您的应用程序涉及复杂和耗时的操作 操作,那么设置不同的执行路径或线程通常很有帮助, 每个线程执行特定的任务。

线程是轻量级的进程。一个常见的线程使用示例是 现代操作系统实现并发编程。使用 线程可以节省CPU周期的浪费,并提高应用程序的效率。

在C#中,System.Threading.Thread类用于处理线程。它允许在多线程应用程序中创建和访问单个线程。在一个进程中,第一个要执行的线程被称为主线程。

当一个C#程序开始执行时,主线程会自动创建 使用Thread类创建的线程称为主线程的子线程。 您可以使用 Thread 类的 CurrentThread 属性访问线程。

示例

class Program{
   public static void Main(){
      Thread thr;
      thr = Thread.CurrentThread;
      thr.Name = "Main thread";
      Console.WriteLine("Name of current running " + "thread: {0}", Thread.CurrentThread.Name);
      Console.WriteLine("Id of current running " + "thread: {0}", Thread.CurrentThread.ManagedThreadId);
      Console.ReadLine();
   }
}
登录后复制

输出

Name of current running thread: Main thread
Id of current running thread: 1
登录后复制

以上就是C#中如何从线程中获取线程ID?的详细内容,更多请关注php中文网其它相关文章!

java – 如何在servlet线程中获取新的有状态会话bean?

java – 如何在servlet线程中获取新的有状态会话bean?

我正在试验EJB3

我想将一个有状态会话bean注入一个servlet,这样每个访问servlet的用户都会获得一个新的bean.

显然,我不能让bean成为servlet的实例变量,因为它将被共享.并且不允许显着地注入局部变量.

我可以使用new运算符来创建bean,但这似乎不是正确的方法.

有没有正确的方法来做到这一点?看起来我想要做的事情是相当简单的,毕竟,我们希望每个新客户都能找到一个空的购物车.

解决方法

您无法使用new来获取新的SFSB.

您通常使用InitialContext查找新的.

MyBean bean = (MyBean) new InitialContext().lookup( name );

然后,您将获得对可以在请求中重用的特定SFSB的引用.

从this answer开始:

You should not typically inject SFSB,
unless it is into another SFSB or into
a Java EE client. You should use @EJB
on the referencing class (e.g. your
servlet) to declare the ejb-ref and
then do a JNDI lookup in the code to
obtain the instance. This instance
Could then be placed directly in your
Http session.

有关SFSB的更多信息,您可能对我的其他答案感兴趣:

> Stateful EJBs in web application?
> Java: Tracking a user login session – Session EJBs vs HTTPSession
> Correct usage of Stateful Beans with Servlets

希望能帮助到你.

MySQL获取最新的对话消息

MySQL获取最新的对话消息

我正在开发一个应用程序,用户可以在其中与其他用户聊天(1对1,而不是群聊)。我有一个MySQL表,用于存储来自每个用户的所有消息,例如:

from_id   to_id          message             timeabc123    def456         Hello               789def456    abc123         What''s up?          1234`def456    abc123         How was last night? 2345abc123    p0tat0         I missed the bus    3456def456    p0tat0         I hate you :(       4567`def456    another_user   I hate Potato!      5678`

如何从AND获取最新消息,abc123从最新到最旧排序,例如:

from_id to_id message time

abc123 p0tat0 I missed the bus 3456

def456 abc123 How was last night? 2345

time 如果重要的话,在消息表中将始终以升序排列。

任何帮助将不胜感激。谢谢^。^

SELECT *, ''from'' as directionFROM messages WHEREfrom_username=''admin'' AND ''time'' = ( SELECT MAX(''time'') FROM messages WHEREfrom_username=''admin'' OR to_username=''admin'')

UNION ALL

SELECT *, ''to'' as directionFROM messages WHERE to_username=''admin''AND ''time'' = ( SELECT MAX(''time'') FROM messages WHERE to_username=''admin''ORto_username=''admin'' )

答案1

小编典典

试试这个

SELECT * FROMmessageTableWHERE from_id=''abc123''AND `time` = ( SELECT MAX(`time`) FROM messageTable WHERE from_id=''abc123'' )UNION ALLSELECT * FROMmessageTableWHERE to_id=''abc123''AND `time` = ( SELECT MAX(`time`) FROM messageTable WHERE to_id=''abc123'' )

今天关于php – 在线程中获取最新的消息php获取信息的讲解已经结束,谢谢您的阅读,如果想了解更多关于asp.net – 如何从复选框列表中获取最新的选定值?、C#中如何从线程中获取线程ID?、java – 如何在servlet线程中获取新的有状态会话bean?、MySQL获取最新的对话消息的相关知识,请在本站搜索。

本文标签: