Welcome Guest, Not a member yet? Register   Sign In
Using "PHP" inside front end scripts
#1

Hey everyone,

I have a question that I was able to only partially answer on my own. I have read and understood that it is not a best practice to mix back end and front end code, but I am still doing it and simply can not find better way. To be exact - I am using cached variables sent from controller to view in my jquery and javascript scripts.

I have implemented a one-question survey in the sidebar, so a real life example would be my survey controller that sends a $question, $array[answer => count] and $date to view. And then I use those variables in googles JSAPI pie chart to make a nice visualization.

Is this considered standard practice and it's only the functional back end code that should not mix in the front end scenery or am I missing something here?

Thank you for your time and best regards,

Rok
Reply
#2

For my work it OK but make it to your standard.

I have my own standard to load only 1 view from controller and load 1 header view and 1 footer view in that view.
> controller
>>> first view
>>> >>> header
>>> >>> content of first view
>>> >>> footer

This make me to easy when I want to change something in header , footer (first view pass some value into header,footer view).

So every css, js are under control by logic of view.

That is my standard.

You can create your standard for you (or your team).
Reply
#3

I prefer to write my JavaScript to retrieve any data it needs from hidden fields or data-* attributes in the HTML, rather than making it aware of PHP variables passed from a controller. However, I'm probably as guilty as anyone when it comes to occasionally echoing a value into a script or even building JavaScript in PHP. In the end, the larger concern is about putting logic into PHP code in a view (rather than just retrieving the values of variables).

I think you'll find that creating JavaScript which is as independent of your PHP environment as possible has long term benefits, though. It certainly makes it easier if you need to get the same functionality in another project, or even another portion of the same site where you may want a similar visualization for some unrelated data.
Reply
#4

Thank you both for your quick reply.  

(07-02-2015, 08:16 AM)mwhitney Wrote: I prefer to write my JavaScript to retrieve any data it needs from hidden fields or data-* attributes in the HTML, rather than making it aware of PHP variables passed from a controller.

Are you aware of any online resources describing this methodology? I tried searching for something like "using php cached variables in script", but was unsuccessful.

This is how I managed to parse cached array ($survey->answers) to js array (var answers) in my view:


PHP Code:
<?php
$js_answers 
json_encode($survey->answers);
?>



Code:
<script>
   var JSON_array = <?= $js_answers ?>;
   var answers = [];

   for (var answer in JSON_array) {
       if (JSON_array.hasOwnProperty(answer)) {
           answers.push(JSON_array[answer].answer);
       }
   }
</script>
 

But based on what you wrote, I would image this is not the most effective/scalable approach.
Reply
#5

(07-02-2015, 08:47 AM)kamenjan Wrote: Are you aware of any online resources describing this methodology? I tried searching for something like "using php cached variables in script", but was unsuccessful.

This is how I managed to parse cached array ($survey->answers) to js array (var answers) in my view:


PHP Code:
<?php
$js_answers 
json_encode($survey->answers);
?>


Code:
<script>
   var JSON_array = <?= $js_answers ?>;
   var answers = [];

   for (var answer in JSON_array) {
       if (JSON_array.hasOwnProperty(answer)) {
           answers.push(JSON_array[answer].answer);
       }
   }
</script>
 
But based on what you wrote, I would image this is not the most effective/scalable approach.

The basic idea is to echo the $js_answers variable into the HTML in your view instead of your script, then use JavaScript to retrieve the value from the DOM and pass it into your script. In most cases, you can encapsulate most of your code into reusable .js files with objects/functions/methods, which you can read about almost anywhere (I prefer https://developer.mozilla.org, but in some areas they don't do much more than give you the specs).

Then you can either:
- determine a convention to follow within your Views' HTML to pass data to your script and create a common script you use to check for the presence of the data and call the appropriate methods in your JS; or
- add a small script to individual Views as needed to retrieve the data and call the appropriate methods.

For example, if you're using jQuery, you might do something like this:
Code:
<div id="surveyResultsDiv" data-answers="<?php echo $js_answers; ?>">
</div>
<script>
// Assuming the script which handles this data is available via myScript.graph()
myScript.graph($('#surveyResultsDiv').attr('data-answers'));

</script>
Reply




Theme © iAndrew 2016 - Forum software by © MyBB