Mobile Device recognition, and Query change

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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Mobile Device recognition, and Query change

Post by simonmlewis »

Code: Select all

if ((preg_match("/(iPhone|BlackBerry|Android|motorola|nokia|panasonic|playbook|ipod|alcatel)/", $_SERVER["HTTP_USER_AGENT"])))
{ }
On a mobile device, I want to be able to change how many products are show on a category page.

So if it is seen as a mobile, I can change it from 60 per page, to maybe 30 per page.
The code above I think spots the device, but is that the best way to do it? To wrap the Query into that {} code...?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Mobile Device recognition, and Query change

Post by Celauran »

Would make much more sense to set a variable when the user first hits the site and then use that to set your limit value.

Code: Select all

$mobile = preg_match("/(iPhone|BlackBerry|Android|motorola|nokia|panasonic|playbook|ipod|alcatel)/", $_SERVER["HTTP_USER_AGENT"]);
...
$per_page = $mobile ? 30 : 60;
$query = "SELECT whatever etc etc LIMIT {$per_page}";
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Mobile Device recognition, and Query change

Post by simonmlewis »

Or as I have just discovered... this: http://mobiledetect.net/
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Mobile Device recognition, and Query change

Post by Christopher »

I would recommend doing the different category pages in the browser -- with styles preferably. It is a more modern practice to have different stylesheets for desktop and mobile display widths. Then instead of doing device detection, you check which stylesheet is being used by inspecting a style that is different.
(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Mobile Device recognition, and Query change

Post by simonmlewis »

Why is that better?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Mobile Device recognition, and Query change

Post by Celauran »

What if I'm on a desktop but using a small viewport?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Mobile Device recognition, and Query change

Post by Christopher »

simonmlewis wrote:Why is that better?
Because it relies on the actual size of the display -- not some soon-to-be-out-of-date list of devices. And checking a single style is much quicker and simpler than device detection.
(#10850)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Mobile Device recognition, and Query change

Post by Christopher »

Celauran wrote:What if I'm on a desktop but using a small viewport?
I think that is just my point -- you build the site to optimize the user experience for different viewport sizes. It is not about the device that is displaying the the viewport -- it's all about the viewport size. If someone has a small viewport on their desktop, then you are providing them with a better user experience at that viewport size. Forget about mobile vs desktop.
(#10850)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Mobile Device recognition, and Query change

Post by Celauran »

Christopher wrote:
Celauran wrote:What if I'm on a desktop but using a small viewport?
I think that is just my point
Indeed, and I am in complete agreement.
Post Reply