I need function to serving different data depend on browser type, mobile or desktop.. I need something like this

    <?php
    if browser_resolution_small {
    // one type of data
    } else {
    // another type of data for big resolution
    }
    ?>

so I need to pull different data but I do not want to create all different template for mobile version, for example, one theme for desktop and second theme for mobile... because everything else can be done with css and media css syntax.. only need to find how to create this part of code...

Recommended Answers

All 9 Replies

Member Avatar for LastMitch

I need function to serving different data depend on browser type, mobile or desktop.. I need something like this

What do you mean by that?

so I need to pull different data but I do not want to create all different template for mobile version, for example, one theme for desktop and second theme for mobile...

OK is your website will be in html files or php files?

Regardless of how the webpage looks. The database doesn't change how website looks it's the CSS.

There's no cap on how much data you have on the any website.

You have to figure how much data or space to read the data from the mobile website.

If it's too much then it will render (take a long time to load) slow.

If there was cap don't you think alot of people would have done this already?

Site is in PHP... I do not need to change how website looks.. I know what css does.. I need to serving different data depent on resolution..

So .. we know that iphone and mobile browser is max width 640 px.. I have 2 type of data.. one is for mobile and second is for desktop browsers... and this data is not same.. I do not want to create 2 types of sites and create redirection if somebody use mobile.. I can everything else finish with responsive css.. only this part of site is problem.. so I need something like if statements...

"If is mobile" serve one type of data "else" serve data for desktop browser...

You need to use client side (which is most often javascript) to detect screen size. Have a look at this link:

http://www.javascriptkit.com/howto/newtech3.shtml

How to utilize that with php? Maybe using Ajax, something like this (I use jquery load here):

<script type="text/javascript">
    // URL to open
    var myUrl = '';
    // set URL depending on screen size
    if (screen.width <= 800) {
        url = 'mySmallResolutionPage.php';
    } else {
        url = 'myBigResolutionPage.php';
    }
    // load appropriate script into contentsDiv div
    $('#contentsDiv').load(myUrl);
</script>

<!-- generated contents will get loaded here -->
<div id="contentsDiv"></div>

yes.. this is what I need.. to check first with script resolution and then load the php file with data.. only the problem is that I test this code you send me and its not work.. :) I create index.php with script and code you write.. and call for jquery 1.7 ... also I create in same folder mySmallResolutionPage.php and myBigResolutionPage.php .. and I get nothing .. what I doing wrong???

Can you post the latest code for index, mySmallResolutionPage and myBigResolutionPage pages please.

I think so error might be because of variable mismatch.Change url to myUrl

<script type="text/javascript">
    // URL to open
  var myUrl = '';
    // set URL depending on screen size
    if (screen.width <= 800) {
        myUrl = 'mySmallResolutionPage.php';//change url to myUrl
} else {
    myUrl = 'myBigResolutionPage.php';//change url to myUrl
}
  // load appropriate script into contentsDiv div
    $('#contentsDiv').load(myUrl);
</script>
<!-- generated contents will get loaded here -->
<div id="contentsDiv"></div>
commented: Thnx, my typo +9

Before posting the pages I asked you in my post above try the corrected code. The difference is ony in that the code is wrapped withing jquery ready() method (and of course IIM's correction has been applied).

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
         // URL to open
        var myUrl = '';
        // set URL depending on screen size (800 here is just an example)
        if (screen.width <= 800) {
            myUrl = 'mySmallResolutionPage.php';
        } else {
            myUrl = 'myBigResolutionPage.php';
        }
        // load appropriate script into contentsDiv div
        $('#contentsDiv').load(myUrl);
    });
</script>

<!-- generated contents will get loaded here -->
<div id="contentsDiv"></div>

Please note that the php scripts have to return only the body of html, the index.php shall contain the <html></html>, <head></head> and <body></body> tags and appropriate stuff.

Great.. Thank you very much this is work! I add in zip folder.. demo version.. thank you

mark the thread as solved if your problem is solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.