Page 1 of 2

problems reassigning an array...

Posted: Wed Nov 19, 2003 11:04 pm
by infolock
I'm afraid I might figure this out after I post it ( it always happens that way... ), but i'll post it anyways cuz i'm stuck heh.

Basically, what i'm trying to do is determine the number of occurances in a database of specific browsers. Some may say "good god, ugly code", well, it works ( well until i put this unset function in it ), so for now til i catch up with the rest of you, it will have to do :)

anyways, on to the problem and the code!

What i'm trying to do is, set an array of possible strings for browser types. I query the table looking for this string, and then output the result.

After that, I go through the result, and find every instance of where the # of rows of the count is 0, and then unset that array ( so that I can put these values into a bar chart without having white spaces .. ).

so, i tried this code, but upon compiling and trying to echo out the result, i just get a blank page result, but if i take the unset out, it works fine ( except it still posts the values with a 0 in it ).

also, you might ask "why are you trying to dump the array??? Just rename the result to another array !" well, yeah i know. but the fewer times i have to switch variables, the less i'm gonna have to rewrite code...
plus, i know this is possible, it's just breaking my brain trying to figure it out. anyways, here is the code, maybe you can see where i'm screwing up at :

Code: Select all

<?php

// array of possible strings
$Mozilla = array('Netscape/7.0%','Mozilla/6%','Mozilla/5%','Mozilla/4%', 'NSPlayer/4%','NSPlayer/7%','NSPlayer/9%');

#####################
##  Arrays to be called later ##
#####################
$Moz=array();
$Moz_Found=array();

####################
## Start the query process ##
## ..looping for each string ##
####################

for($i=0; $i<=7; $i++)
{
	$sql = "select count(UserAgent) from log where (UserAgent LIKE '".$Mozilla[$i]."')";
	$result = mysql_query($sql) or die(MySQL_Error());
	$row = mysql_fetch_assoc($result);

########################### 
##  If we find something, process it     ##
###########################

	while($row = mysql_fetch_assoc($result))
	{
		foreach($row['count(UserAgent)'] as $found)
		{
			$Moz_Found[] = $found;
		}
		switch($Mozilla[$i])
		{
			case 'Netscape/7.0%' :
				if ($Moz_Found[0] =='0')
				{
					unset($Moz_Found[0]);
					unset($Moz[0]);
					break;
				}
					$Moz[0] = 'Mozilla 7.0';
				break;
			case 'Mozilla/6%' :
				if ($Moz_Found[1] =='0')
				{
					unset($Moz_Found[1]);
					unset($Moz[1]);
				}
				else
				{
					$Moz[1] = 'Mozilla 6.0';
				}
				break;
			case 'Mozilla/5%' :
				if ($Moz_Found[2] =='0')
				{
					unset($Moz_Found[2]);
					unset($Moz[2]);
				}
				else
				{
					$Moz[2] = 'Mozilla 5.0';
				}
				break;
			case 'Mozilla/4%' :
				if ($Moz_Found[3] =='0')
				{
					unset($Moz_Found[3]);
					unset($Moz[3]);
				}
				else
				{
					$Moz[3] = 'Mozilla 4.0';
				}
				break;
			case 'NSPlayer/4%' :
				if ($Moz_Found[4] =='0')
				{
					unset($Moz_Found[4]);
					unset($Moz[4]);
				}
				else
				{
					$Moz[4] = 'NSPlayer 4.0';
				}
				break;
			case 'NSPlayer/7%' :
				if ($Moz_Found[5] =='0')
				{
					unset($Moz_Found[5]);
					unset($Moz[5]);
				}
				else
				{
					$Moz[5] = 'NSPlayer 7.0';
				}
				break;
			case 'NSPlayer/9%' :
				if ($Moz_Found[6] =='0')
				{
					unset($Moz_Found[6]);
					unset($Moz[6]);
				}
				else
				{
					$Moz[6] = 'NSPlayer 9.0';
				}
				break;
		}
	}
}

########################
##  Echo out any results we find   ##
########################

foreach($Moz as $harhar)
{
	echo $harhar;
	echo ' was Found';
}

?>


I should also note, that in place of the actual Moz[0] array definition call, i also tried $i for each of them to see if maybe i could just loop through and define them on the fly. unfortunately, same results :(

Last edit, I also tried the Break statement in each *if* clause... i just left the first one in there for testing purposes...

Posted: Thu Nov 20, 2003 12:22 am
by infolock
just replaced all the

Code: Select all

<?
        case 'some value' : 
            if ($Moz_Found[x] =='0') 
            { 
               unset($Moz_Found[x]); 
               unset($Moz[x]); 
               break; 
            } 
               $Moz[x] = 'some value'; 
            break; 
?>


and replaced it with

Code: Select all

<?php
			case 'some value' :
					$Moz[x] = 'some value';
					break;
?>

with this at the end of the switch statement :

Code: Select all

<?php

for($i=0; $i<=6; $i++)
{
	if ($Moz_Found[$i] =='0')
	{
		unset($Moz_Found[$i]);
		unset($Moz[$i]);
	}
}

?>
but unfortunately, still no results... this is beginning to be a nightmare.

Posted: Thu Nov 20, 2003 12:30 am
by microthick
Couldn't you also just rewrite your SQL query to something more useful?

SELECT COUNT(UserAgent) AS BrowserCount, UserAgent
FROM log
WHERE UserAgent LIKE 'Netscape%' OR UserAgent LIKE 'Mozilla%' OR UserAgent LIKE 'NS%'
ORDER BY UserAgent
GROUP BY UserAgent

Posted: Thu Nov 20, 2003 12:44 am
by infolock
well, i'm obtaining the actual version # of the UserAgent being used, not just seeing if a Windows or Mozilla agent was found. so, unfortunately, this is the only way i could do it.

Posted: Thu Nov 20, 2003 12:54 am
by scorphus
Infolock,

Please take the momento to see how many fields are being returned by the query:

Code: Select all

for($i=0; $i<=7; $i++)
{
   $sql = "select count(UserAgent) from log where (UserAgent LIKE '".$Mozilla[$i]."')";
   $result = mysql_query($sql) or die(MySQL_Error());
   die(mysql_num_fields($result)); // what does it echoes? (I think it will be 1)
   $row = mysql_fetch_assoc($result);
Cheers,
Scorphus.

Posted: Thu Nov 20, 2003 12:55 am
by infolock
well.... i found out what the problem was....

god i'm an idiot...

I had put the while($row.....) statement in there to try something out, and forgot to take it back out...

my prior thought was wrong too. i just so happend to notice it just then lol...

Posted: Thu Nov 20, 2003 12:57 am
by infolock
scorphus

yeah i know. it loops through it one at a time though. so what it is doing is grabbing that info, processing it, and then repeating until it is finished.

unfortuantely, that's the only way i could figure out how to do it, cuz i'm too lazy to read up on GROUPING atm :P just trying to get it working, and then maybe make it better later. If ya got a pointer, i'm all ears.

Posted: Thu Nov 20, 2003 1:00 am
by scorphus
So the condition for the while($row = mysql_fetch_assoc($result)) will never gets true! if in the past line the only filed was already read from the db there are no more to be read. Hope you understand.

Posted: Thu Nov 20, 2003 1:01 am
by infolock
LOL man, I figured it out just then too hehehehe. it's 2am,a nd i guess i'm getting tired.


edit : i guess i'll try that code i had worked so damn hard on and see it works this time :twisted:

Posted: Thu Nov 20, 2003 1:07 am
by infolock
wh0000000t, the script works too :D

thank god

Posted: Thu Nov 20, 2003 1:11 am
by scorphus
infolock wrote:LOL man, I figured it out just then too hehehehe. it's 2am,a nd i guess i'm getting tired.
it happens... lol

You could try this:

Code: Select all

<pre><?php
$Mozilla = array('Netscape/7.0%','Mozilla/6%','Mozilla/5%','Mozilla/4%', 'NSPlayer/4%','NSPlayer/7%','NSPlayer/9%');
$mozOccur = array();
for ($i=0; $i<=7; $i++) {
   $sql = "select count(UserAgent) from log where (UserAgent LIKE '".$Mozilla[$i]."')";
   $result = mysql_query($sql) or die(MySQL_Error());
   $row = mysql_fetch_row($result);
   $mozOccur[$Mozilla[$i]] = $row[0];
}
print_r($mozOccur); // could unset() it first...
?></pre>
Cheers,
Scorphus.

Posted: Thu Nov 20, 2003 1:15 am
by scorphus
I'll try to find out an SQL query to do all this job for you...

Posted: Thu Nov 20, 2003 1:29 am
by infolock
schweet

Posted: Thu Nov 20, 2003 2:50 am
by microthick
infolock wrote:well, i'm obtaining the actual version # of the UserAgent being used, not just seeing if a Windows or Mozilla agent was found. so, unfortunately, this is the only way i could do it.
The output of my query would have returned the count for each version of each browser.

Posted: Thu Nov 20, 2003 11:01 am
by infolock
As I drove to school today, I had a sudden manifestation and figured out how to make this work with the values I needed.

What I can do is get a count of each total user agent, and create a temporary folder for browsers ONLY with group counts > 0.

Then, I can sort it any which way I want, and drop the table whenever I choose (wether at the end of the day, or the beginning of the next log parse ).

Microthick, I wasn't ignoring your solution at all, and it would have used it had it not been that I wasn't getting total counts that were greater than 0.

More then anything, I was just throwing code into this script to get what I needed, and then would clean up the queries afterwards. The query you gave, though, may come in useful for after I make the temp folder, so dont think I overlooked it :P

well, thanks for all the help peeps. If I can get through physicsII and calc III today, i'll be good to go :D