0

The code may not make much sense . This is just me just getting to know to code :) I am making a chat where i am getting the chat messages from a mysql database and i want to output the data from the database in separate div`s using another function. I have som dificulties getting this to work!

Function to get the data from the database

function messages($user_id) {
    $user_id = (int)$user_id;

    $data = mysql_fetch_assoc(mysql_query("SELECT chat_content.chat_content_id, user.user_id, user.username, chat_content.chat_content, group_info.group_name FROM user, chat_content, group_info WHERE user.user_id = chat_content.user_id AND group_info.group_info_id = chat_content.group_info_id AND user.user_id = '$user_id'"));
    return $data;
}

Function to output the data from the other function to a page

function messages_structure($user_id) {
    messages($user_id);
    $chat_id = $data[0]['chat_content_id'];
    $output = array();
    foreach($chat_id) {
        $output[] = '<div id="chat_main_textbox"' . $chat_id . '</div>';
    }
    return '<div id="wrap">' .implode('', $output). '</div>';
}

Page output

$user_id = 56;
echo messages_structure($user_id);
  • Like `$data = messages($user_id);` so you actualy have access to what `messages()` returns – RiggsFolly Aug 09 '16 at 21:20
  • First avoid using `mysql_` functions. Use `mysqli_` functions. – Robert Aug 09 '16 at 21:20
  • Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) its really pretty easy – RiggsFolly Aug 09 '16 at 21:20
  • @RiggsFolly Yeah i know. Its a shame we still learn the old mysql in my school. I will for sure start to learn PDO or mysqli(dont really know the diffference) on my own. – Rein Bentdal Aug 09 '16 at 21:24
  • 1
    You should also mention this to your teachers, they are just being lazy, as they would also have to learn the new API's – RiggsFolly Aug 09 '16 at 21:25

2 Answers2

3

messages() returns an array, you need to assign that return value to a variable with messages_structure() where you call it:

$data = messages($user_id);

If you don't, the $data variable that is created in messages() is only available with that function's scope.

scrowler
  • 24,273
  • 9
  • 60
  • 92
0

I think your code should be

function messages($user_id) {
$user_id = (int)$user_id;

$data = mysql_fetch_assoc(mysql_query("SELECT chat_content.chat_content_id, user.user_id, user.username, chat_content.chat_content, group_info.group_name FROM user, chat_content, group_info WHERE user.user_id = chat_content.user_id AND group_info.group_info_id = chat_content.group_info_id AND user.user_id = '$user_id'"));
return $data;
}

Function to output the data from the other function to a page

function messages_structure($user_id) {
$data=messages($user_id);
$chat_id = $data[0]['chat_content_id'];
$output = array();
foreach($chat_id) {
    $output[] = '<div id="chat_main_textbox"' . $chat_id . '</div>';
}
return '<div id="wrap">' .implode('', $output). '</div>';

}

Page output

$user_id = 56;
echo messages_structure($user_id);
Abhijit Jagtap
  • 2,740
  • 2
  • 29
  • 43