What's new

Create a Facebook Messenger Script

reacah

Eternal Poster
Established
Joined
Oct 9, 2015
Posts
1,095
Reaction
167
Points
362
Age
34
Bot for bot only


Create a facebook messenger style script using with using the following mysql rows: id, sender, receiver, timesent, status, text and using php language
 
Certainly! Here's an example of a Facebook Messenger-style script using PHP and MySQL, based on the given table structure:

PHP:
<?php
// Establish database connection
$servername = "your_servername";
$username = "your_username";
$password = "your_password";
$dbname = "your_dbname";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Function to retrieve and display messages for a conversation
function displayMessages($sender, $receiver)
{
    global $conn;

    $sql = "SELECT * FROM messages WHERE (sender = '$sender' AND receiver = '$receiver') OR (sender = '$receiver' AND receiver = '$sender') ORDER BY timesent ASC";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $messageSender = $row["sender"];
            $messageText = $row["text"];
            $messageTime = $row["timesent"];

            // Format the message display
            $formattedTime = date("H:i", strtotime($messageTime));
            $messageClass = ($messageSender == $sender) ? "outgoing" : "incoming";

            echo "<div class='message $messageClass'>";
            echo "<div class='message-sender'>$messageSender</div>";
            echo "<div class='message-text'>$messageText</div>";
            echo "<div class='message-time'>$formattedTime</div>";
            echo "</div>";
        }
    } else {
        echo "<div class='no-messages'>No messages yet.</div>";
    }
}

// Function to send a message
function sendMessage($sender, $receiver, $text) {
    global $conn;

    $sql = "INSERT INTO messages (sender, receiver, timesent, status, text) VALUES ('$sender', '$receiver', NOW(), 'unread', '$text')";
    if ($conn->query($sql) === TRUE) {
        echo "Message sent successfully!";
    } else {
        echo "Error sending message: " . $conn->error;
    }
}

// Example usage:
$currentUser = "john";
$otherUser = "jane";

// Display messages for the conversation between $currentUser and $otherUser
displayMessages($currentUser, $otherUser);

// Send a new message from $currentUser to $otherUser
sendMessage($currentUser, $otherUser, "Hello, Jane!");

$conn->close();
?>

In this script, the displayMessages function retrieves the messages from the messages table based on the sender and receiver. It then formats and displays the messages with sender name, text, and time.

The sendMessage function inserts a new message with the sender, receiver, current time, status, and message text into the messages table.

You can replace the database connection credentials ($servername, $username, $password, $dbname) with your own database details. Additionally, you can modify the HTML structure and CSS classes as per your requirements to match the Facebook Messenger style.

Note: This script assumes you have already created the messages table with the given column names in your MySQL database.

Feel free to modify the script as needed to suit your specific requirements.
 
Certainly! Here's an example of a Facebook Messenger-style script using PHP and MySQL, based on the given table structure:

PHP:
<?php
// Establish database connection
$servername = "your_servername";
$username = "your_username";
$password = "your_password";
$dbname = "your_dbname";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Function to retrieve and display messages for a conversation
function displayMessages($sender, $receiver)
{
    global $conn;

    $sql = "SELECT * FROM messages WHERE (sender = '$sender' AND receiver = '$receiver') OR (sender = '$receiver' AND receiver = '$sender') ORDER BY timesent ASC";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $messageSender = $row["sender"];
            $messageText = $row["text"];
            $messageTime = $row["timesent"];

            // Format the message display
            $formattedTime = date("H:i", strtotime($messageTime));
            $messageClass = ($messageSender == $sender) ? "outgoing" : "incoming";

            echo "<div class='message $messageClass'>";
            echo "<div class='message-sender'>$messageSender</div>";
            echo "<div class='message-text'>$messageText</div>";
            echo "<div class='message-time'>$formattedTime</div>";
            echo "</div>";
        }
    } else {
        echo "<div class='no-messages'>No messages yet.</div>";
    }
}

// Function to send a message
function sendMessage($sender, $receiver, $text) {
    global $conn;

    $sql = "INSERT INTO messages (sender, receiver, timesent, status, text) VALUES ('$sender', '$receiver', NOW(), 'unread', '$text')";
    if ($conn->query($sql) === TRUE) {
        echo "Message sent successfully!";
    } else {
        echo "Error sending message: " . $conn->error;
    }
}

// Example usage:
$currentUser = "john";
$otherUser = "jane";

// Display messages for the conversation between $currentUser and $otherUser
displayMessages($currentUser, $otherUser);

// Send a new message from $currentUser to $otherUser
sendMessage($currentUser, $otherUser, "Hello, Jane!");

$conn->close();
?>

In this script, the displayMessages function retrieves the messages from the messages table based on the sender and receiver. It then formats and displays the messages with sender name, text, and time.

The sendMessage function inserts a new message with the sender, receiver, current time, status, and message text into the messages table.

You can replace the database connection credentials ($servername, $username, $password, $dbname) with your own database details. Additionally, you can modify the HTML structure and CSS classes as per your requirements to match the Facebook Messenger style.

Note: This script assumes you have already created the messages table with the given column names in your MySQL database.

Feel free to modify the script as needed to suit your specific requirements.
Bot do seperate the code for sending message, reading message, and showing all messages
 
Bot do seperate the code for sending message, reading message, and showing all messages
Certainly! Here's an updated version of the script that separates the code for sending messages, reading messages, and displaying all messages:

PHP:
<?php
// Establish database connection
$servername = "your_servername";
$username = "your_username";
$password = "your_password";
$dbname = "your_dbname";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Function to retrieve messages for a conversation
function getMessages($sender, $receiver)
{
    global $conn;

    $sql = "SELECT * FROM messages WHERE (sender = '$sender' AND receiver = '$receiver') OR (sender = '$receiver' AND receiver = '$sender') ORDER BY timesent ASC";
    $result = $conn->query($sql);

    $messages = array();

    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $messageSender = $row["sender"];
            $messageText = $row["text"];
            $messageTime = $row["timesent"];

            $message = array(
                "sender" => $messageSender,
                "text" => $messageText,
                "time" => $messageTime
            );

            $messages[] = $message;
        }
    }

    return $messages;
}

// Function to send a message
function sendMessage($sender, $receiver, $text)
{
    global $conn;

    $sql = "INSERT INTO messages (sender, receiver, timesent, status, text) VALUES ('$sender', '$receiver', NOW(), 'unread', '$text')";
    if ($conn->query($sql) === TRUE) {
        return true;
    } else {
        return false;
    }
}

// Function to display messages
function displayMessages($messages)
{
    if (!empty($messages)) {
        foreach ($messages as $message) {
            $messageSender = $message["sender"];
            $messageText = $message["text"];
            $messageTime = $message["time"];

            // Format the message display
            $formattedTime = date("H:i", strtotime($messageTime));
            $messageClass = ($messageSender == $_SESSION["currentUser"]) ? "outgoing" : "incoming";

            echo "<div class='message $messageClass'>";
            echo "<div class='message-sender'>$messageSender</div>";
            echo "<div class='message-text'>$messageText</div>";
            echo "<div class='message-time'>$formattedTime</div>";
            echo "</div>";
        }
    } else {
        echo "<div class='no-messages'>No messages yet.</div>";
    }
}

// Example usage:
session_start();

if (isset($_SESSION["currentUser"])) {
    $currentUser = $_SESSION["currentUser"];
    $otherUser = "jane";

    // Display messages for the conversation between $currentUser and $otherUser
    $messages = getMessages($currentUser, $otherUser);
    displayMessages($messages);

    // Send a new message from $currentUser to $otherUser
    $text = "Hello, Jane!";
    if (sendMessage($currentUser, $otherUser, $text)) {
        echo "Message sent successfully!";
    } else {
        echo "Error sending message.";
    }
}
$conn->close();
?>

In this updated version, the code for retrieving messages is moved into the getMessages function, which returns an array of messages. The code for sending a message is moved into the sendMessage function, which returns either true or false depending on the success of the operation. The code for displaying messages is moved into the displayMessages function, which takes the array of messages as an argument.

Note that the example usage assumes that the current user is stored in the $_SESSION["currentUser"] variable. You may need to modify this part based on your specific implementation.

Feel free to adjust the code and HTML structure to match your requirements.
 
Certainly! Here's an updated version of the script that separates the code for sending messages, reading messages, and displaying all messages:

PHP:
<?php
// Establish database connection
$servername = "your_servername";
$username = "your_username";
$password = "your_password";
$dbname = "your_dbname";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Function to retrieve messages for a conversation
function getMessages($sender, $receiver)
{
    global $conn;

    $sql = "SELECT * FROM messages WHERE (sender = '$sender' AND receiver = '$receiver') OR (sender = '$receiver' AND receiver = '$sender') ORDER BY timesent ASC";
    $result = $conn->query($sql);

    $messages = array();

    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $messageSender = $row["sender"];
            $messageText = $row["text"];
            $messageTime = $row["timesent"];

            $message = array(
                "sender" => $messageSender,
                "text" => $messageText,
                "time" => $messageTime
            );

            $messages[] = $message;
        }
    }

    return $messages;
}

// Function to send a message
function sendMessage($sender, $receiver, $text)
{
    global $conn;

    $sql = "INSERT INTO messages (sender, receiver, timesent, status, text) VALUES ('$sender', '$receiver', NOW(), 'unread', '$text')";
    if ($conn->query($sql) === TRUE) {
        return true;
    } else {
        return false;
    }
}

// Function to display messages
function displayMessages($messages)
{
    if (!empty($messages)) {
        foreach ($messages as $message) {
            $messageSender = $message["sender"];
            $messageText = $message["text"];
            $messageTime = $message["time"];

            // Format the message display
            $formattedTime = date("H:i", strtotime($messageTime));
            $messageClass = ($messageSender == $_SESSION["currentUser"]) ? "outgoing" : "incoming";

            echo "<div class='message $messageClass'>";
            echo "<div class='message-sender'>$messageSender</div>";
            echo "<div class='message-text'>$messageText</div>";
            echo "<div class='message-time'>$formattedTime</div>";
            echo "</div>";
        }
    } else {
        echo "<div class='no-messages'>No messages yet.</div>";
    }
}

// Example usage:
session_start();

if (isset($_SESSION["currentUser"])) {
    $currentUser = $_SESSION["currentUser"];
    $otherUser = "jane";

    // Display messages for the conversation between $currentUser and $otherUser
    $messages = getMessages($currentUser, $otherUser);
    displayMessages($messages);

    // Send a new message from $currentUser to $otherUser
    $text = "Hello, Jane!";
    if (sendMessage($currentUser, $otherUser, $text)) {
        echo "Message sent successfully!";
    } else {
        echo "Error sending message.";
    }
}
$conn->close();
?>

In this updated version, the code for retrieving messages is moved into the getMessages function, which returns an array of messages. The code for sending a message is moved into the sendMessage function, which returns either true or false depending on the success of the operation. The code for displaying messages is moved into the displayMessages function, which takes the array of messages as an argument.

Note that the example usage assumes that the current user is stored in the $_SESSION["currentUser"] variable. You may need to modify this part based on your specific implementation.

Feel free to adjust the code and HTML structure to match your requirements.
Bot do seperate php file fot the code for sending message, reading message, and showing all messages include html
 
Bot do seperate php code for sending message, reading message, and showing all messages
Certainly! Here's an example of the separated PHP code for sending messages, reading messages, and showing all messages:

send_message.php
PHP:
<?php
// Establish database connection and include necessary files

// Function to send a message
function sendMessage($sender, $receiver, $text) {
    // Code for sending the message
    // ...
}

// Retrieve form data
$sender = $_POST['sender'];
$receiver = $_POST['receiver'];
$message = $_POST['message'];

// Send the message
sendMessage($sender, $receiver, $message);

// Redirect back to the conversation page
header("Location: conversation.php?sender=$sender&receiver=$receiver");
exit;
?>

read_message.php
PHP:
<?php
// Establish database connection and include necessary files

// Function to retrieve messages for a conversation
function getMessages($sender, $receiver) {
    // Code for retrieving messages
    // ...
}

// Retrieve query parameters
$sender = $_GET['sender'];
$receiver = $_GET['receiver'];

// Get messages for the conversation
$messages = getMessages($sender, $receiver);

// Display the messages
foreach ($messages as $message) {
    // Code for displaying each message
    // ...
}
?>

show_all_messages.php
PHP:
<?php
// Establish database connection and include necessary files

// Function to retrieve all messages
function getAllMessages() {
    // Code for retrieving all messages
    // ...
}

// Get all messages
$messages = getAllMessages();

// Display all messages
foreach ($messages as $message) {
    // Code for displaying each message
    // ...
}
?>

In each file, you will need to establish a database connection and include any necessary files for database access. You can then define the respective functions for sending messages, retrieving messages for a conversation, and retrieving all messages.

In the send_message.php file, you will handle the form submission to send a message. Retrieve the form data, call the sendMessage() function with the appropriate parameters, and then redirect back to the conversation page.

In the read_message.php file, you will retrieve the query parameters for the sender and receiver from the URL. Use these parameters to call the getMessages() function and retrieve the messages for the conversation. Iterate over the messages and display them.

In the show_all_messages.php file, you will call the getAllMessages() function to retrieve all messages. Iterate over the messages and display them.

Remember to customize the code as per your specific database structure and requirements.

Note: The code examples provided are simplified and may require additional error handling and validation based on your specific application needs.
 
Certainly! Here's an example of the separated PHP code for sending messages, reading messages, and showing all messages:

send_message.php
PHP:
<?php
// Establish database connection and include necessary files

// Function to send a message
function sendMessage($sender, $receiver, $text) {
    // Code for sending the message
    // ...
}

// Retrieve form data
$sender = $_POST['sender'];
$receiver = $_POST['receiver'];
$message = $_POST['message'];

// Send the message
sendMessage($sender, $receiver, $message);

// Redirect back to the conversation page
header("Location: conversation.php?sender=$sender&receiver=$receiver");
exit;
?>

read_message.php
PHP:
<?php
// Establish database connection and include necessary files

// Function to retrieve messages for a conversation
function getMessages($sender, $receiver) {
    // Code for retrieving messages
    // ...
}

// Retrieve query parameters
$sender = $_GET['sender'];
$receiver = $_GET['receiver'];

// Get messages for the conversation
$messages = getMessages($sender, $receiver);

// Display the messages
foreach ($messages as $message) {
    // Code for displaying each message
    // ...
}
?>

show_all_messages.php
PHP:
<?php
// Establish database connection and include necessary files

// Function to retrieve all messages
function getAllMessages() {
    // Code for retrieving all messages
    // ...
}

// Get all messages
$messages = getAllMessages();

// Display all messages
foreach ($messages as $message) {
    // Code for displaying each message
    // ...
}
?>

In each file, you will need to establish a database connection and include any necessary files for database access. You can then define the respective functions for sending messages, retrieving messages for a conversation, and retrieving all messages.

In the send_message.php file, you will handle the form submission to send a message. Retrieve the form data, call the sendMessage() function with the appropriate parameters, and then redirect back to the conversation page.

In the read_message.php file, you will retrieve the query parameters for the sender and receiver from the URL. Use these parameters to call the getMessages() function and retrieve the messages for the conversation. Iterate over the messages and display them.

In the show_all_messages.php file, you will call the getAllMessages() function to retrieve all messages. Iterate over the messages and display them.

Remember to customize the code as per your specific database structure and requirements.

Note: The code examples provided are simplified and may require additional error handling and validation based on your specific application needs.
please include all nessesary html codes
 
Certainly! Here's an example of the separated PHP code for sending messages, reading messages, and showing all messages:

send_message.php
PHP:
<?php
// Establish database connection and include necessary files

// Function to send a message
function sendMessage($sender, $receiver, $text) {
    // Code for sending the message
    // ...
}

// Retrieve form data
$sender = $_POST['sender'];
$receiver = $_POST['receiver'];
$message = $_POST['message'];

// Send the message
sendMessage($sender, $receiver, $message);

// Redirect back to the conversation page
header("Location: conversation.php?sender=$sender&receiver=$receiver");
exit;
?>

read_message.php
PHP:
<?php
// Establish database connection and include necessary files

// Function to retrieve messages for a conversation
function getMessages($sender, $receiver) {
    // Code for retrieving messages
    // ...
}

// Retrieve query parameters
$sender = $_GET['sender'];
$receiver = $_GET['receiver'];

// Get messages for the conversation
$messages = getMessages($sender, $receiver);

// Display the messages
foreach ($messages as $message) {
    // Code for displaying each message
    // ...
}
?>

show_all_messages.php
PHP:
<?php
// Establish database connection and include necessary files

// Function to retrieve all messages
function getAllMessages() {
    // Code for retrieving all messages
    // ...
}

// Get all messages
$messages = getAllMessages();

// Display all messages
foreach ($messages as $message) {
    // Code for displaying each message
    // ...
}
?>

In each file, you will need to establish a database connection and include any necessary files for database access. You can then define the respective functions for sending messages, retrieving messages for a conversation, and retrieving all messages.

In the send_message.php file, you will handle the form submission to send a message. Retrieve the form data, call the sendMessage() function with the appropriate parameters, and then redirect back to the conversation page.

In the read_message.php file, you will retrieve the query parameters for the sender and receiver from the URL. Use these parameters to call the getMessages() function and retrieve the messages for the conversation. Iterate over the messages and display them.

In the show_all_messages.php file, you will call the getAllMessages() function to retrieve all messages. Iterate over the messages and display them.

Remember to customize the code as per your specific database structure and requirements.

Note: The code examples provided are simplified and may require additional error handling and validation based on your specific application needs.
please do include other nessesary codes
 

Similar threads

Back
Top