Jump to content

Need Help With "Simple" Function


scheda

Recommended Posts

I'm working on a web statistics program and have run into a little snafoo with a function. I think it's just because my brain has stopped working for the day - but if I could get another pair of eyes on it, that'd be great.

 

Here's the function.

 

//get hits per day in a comma seperated list
	public function get_daily_hits($days) {
		$daily_hits = array();
		$ip = $_SERVER['REMOTE_ADDR'];
		$time = time();
		$num = 0;

		for($i = $days; $i > 0; $i--) {
			$days_in_seconds = $i * 86400;
			$b = $days_in_seconds + 86400;
			foreach($this -> xml -> hit as $a) {
				if ($a -> ip != $ip && $a -> time > ($time - $days_in_seconds) && $a -> time < $b) {//FIX THIS LINE!
					$num++;
				}
			}
			array_push($daily_hits, $num);
			$num = 0;
		}
		$daily_hits = implode(',', $daily_hits);
		return $daily_hits;
	}

 

The if statement in the foreach is where I'm having trouble.

 

The function is supposed to create a comma delineated string of how many hits were registered on each day for the number of days entered as an argument.

 

So, as you can see in the if statement, what I'm trying to do is...

 

1. Weed out the admin's IP

2. Start the search at the end of the day being searched

3. End the search at the beginning of the day.

 

Included below is a sample XML file that I'm trying to pull from.

 

<?xml version="1.0"?>
<visits>
<hit>
<id>0</id>
<ip>97.101.183.201</ip>
<referrer>http://www.google.com</referrer>
<time>1254013189</time>
</hit>
<hit>
<id>1</id>
<ip>127.0.0.1</ip>
<referrer>http://www.google.com</referrer>
<time>1253926689</time>
</hit>
</visits>

 

 

It's being read just fine, but the function is just displaying a comma delimited string of 0's - which doesn't match up with the data in the XML file.

 

Thanks in advance for any help you can lend!

Link to comment
https://forums.phpfreaks.com/topic/175749-need-help-with-simple-function/
Share on other sites

Yeah! That was it.

 

Thanks a ton.

 

I guess this was one of those things where I just need to do the actual math myself and see where that takes me.

 

Thanks again.

 

It looks like your third line isn't going to be true.

 

$a -> time < $b 

 

$b is going to always be much smaller than the current time. do you mean

 

$a -> time < ($time +$b)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.