Page 1 of 1
Mobile Device recognition, and Query change
Posted: Fri Jun 26, 2015 3:37 am
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...?
Re: Mobile Device recognition, and Query change
Posted: Fri Jun 26, 2015 6:21 am
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}";
Re: Mobile Device recognition, and Query change
Posted: Fri Jun 26, 2015 6:50 am
by simonmlewis
Or as I have just discovered... this:
http://mobiledetect.net/
Re: Mobile Device recognition, and Query change
Posted: Fri Jun 26, 2015 11:42 am
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.
Re: Mobile Device recognition, and Query change
Posted: Fri Jun 26, 2015 11:49 am
by simonmlewis
Why is that better?
Re: Mobile Device recognition, and Query change
Posted: Fri Jun 26, 2015 12:23 pm
by Celauran
What if I'm on a desktop but using a small viewport?
Re: Mobile Device recognition, and Query change
Posted: Fri Jun 26, 2015 9:03 pm
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.
Re: Mobile Device recognition, and Query change
Posted: Fri Jun 26, 2015 9:11 pm
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.
Re: Mobile Device recognition, and Query change
Posted: Sat Jun 27, 2015 1:07 pm
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.