在标题php

时间:2016-02-22 01:30:24

标签: php

我会尝试解释我想用英语做的最好的事情。我在php中购买了一个虚假的实时聊天脚本。它显示了定义的消息等。

我试图在脚本中添加随机用户名的东西。我会告诉你我的代码和我的意思

基本上我想在我的标签

中添加我的$ default_users
session_start();
$numofmessages = 5;
$repeatmessages = 0;
$default_messages = array(
"my random line",
"my random line",
"my random line",
"my random line",
"my random line"
);

$default_users = array(
"user1",
"user2",
"user3",
"user4",
"user5"
);

function templateMessage($message, $client = 0){
    $html = '
    <div class="message_a">
        <h4 class="name">I WANT MY RANDOM USERNAMES HERE</h4>
        <h4 class="ago"><span data-livestamp="'.strtotime(Date("Y-m-d H:i:s")).'"></span></h4>
        <div class="message">
        '.$message.'
        </div>
    </div>';
    if($client == 0) $_SESSION['messages']['c_' . microtime()] = $html;
    else $_SESSION['messages']['u_' . microtime()] = $html;
    return $html;
}

function getAllMessages($max, $default_messages){
    $html = "";

    if(isset($_SESSION['messages'])):
        foreach($_SESSION['messages'] as $message_html):
            $html .= $message_html;
        endforeach;
    else:
        $html = templateMessage($default_messages[0]);
    endif;

    return $html;
}

function getMessage($max, $repeat, $default_messages){
    $recievedmessages = 0;
    $html = "";

    if(isset($_SESSION['messages'])):
        foreach($_SESSION['messages'] as $message_type => $message_html):
            if(strstr($message_type, "c_")) $recievedmessages++;
        endforeach;
    endif;

    if($recievedmessages > $max - 1):
        if($repeat == 1):
            $html = templateMessage($default_messages[$recievedmessages%$max]);
        endif;
    else:
        $html = templateMessage($default_messages[$recievedmessages]);
    endif;

    return $html;
}

function sendMessage(){
    return templateMessage($_POST['message'], 1);
}

if(isset($_POST['action']))
switch($_POST['action']):
    case "getmessages":
        echo getAllMessages($numofmessages, $default_messages);
    break;
    case "getmessage":
        echo getMessage($numofmessages, $repeatmessages, $default_messages);
    break;
    case "sendmessage":
        echo sendMessage();
    break;
endswitch;

0 个答案:

没有答案