Jump to content

IPB Modification PHP Script not working as it should


Padgoi

Recommended Posts

Ok, so the following script is a modification to an IPB board.  It's supposed to display how many users are currently viewing a topic from forum view.  The modification is working somewhat, however, when displaying how many users are viewing a topic, it says, "%s members viewing" instead of showing the actual number.  Any help would be appreciated.

 

			//-----------------------------------------
	// How many users are viewing this?
	//-----------------------------------------

	if( $this->ipsclass->vars['ma23-usersviewing_enable'] && 
		strstr( ',' . $this->ipsclass->vars['ma23-usersviewing_perms'] . ',', ',' . $this->ipsclass->member['mgroup'] . ',') &&
		strstr( ',' . $this->ipsclass->vars['ma23-usersviewing_forums'] . ',', ',' . $this->forum['id'] . ',' ) &&
		$topic['state'] != 'link'
		)
	{
		$cut_off = ($this->ipsclass->vars['au_cutoff'] != "") ? $this->ipsclass->vars['au_cutoff'] * 60 : 900;
		$time    = time() - $cut_off;

		$this->ipsclass->DB->simple_construct( array( 'select' => 's.member_id, s.member_name, s.member_group, s.id, s.login_type, s.location, s.running_time',
												'from' => 'sessions s',
												'where' => "s.location_1_type='topic' AND s.location_1_id={$topic['tid']}
															AND s.running_time > {$time} AND s.in_error=0"
													)      );

		$this->ipsclass->DB->simple_exec();

		if( $this->ipsclass->DB->get_num_rows() > 0 )
		{
			$ar_time = time();
			$cached = array();
			$active = array( 'guests' => 0, 'anon' => 0, 'members' => 0 );
			$rows   = array( $ar_time => array( 'login_type'   => substr($this->ipsclass->member['login_anonymous'],0, 1),
												'running_time' => $ar_time,
												'id'		   => 0,
												'location'	   => 'st',
												'member_id'    => $this->ipsclass->member['id'],
												'member_name'  => $this->ipsclass->member['members_display_name'],
												'member_group' => $this->ipsclass->member['mgroup'] 
											) );

			while ($r = $this->ipsclass->DB->fetch_row() )
			{
				$rows[ $r['running_time'].'.'.$r['id'] ] = $r;
			}

			krsort( $rows );

			//-----------------------------------------
			// PRINT...
			//-----------------------------------------

			foreach( $rows as $result )
			{

				$last_date = $this->ipsclass->get_time( $result['running_time'] );

				if ($result['member_id'] == 0)
				{
					$active['guests']++;
				}
				else
				{
					if (empty( $cached[ $result['member_id'] ] ) )
					{
						$cached[ $result['member_id'] ] = 1;

						if ($result['login_type'] == 1)
						{
							$active['anon']++;
						}
						else
						{
							$active['members']++;
						}
					}
				}
			}

			$total = $active['anon'] + $active['guests'] + $active['members'];

			$this->ipsclass->lang['usersviewing_prefix'] 	= sprintf( $this->ipsclass->lang['usersviewing_prefix'], $active['members'], $active['guests'], $active['anon'] );
			$this->ipsclass->lang['usersviewing'] 			= sprintf( $this->ipsclass->lang['usersviewing'], $this->ipsclass->lang['usersviewing_prefix'], $total );
		}
		else
		{
			$this->ipsclass->lang['usersviewing_prefix'] 	= '';
			$this->ipsclass->lang['usersviewing'] 			= '';
		}
	}
	else
	{
		$this->ipsclass->lang['usersviewing_prefix'] 	= '';
		$this->ipsclass->lang['usersviewing'] 			= '';
	}
	unset( $rows, $cached, $active );

Link to comment
Share on other sites

I can offer general help only.  That output indicates that you didn't use sprintf() where you should have, OR you didn't provide enough arguments to sprintf().

 

I would guess this line is the problem:

 

$this->ipsclass->lang['usersviewing'] 			= sprintf( $this->ipsclass->lang['usersviewing'], $this->ipsclass->lang['usersviewing_prefix'], $total );

Link to comment
Share on other sites

Here's some sprintf examples.  Basically, sprintf replaces things like %s and %d with variables.

 

$name = 'Bob';
$num_widgets = 5;
$message = sprintf('%s has %d widgets', $name, $num_widgets);

 

The replacements are done in the same order as they are written.  %s means "string", and %d means "integer".

 

Play around with that a while and get familiar with it, and you will understand the code you posted better!

Link to comment
Share on other sites

Here's some sprintf examples.  Basically, sprintf replaces things like %s and %d with variables.

 

$name = 'Bob';
$num_widgets = 5;
$message = sprintf('%s has %d widgets', $name, $num_widgets);

 

The replacements are done in the same order as they are written.  %s means "string", and %d means "integer".

 

Play around with that a while and get familiar with it, and you will understand the code you posted better!

Why not just do
$name = 'Bob';
$num_widgets = 5;
$message = echo"$name has $num_widgets widgets";

?

Link to comment
Share on other sites

Because the code the OP is dealing with uses sprintf :)  Not much point teaching him how to use php style substitution when he's modifying code that uses sprintf.

 

Padgoi, I think you've got 2 choices here:

 

1. Learn enough php to do your own modification

2. Post on the freelancing forum and get someone to do it for you

Link to comment
Share on other sites

No offense, but IPB already has this ability out of the box. If you got this code from ize be careful as a number of posted modifications simply duplicate existing functionality. Be equally careful of installing mods from ize to a production site, a lot of them simply don't work and the authors know about as much php as I do, i.e newbie level. Been waiting over a month on ize for an answer to what is a major issue with an add on module.

 

Which version of IPB are you using?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.