Jump to content

Need Help With Mobile Detect - Specifically the Tablet code


ecarollo

Recommended Posts

Hi there,
     I've been trying to figure out some PHP that will detect mobile, tablet and desktop. I'm so close... the problem is that the 'tablet' part of the code is also reading as a 'mobile' device. Is there a way to make the 'tablet' function ignore the 'mobile' function? P.S. I'm using the standard Mobile_detect.php
     Here's my code:
[
<?php
include_once "wa/includes/Mobile_Detect.php";
$detect = new My_Mobile_Detect();
?>
<!-- TABLET START --><?php if($detect->isTablet()) { ?>

                <div style="float:right;position:relative;margin:35px 25px 40px 0px;font-size:40px">(206) 601-1904</div>
<!-- TABLET END --><?php }
                    if($detect->isMobile()) { ?>

                <a href="tel:(206) 601-1904" style="float:left; position:right; position:relative;top:10px; left:100px; margin-bottom:-25px !important; text-decoration:none;" title="Call (206) 601-1904"><img src="http://www.legend-cars.com/wp-content/uploads/2012/11/phone_icon.png" width="90" height="90" alt="Call (206) 601-1904"></a>
                    <a href="tel:(206) 601-1904" style="text-decoration:none;" title="Call (206) 601-1904"><span style="font-size:40px;float:left; margin:15px 25px 0px 115px;line-height:1em;">Call<br />Us!</span></a>
                <a href="sms:2066011904" style="float:left; position:right; position:relative;top:10px; margin-bottom:-25px !important; text-decoration:none;" title="Text (206) 601-1904"><img src="http://www.legend-cars.com/wp-content/uploads/2012/11/text_icon.png" width="90" height="90" alt="Phone Icon"></a>
                <a href="sms:2066011904" style="text-decoration:none;" title="Text (206) 601-1904"><span style="font-size:40px;float:left; margin:15px 0px 0px 20px;line-height:1em;">Text<br />Us!</span></a>
                    <?php }else{ ?>
<!-- DESKTOP START -->
                                    <div style="float:right;position:relative;margin:35px 25px 40px 0px;font-size:40px">(206) 601-1904</div>
<!-- DESKTOP END --><?php } ?>
]

Thanks in advance :)
ecarollo
 

Please wrap your code in code blocks. (<> button)

<?php
include_once "wa/includes/Mobile_Detect.php";
$detect = new My_Mobile_Detect();
?>
<!-- TABLET START --><?php if ($detect->isTablet()) { ?>

    <div style="float:right;position:relative;margin:35px 25px 40px 0px;font-size:40px">(206) 601-1904</div>
    <!-- TABLET END --><?php }
if ($detect->isMobile()) {
    ?>

    <a href="tel:(206) 601-1904" style="float:left; position:right; position:relative;top:10px; left:100px; margin-bottom:-25px !important; text-decoration:none;" title="Call (206) 601-1904"><img src="http://www.legend-cars.com/wp-content/uploads/2012/11/phone_icon.png" width="90" height="90" alt="Call (206) 601-1904"></a>
    <a href="tel:(206) 601-1904" style="text-decoration:none;" title="Call (206) 601-1904"><span style="font-size:40px;float:left; margin:15px 25px 0px 115px;line-height:1em;">Call<br />Us!</span></a>
    <a href="sms:2066011904" style="float:left; position:right; position:relative;top:10px; margin-bottom:-25px !important; text-decoration:none;" title="Text (206) 601-1904"><img src="http://www.legend-cars.com/wp-content/uploads/2012/11/text_icon.png" width="90" height="90" alt="Phone Icon"></a>
    <a href="sms:2066011904" style="text-decoration:none;" title="Text (206) 601-1904"><span style="font-size:40px;float:left; margin:15px 0px 0px 20px;line-height:1em;">Text<br />Us!</span></a>
<?php } else { ?>
    <!-- DESKTOP START -->
    <div style="float:right;position:relative;margin:35px 25px 40px 0px;font-size:40px">(206) 601-1904</div>
    <!-- DESKTOP END --><?php } ?>
<?php

include_once "wa/includes/Mobile_Detect.php";
$detect = new My_Mobile_Detect();
 
$tablet = '<div style="float:right;position:relative;margin:35px 25px 40px 0px;font-size:40px">(206) 601-1904</div>';
 
$mobile = '<a href="tel:(206) 601-1904" style="float:left; position:right; position:relative;top:10px; left:100px; margin-bottom:-25px !important; text-decoration:none;" title="Call (206) 601-1904"><img src="http://www.legend-cars.com/wp-content/uploads/2012/11/phone_icon.png" width="90" height="90" alt="Call (206) 601-1904"></a>';
$mobile .= '<a href="tel:(206) 601-1904" style="text-decoration:none;" title="Call (206) 601-1904"><span style="font-size:40px;float:left; margin:15px 25px 0px 115px;line-height:1em;">Call<br />Us!</span></a>';
$mobile .= '<a href="sms:2066011904" style="float:left; position:right; position:relative;top:10px; margin-bottom:-25px !important; text-decoration:none;" title="Text (206) 601-1904"><img src="http://www.legend-cars.com/wp-content/uploads/2012/11/text_icon.png" width="90" height="90" alt="Phone Icon"></a>';
$mobile .= '<a href="sms:2066011904" style="text-decoration:none;" title="Text (206) 601-1904"><span style="font-size:40px;float:left; margin:15px 0px 0px 20px;line-height:1em;">Text<br />Us!</span></a>';

$desktop = '<div style="float:right;position:relative;margin:35px 25px 40px 0px;font-size:40px">(206) 601-1904</div>';
 
if ($detect->isTablet()) {
  echo $tablet;
} elseif ($detect->isMobile()) {
  echo $mobile;
} else {
  echo $desktop;
}

 

You may want to start looking at your code like this. When you are jumping in and out of PHP tags and logic all the time, your code becomes difficult to read. Look at how I've structured your code here, and I think you'll find it's a little easier to read, which in turn will make it easier to debug when you are having issues.

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.