Topic: PHP Help needed

Problem:
How can I use echo inside a return command?
I would highly appreciate fast help. Thanks a lot!

FluxBB - v1.4.8

Re: PHP Help needed

Try it.
You mean you want to evaluate the echo execution?

You can assign whatever you want to echo into a variable through a function call like this

if($foo = function_call_to_evaluate($foo2)){
     echo $foo;
}

Re: PHP Help needed

Could you say that in easy English again, please? wink

FluxBB - v1.4.8

4 (edited by lie2815 2007-03-03 00:18)

Re: PHP Help needed

The following is my problem:

return 'Welcome <strong>'.$pun_user['username'].'</strong><br />
            <a href="login.php?action=out&id='.$pun_user['id'].'">'.$lang_common['Logout'].'</a><br /><br />
            '.implode($online,'').'<br />
            '.implode($chatonline,'').'<br />
            Users: <strong>'. $stats['total_users'].'</strong><br />
            Topics: <strong>'.$stats['total_topics'].'</strong><br />
            Posts: <strong>'.$stats['total_posts'].'</strong><br />';

I want the implode statements somehow expressed with echo (still with implode, though), because this data does not update immediately and with echo it does. I have been playing around with it a lot, but I could not figure out the right syntax.
*bloody beginner*

FluxBB - v1.4.8

Re: PHP Help needed

Erm, when you echo out the return of the function ( echo function($yay); ) it'll execute that code...

Re: PHP Help needed

Hey that is a sweet idea. Do I have to do that wherever the function is called?

FluxBB - v1.4.8

Re: PHP Help needed

Oops, that would kinda be a problem, since I somehow have to put that in header.php in the following substitution:

$tpl_main = str_replace('<pun_online>','<div class="inbox">'."\n\t\t\t". generate_whosonline()."\n\t\t".'</div>', $tpl_main);

generate_whosonline() is the function

FluxBB - v1.4.8

Re: PHP Help needed

Ok, I pretty much got that working now.
I created a new function that echos the generate_whosonline function. And now I call the output function from the header.php file. The problem is though, that the output happens at the top of the page, not where it is planned - in the box in the sidebar.

FluxBB - v1.4.8

Re: PHP Help needed

The way it was works just fine: the function gets called, and the result is stored wherever <pun_online> is in main.tpl. Then when you open the page it'll appear in the right position.

Re: PHP Help needed

Well, it does not work right. It does not update immediately. When I login, my name does not appear there immediately. I know though, that echo would do it immediately. That is why I tried it that way.

FluxBB - v1.4.8

Re: PHP Help needed

Erm, it should update it immediately =/

12 (edited by lie2815 2007-03-03 19:17)

Re: PHP Help needed

It does not though.
Here is the whole code:
I added that chatonline myself to show on the main page how many people are in my chatroom

function generate_whosonline()
{
    global $pun_config, $lang_common, $pun_user, $db;
    
    if ($pun_user['is_guest'])
    {
        require PUN_ROOT.'lang/'.$pun_user['language'].'/login.php';
        return 'Welcome Guest!<br />
            Please login.<br />
            <form id="login" method="post" action="login.php?action=in" onsubmit="return process_form(this)">
                <input type="hidden" name="form_sent" value="1" />
                <input type="hidden" name="redirect_url" value="index.php" />
                <label><strong>Username</strong><br /><input type="text" name="req_username" size="13" maxlength="25" tabindex="1" /><br /></label>
                <label><strong>Password</strong><br /><input type="password" name="req_password" size="13" maxlength="16" tabindex="2" /><br /></label>
                <p><a href="register.php" tabindex="4">'.$lang_login['Not registered'].'</a><br /></p>
                <p><a href="login.php?action=forget" tabindex="5">'.$lang_login['Forgotten pass'].'</a></p>
                <p><input type="submit" name="login" value="Login" tabindex="3" /></p>
            </form>';
    }
    else
    {
        require PUN_ROOT.'lang/'.$pun_user['language'].'/index.php';
        $result = $db->query('SELECT COUNT(id)-1 FROM '.$db->prefix.'users') or error('Unable to fetch total user count', __FILE__, __LINE__, $db->error());
        $stats['total_users'] = $db->result($result);
        
        $result = $db->query('SELECT id, username FROM '.$db->prefix.'users ORDER BY registered DESC LIMIT 1') or error('Unable to fetch newest registered user', __FILE__, __LINE__, $db->error());
        $stats['last_user'] = $db->fetch_assoc($result);
        
        $result = $db->query('SELECT SUM(num_topics), SUM(num_posts) FROM '.$db->prefix.'forums') or error('Unable to fetch topic/post count', __FILE__, __LINE__, $db->error());
        list($stats['total_topics'], $stats['total_posts']) = $db->fetch_row($result);
        
        $result = $db->query('SELECT COUNT(id) FROM '.$db->prefix.'polls') or error('Unable to fetch poll count', __FILE__, __LINE__, $db->error());
        $stats['total_polls'] = $db->result($result);
        
        if ($pun_config['o_users_online'] == '1')
        {
            // Fetch users online info and generate strings for output
            
            $num_guests = 0;
            $users = array();
            $online = array();
            $chatusers = array();
            $chatonline = array();
            $result = $db->query('SELECT user_id, ident FROM '.$db->prefix.'online WHERE idle=0 ORDER BY ident', true) or error('Unable to fetch online list', __FILE__, __LINE__, $db->error());
        
            while ($pun_user_online = $db->fetch_assoc($result))
            {
                if ($pun_user_online['user_id'] > 1)
                    $users[] = "\n\t\t\t\t".'<a href="profile.php?id='.$pun_user_online['user_id'].'">'.pun_htmlspecialchars($pun_user_online['ident']).'</a><br />';
                else
                    ++$num_guests;
            }
            
            $result = $db->query('SELECT user_id, ident FROM '.$db->prefix.'online WHERE in_chat_room ORDER BY ident', true) or error('Unable to fetch chatters online list', __FILE__, __LINE__, $db->error());
            
            while ($pun_user_online = $db->fetch_assoc($result))
            {
                if ($pun_user_online['user_id'] > 1)
                    $chatusers[] = "\n\t\t\t\t".'<a href="profile.php?id='.$pun_user_online['user_id'].'">'.pun_htmlspecialchars($pun_user_online['ident']).'</a><br />';
            }
                    
            $num_users = count($users);
            $online[] = "\t\t\t\t".$lang_index['Users online'].': <strong>'.$num_users.'</strong><br />'."\n\t\t\t\t".$lang_index['Guests online'].': <strong>'.$num_guests.'</strong>'."\n\t\t\t"."\n";
            $num_chatusers = count($chatusers);
            
            if ($num_users > 0 && $num_users < 10)
                $online[] = "\t\t\t"."\n\t\t\t\t".'<br /><br /><strong>'.$lang_index['Online'].': </strong>'."\t\t\t\t".implode($users)."\n";
            else
                $online[] = "\t\t\t".'<div class="clearer"></div>'."\n";
            
            if ($num_chatusers > 0)
                $chatonline[] = "\t\t\t"."\n\t\t\t\t".'<br /><br /><strong>'.$lang_index['ChatOnline'].': </strong>'."\t\t\t\t".implode($chatusers)."\n";    
                            
  }
      
        
         return 'Welcome <strong>'.$pun_user['username'].'</strong><br />
            <a href="login.php?action=out&id='.$pun_user['id'].'">'.$lang_common['Logout'].'</a><br /><br />
            '.implode($online,'').'<br />
            '.implode($chatonline,'').'<br />
            Users: <strong>'. $stats['total_users'].'</strong><br />
            Topics: <strong>'.$stats['total_topics'].'</strong><br />
            Posts: <strong>'.$stats['total_posts'].'</strong><br />';
          /*'Welcome <strong>'.$pun_user['username'].'</strong><br />
            <a href="login.php?action=out&id='.$pun_user['id'].'">'.$lang_common['Logout'].'</a><br /><br />
            'echo $onlineoutput<br />
            /*'echo $chatonlineoutput<br />
            Users: <strong>'. $stats['total_users'].'</strong><br />
            Topics: <strong>'.$stats['total_topics'].'</strong><br />
            Posts: <strong>'.$stats['total_posts'].'</strong><br />';*/
    }
}

Here is the other function:

//
// Generate the generate_whosonline() output content
//
function output_whosonline()
{
    echo generate_whosonline();
}
FluxBB - v1.4.8

Re: PHP Help needed

I figured it out - pretty much. It is because of the forum main page. It is the only page where the people that are logged in don't show up. Not even after I update it 50 times!
@elbekko: Could I send you the questionable file so that you could look through it. It is not even really long.
Thanks for all the help.

FluxBB - v1.4.8