Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert a php variable to session variable
    primarykey
    data
    text
    <p>I am having problem setting a variable in PHP for multiple use into <code>if statements</code>. </p> <p>Although there are many files in my layout but these are the 3 files relevant to this issue:</p> <p><strong>session.php</strong> (it is actually a JavaScript function which determines the browser-width of the visitor which is included in my javascript file and I call this via an ajax call. However since that is irrelevant to this issue hence to avoid confusion and too much code on this page I am avoiding that file.)</p> <pre><code>&lt;?php session_start(); $_SESSION['layoutType'] = $_REQUEST['layoutType']; ?&gt; </code></pre> <p><strong>core.php</strong></p> <pre><code>&lt;?php session_start(); if (empty($_SESSION['layoutType'])) { echo '&lt;script type="text/javascript"&gt; var session=false; var layoutType;&lt;/script&gt;'; } else { echo '&lt;script type="text/javascript"&gt; var session=true; var layoutType=' . $_SESSION['layoutType'] . ';&lt;/script&gt;'; } $layout = 'normal'; if (!empty($_SESSION['layoutType'])) { $layoutType = $_SESSION['layoutType']; if ( $layoutType &lt;= 219 ) { $layout = 'minimal'; } else if ($layoutType &gt;= 220 &amp;&amp; $layoutType &lt;= 1024 ) { $layout = 'small'; } else { $layout = 'normal'; } $_SESSION['layout'] = $layout; } </code></pre> <p><strong>index.php</strong></p> <pre><code>&lt;?php include ('core/core.php'); // Function to load all the JavaScript files getJS(); echo '&lt;div&gt;Your browser width is: ' . $_SESSION['layoutType'] . ' and Value of $layout variable currently is &lt;strong&gt;'.$layout.'&lt;/strong&gt;&lt;/div&gt;'; if ($layout = 'normal') { ?&gt; &lt;?php echo '&lt;strong&gt;' . $layout . '&lt;/strong&gt; is the value of $layout in session'; ?&gt; &lt;div id="sidebar"&gt; &lt;p&gt;Widgets go here&lt;/p&gt; &lt;/div&gt; &lt;?php } ?&gt; </code></pre> <p><strong>The output</strong> <img src="https://i.stack.imgur.com/DerbZ.jpg" alt="enter image description here"></p> <p>Even though, The layout-width falls in the small category <strong><em>(Your browser width is: 872 and Value of $layout variable currently is small)</em></strong>, it is still displaying the bit which it should display only is the layout-widthe category is normal <strong><em>(normal is the value of $layout in session)</em></strong>.</p> <p>Typically what is hapenning here is the value of <code>$layout</code> is being overwritten by the <code>if statement</code> in the <code>index.php</code> file.</p> <p><strong>What I am looking for?</strong></p> <p>Along with <code>layoutType</code> I also want the value of <code>$layout</code> to be set in the session based on the logic in <code>core.php</code>. So that I can use it multiple times across the website in various <code>if statements</code>. </p> <p>This is very important because these are varioys widgets / content in my layout which I want to load / not-load depending on the device of my visitor. Hence almost all the widhets wil be wrapped into an <code>if statement</code>.</p> <p>Kindly help.</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. This table or related slice is empty.
    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