need help with array looping

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

need help with array looping

Post by qads »

Code: Select all

<?php
$type = $_ENVї"HTTP_USER_AGENT"];
if(empty($type))
{
$type = $_SERVERї"HTTP_USER_AGENT"];
}

$arrayї3] = "Win";
$arrayї2] = "Linux";
$arrayї1] = "Mac";
$howmany = count($array);

for($cvf = 1; $cvf<=$howmany; $cvf++)
{
if(!strstr($type,$arrayї$cvf])) 
{ 
$type = "Unknowen";
}
}
echo $type;
?>
this works fine if the $type is "Win" but it puts Unknowen with "Mac" and "Linxs".

what i am trying to do is to see if the user agent is on one of $array(win, mac, linux), if it is then it inserts into a table if not it then it replaces the $type value with "Unknowen".

thanks for your help.

- Qads
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you're setting $type -the variable you're testing- to 'unknown' in every loop if the user-agent string does not match. Quite weird ;)
try

Code: Select all

<?php
$array = array('Mac', 'Linux', 'Win');
if(isset($_ENVї"HTTP_USER_AGENT"]))
	$type = $_ENVї"HTTP_USER_AGENT"];
if(empty($type) && isset($_SERVERї"HTTP_USER_AGENT"]))
{
	$type = isset($_SERVERї"HTTP_USER_AGENT"]) ? 
	$howmany = count($array);
	for($cvf = 1; $cvf!=$howmany; $cvf++)
	{
		if(strstr($type,$arrayї$cvf])) 
			break;
	}
	if($cvf==$howmany)
		$type = 'Unknown';
}
else
	$type = 'unknown';
echo $type;
?>
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

<?php 
$type = $_ENV&#1111;"HTTP_USER_AGENT"]; 
if(empty($type)) 
&#123; 
$type = $_SERVER&#1111;"HTTP_USER_AGENT"]; 
&#125; 

$array&#1111;3] = "Win"; 
$array&#1111;2] = "Linux"; 
$array&#1111;1] = "Mac"; 

foreach($array as $var)&#123;
 if(strstr($type, $var)&#123;
 $unknown = TRUE;
 &#125;
&#125;

if($unknown === TRUE) $type = "Unknowen";

echo $type; 
?>
;)
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

these did't work :(
but this did:

Code: Select all

&lt;?php
$array&#1111;1] = "Win"; 
$array&#1111;2] = "Linux"; 
$array&#1111;3] = "Mac"; 

foreach($array as $var)
{ 
if(strstr($type,$var))
{ 
$unknown = TRUE; 
} 
} 
//this line was missing a !.. 
if(!$unknown === TRUE) {$type = "Unknowen"; }
echo $type; 
?&gt;
thanks alot guys :D
Post Reply