Page 1 of 1
need help with array looping
Posted: Tue Nov 12, 2002 4:14 pm
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
Posted: Tue Nov 12, 2002 5:15 pm
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;
?>
Posted: Tue Nov 12, 2002 5:47 pm
by hob_goblin
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";
foreach($array as $var){
if(strstr($type, $var){
$unknown = TRUE;
}
}
if($unknown === TRUE) $type = "Unknowen";
echo $type;
?>

Posted: Tue Nov 12, 2002 8:48 pm
by qads
these did't work
but this did:
Code: Select all
<?php
$arrayї1] = "Win";
$arrayї2] = "Linux";
$arrayї3] = "Mac";
foreach($array as $var)
{
if(strstr($type,$var))
{
$unknown = TRUE;
}
}
//this line was missing a !..
if(!$unknown === TRUE) {$type = "Unknowen"; }
echo $type;
?>
thanks alot guys
