Reputation: 260
Can someone tell me how I can get this code to not show the app download button on a device that is not listed below?
I know it can be done but I'm not really sure.
Thanks.
<?php
$ua=$_SERVER['HTTP_USER_AGENT'];
switch(true) {
case stripos($ua,'android') :
$device = 'android'; break;
case stripos($ua,'ipad') :
$device = 'ipad'; break;
case stripos($ua,'iphone') :
$device = 'iphone'; break;
}
?>
<ul class="pageitem"><li class="button android"><input name="Submit" value="App Downloads" onclick="window.location='apps.php?id=<?php echo $device; ?>' " type="submit" /></li></ul>
Upvotes: 1
Views: 99
Reputation: 40639
Try this,
<?php
$device='';
$ua=$_SERVER['HTTP_USER_AGENT'];
switch(true) {
case stripos($ua,'android') :
$device = 'android'; break;
case stripos($ua,'ipad') :
$device = 'ipad'; break;
case stripos($ua,'iphone') :
$device = 'iphone'; break;
}
if($device != '')
{
echo '<ul class="pageitem"><li class="button android">
<input name="Submit" value="App Downloads" onclick="window.location=\'apps.php?id='.$device.'\'" type="submit" />
</li></ul>';
}
?>
Alternatively try the simple one
using preg_match
<?php
$ua=$_SERVER['HTTP_USER_AGENT'];
if (preg_match('/android|ipad|iphone/i', $ua)) {
echo '<ul class="pageitem"><li class="button android">
<input name="Submit" value="App Downloads" onclick="window.location=\'apps.php?id='.$device.'\'" type="submit" />
</li></ul>';
}
?>
Upvotes: 2
Reputation: 10717
Try with default
case. If not matches, default case will work.
<?php
$ua=$_SERVER['HTTP_USER_AGENT'];
switch(true) {
case stripos($ua,'android') :
$device = 'android'; break;
case stripos($ua,'ipad') :
$device = 'ipad'; break;
case stripos($ua,'iphone') :
$device = 'iphone'; break;
default:
$device = false;
}
?>
<?php if($device): ?>
<ul class="pageitem"><li class="button android"><input name="Submit" value="App Downloads" onclick="window.location='apps.php?id=<?php echo $device; ?>' " type="submit" /></li></ul>
<?php endif; ?>
Upvotes: 1
Reputation: 12173
How about...
<?php
$ua=$_SERVER['HTTP_USER_AGENT'];
switch(true) {
case stripos($ua,'android') :
$device = 'android'; break;
case stripos($ua,'ipad') :
$device = 'ipad'; break;
case stripos($ua,'iphone') :
$device = 'iphone'; break;
default:
$device = 'unknown';
}
if($device != 'unknown')
{
?>
<ul class="pageitem"><li class="button android"><input name="Submit" value="App Downloads" onclick="window.location='apps.php?id=<?php echo $device; ?>' " type="submit" /></li></ul>
<?php } ?>
Not tested, my PHP is rusty, so unsure if I did the default case right..
Upvotes: 2