creata.physics Posted May 29, 2011 Share Posted May 29, 2011 Hello all, I have an online script that counts the online registered users, guests and bots. The script displays the users names that are online, and the bots names that are online. Since a user can only be logged in at one time on one machine, I have no issues with duplicates here. My issue is that bots appear depending how many are online, it'll say for example: Users Online Matt, Frank, Taylor, Google[bot], Google[bot], Google[bot]. I want to combine the bots, so it'll say: Matt, Frank, Taylor, Google[bot][3]. I've tried doing this with if statements alone, but it seems maybe I'll need a php function to do this with, how would I go about doing this? Thanks in advance, Matt. Quote Link to comment https://forums.phpfreaks.com/topic/237746-combining-duplicate-entires/ Share on other sites More sharing options...
Psycho Posted May 29, 2011 Share Posted May 29, 2011 How are you getting the names now? DB query or what? show the code you are currently using. Quote Link to comment https://forums.phpfreaks.com/topic/237746-combining-duplicate-entires/#findComment-1221762 Share on other sites More sharing options...
creata.physics Posted May 29, 2011 Author Share Posted May 29, 2011 I knew I should have done that, I'm going to post the full class so you have a complete understanding of the code. <?php class online { var $bots = array(); var $guests = array(); function run() { global $zext, $db, $std; //----------------------------------------------------------------- // Cover the basics //----------------------------------------------------------------- $this->html = $zext->theme->load_template('online'); $zext->nav[] = '<a href="'.$zext->base_url.'mod=online">Online List</a>'; $zext->input['tab'] = ($zext->input['tab'] != '') ? $zext->input['tab'] : 'guests'; $expire = time() - 900; //----------------------------------------------------------------- // Handle Tabs //----------------------------------------------------------------- $tabs = array('users','guests','bots'); foreach ($tabs AS $tab) { if ($zext->input['tab'] === $tab) { $tab_html .= $this->html->tabs($tab,1); } else { $tab_html .= $this->html->tabs($tab); } } //----------------------------------------------------------------- // Prepare users and guests array //----------------------------------------------------------------- $this->users = $db->get_table("SELECT s.sid as sid, s.user_id, s.referrer, s.last_action, s.ip_address, s.is_hidden, u.id as uid, u.group_id, u.username, u.flag FROM lnx_sessions AS s LEFT JOIN lnx_users AS u ON s.user_id = u.id WHERE s.user_id != 0 and s.last_action > $expire ORDER BY last_action DESC"); $this->guests = $db->get_table("SELECT ip_address, browser, referrer, last_action FROM lnx_sessions WHERE user_id = 0 AND last_action > $expire ORDER BY last_action DESC"); switch ($zext->input['tab']) { case 'guests': $return_html = $this->guests_online(); break; case 'users': $return_html = $this->users_online(); break; case 'bots': $return_html = $this->bots_online(); break; } //----------------------------------------------------------------- // Do final output //----------------------------------------------------------------- $this->output = $this->html->online_list($tab_html,$return_html); $zext->theme->html_insert($this->output); $zext->theme->do_output(); } function bots_online() { global $zext, $db, $std; //----------------------------------------------------------------- // Do search engine spiders //----------------------------------------------------------------- foreach ($this->guests AS $guest) { if ( $zext->settings['log_spider_visits'] == 1) { $spider_list = explode("\n",$zext->settings['spider_list']); if(is_array($spider_list)) { foreach($spider_list as $key=>$spiders) { $spider = explode('=',$spiders); $spider[1] = str_replace("\r",'',$spider[1]); if(in_string($spider[0],$guest['browser'],1) && !empty($spider[1]) && !in_array($spider[1],$this->bots)) { $guest['name'] = "{$spider[1]}"; } } $results[] = $guest; } } } //----------------------------------------------------------------- // Produce bot html. //----------------------------------------------------------------- if (is_array($results)) { foreach ($results AS $result) { if (isSet($result['name'])) { $result['lastvisit'] = $std->last_visit($result['last_action'], FALSE, FALSE); $result['referrer'] = $std->commons->check_referer($result['referrer']); $bot_list .= $this->html->bot_row($result); } } } else { $bot_list = $this->html->none(); } return $this->html->bot_list($bot_list); } function guests_online() { global $zext, $db, $std; //----------------------------------------------------------------- // Do search engine spiders //----------------------------------------------------------------- foreach ($this->guests AS $guest) { if ( $zext->settings['log_spider_visits'] == 1) { $spider_list = explode("\n",$zext->settings['spider_list']); if(is_array($spider_list)) { foreach($spider_list as $key=>$spiders) { $spider = explode('=',$spiders); $spider[1] = str_replace("\r",'',$spider[1]); if(in_string($spider[0],$guest['browser'],1) && !empty($spider[1]) && !in_array($spider[1],$this->bots)) { $guest['name'] = "{$spider[1]}"; } } $results[] = $guest; } } } //----------------------------------------------------------------- // Produce bot html. //----------------------------------------------------------------- if (is_array($results)) { foreach ($results AS $result) { if (!isSet($result['name'])) { $result['ip_address'] = $std->commons->hide_ip($result['ip_address']); $result['lastvisit'] = $std->last_visit($result['last_action'], FALSE, FALSE); $result['referrer'] = $std->commons->check_referer($result['referrer']); $guests_html .= $this->html->guest_row($result); } } } else { $guests_html = $this->html->none(); } return $this->html->guest_list($guests_html); } function users_online() { global $zext, $db, $std; //----------------------------------------------------------------- // Produce user html //----------------------------------------------------------------- if ($this->users != NULL) { foreach ($this->users AS $user) { // Lets parse all data first, knowing we still have to hide hidden users $user['flag'] = $std->commons->find_flag($user['flag'],$user['location']); $user['lastvisit'] = $std->last_visit($user['last_action'], FALSE, FALSE); $user['referrer'] = $std->commons->check_referer($user['referrer']); $user['group'] = $std->commons->find_group($user['group_id']); if ($user['is_hidden'] == 0) { $user['username'] = $std->commons->find_user($user['user_id']); $user_list .= $this->html->user_row($user); } else if ($user['is_hidden'] == 1 && $zext->user['is_admin'] == 1) { $user['username'] = $std->commons->find_user($user['user_id']).' <span class="inv">(Hidden)</span>'; $user_list .= $this->html->user_row($user); } } } return $this->html->user_list($user_list); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/237746-combining-duplicate-entires/#findComment-1221763 Share on other sites More sharing options...
Zurev Posted May 29, 2011 Share Posted May 29, 2011 Assuming the users are returned in an array, array_merge would do exactly what you're looking for. Quote Link to comment https://forums.phpfreaks.com/topic/237746-combining-duplicate-entires/#findComment-1221764 Share on other sites More sharing options...
creata.physics Posted May 29, 2011 Author Share Posted May 29, 2011 Yes but how would I go about doing the merge? In the foreach loop where I determine the spiders, would I make an if statement asking if the bot name == the bot name then i would merge the array? I understand reluctance for people providing others with code, because this website is intended to help people learn, not give them their own code. But like I said, I'm having trouble getting to understand exactly how to prepare the if statement that will perform the merge, your answer was quite vague, can I have a bit more detail please? Thanks, Matt Quote Link to comment https://forums.phpfreaks.com/topic/237746-combining-duplicate-entires/#findComment-1221765 Share on other sites More sharing options...
mikesta707 Posted May 29, 2011 Share Posted May 29, 2011 I would use array_unique. It takes an array, and returns an array without duplicates, which seems to be exactly what you want. THe good thing about this is that you don't really need a condition to use this, because if the array is already without duplicates, array_unique will just return the original array unchanged. here is an example of usage of array_unique $array = array(... some array with a bunch of repeat values); $unique_array = array_unique($array); //now $unique_array has all the values of $array, without duplicates hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/237746-combining-duplicate-entires/#findComment-1221870 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.