Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Firstly, PHP happens server side, so once the page loads the only way to use PHP again (without loading a page) is to make an ajax call, presumably to another php page that performs a certain function and returns a value.</p> <p>If you want to store a value as a session variable so that it can be used continuously, you can do the following:</p> <ol> <li>when they first land on your site have php check for the existence of a session and if it does not exist, call a Javascript function to get the layoutWidth and make an ajax call to a php page that will store it as a session variable and then reload the page and include the correct layout file. </li> </ol> <p>I would probably not use this method and actually look at ways to do this with JavaScript/JQuery instead. But this is how you have asked to do it.</p> <p>For your example, I have not used JQuery at all, but I would as the include is only about 19kb or so and it makes life SO much easier.</p> <p>Example:</p> <p>index.php</p> <pre><code>&lt;?php session_start(); ?&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt; &lt;?php if (empty($_SESSION['layoutWidth'])) { echo '&lt;script type="text/javascript"&gt; var session=false; var layoutWidth;&lt;/script&gt;'; } else { echo '&lt;script type="text/javascript"&gt; var session=true; var layoutWidth=' . $_SESSION['layoutWidth'] . ';&lt;/script&gt;'; } ?&gt; &lt;script type="text/javascript" src="js/viewport.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;?php if (!empty($_SESSION['layoutWidth'])) { $layoutWidth = $_SESSION['layoutWidth']; if ( $layoutWidth &gt;= 240 &amp;&amp; $layoutWidth &lt;= 900 ) { include('layout1.php'); } else if ($layoutWidth &gt; 900 &amp;&amp; $layoutWidth &lt;= 1200 ) { include('layout2.php'); } else { include('layout3.php'); } } ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>js/viewport.js</p> <pre><code>// JavaScript Document window.onload = function() { // if there is no session (session = false) if (!session) { // call function to get the screen size getScreenWidth(); // make ajax call to php page to set the session variable setSession(); } } function getScreenWidth() { layoutWidth = screen.width; } function setSession() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) { // Reload the page window.location.reload(); } } xmlhttp.open("POST","set_session.php?layoutWidth=" + layoutWidth,true); xmlhttp.send(); } </code></pre> <p>your php file to set the session the parameter is passed from your ajax call:</p> <p>set_session.php</p> <pre><code>&lt;?php session_start(); // you can check if it is already set if you like otherwise: $_SESSION['layoutWidth'] = $_REQUEST['layoutWidth']; ?&gt; </code></pre> <p>layout1.php</p> <pre><code>&lt;?php echo '&lt;div&gt;This is layout1.php&lt;/div&gt;'; echo '&lt;div&gt;Your screen width is: ' . $_SESSION['layoutWidth'] . '&lt;/div&gt;'; ?&gt; </code></pre> <p>layout2.php</p> <pre><code>&lt;?php echo '&lt;div&gt;This is layout2.php&lt;/div&gt;'; echo '&lt;div&gt;Your screen width is: ' . $_SESSION['layoutWidth'] . '&lt;/div&gt;'; ?&gt; </code></pre> <p>layout3.php</p> <pre><code>&lt;?php echo '&lt;div&gt;This is layout3.php&lt;/div&gt;'; echo '&lt;div&gt;Your screen width is: ' . $_SESSION['layoutWidth'] . '&lt;/div&gt;'; ?&gt; </code></pre> <p>This method means you dont have to pass parameters around in your URL. it will all be hidden.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload