Insert html into PHP return - php

My PHP is failing me. I'm trying to put a div around an element in a function. Here is the original code:
if ($withCurrency) {
$currency = (isset($GLOBALS['aitThemeOptions']->directory->currency)) ? $GLOBALS['aitThemeOptions']->directory->currency : '';
return $currency . ' ' . $lowestPrice;
}
I want to put a div around the $currency.
I've tried changing this line:
return $currency . ' ' . $lowestPrice;
to this:
return "<div>" . $currency . "</div> " . $lowestPrice;
However that actually outputs the div tags as text on the page. Can someone tell me how to add the divs so they render as html?
UPDATE:
Here is the entire function:
function getTourPrice($tourId, $withCurrency = true) {
$lowestPrice = false;
$offers = getTourOffers($tourId);
// first get price from special offers
foreach ($offers as $offer) {
if ((isset($offer->options['special'])) && ((!$lowestPrice) || floatval($offer->options['price']) < $lowestPrice)) {
$lowestPrice = floatval($offer->options['price']);
}
}
// if special price isn't available then get price from normal offers
if (!$lowestPrice) {
foreach ($offers as $offer) {
if ((!$lowestPrice) || (floatval($offer->options['price']) < $lowestPrice)) {
$lowestPrice = floatval($offer->options['price']);
}
}
}
if (!$lowestPrice) return false;
if ($withCurrency) {
$currency = (isset($GLOBALS['aitThemeOptions']->directory->currency)) ? $GLOBALS['aitThemeOptions']->directory->currency : '';
return $currency . ' ' . $lowestPrice;
} else {
return $lowestPrice;
}
}
The result of that function is called with the following code:
{var $lp = getTourPrice($post->id)}
{if $lp}<div class="item-price"><span><span><span class="from">From</span>{$lp}</span></span></div>{/if}
If I use echo instead of return like this:
echo "<div>" . $currency . "</div> " . $lowestPrice;
I lose all the html above. I.E. What was the following:
<div class="item-price"><span><span><span class="from">From</span>{$lp}</span></span></div>
Becomes this:
<div>R</div> 56200
(where R is $currency and 56200 is $lowestPrice)
Hope that's not painfully convoluted.

This will have something to do with what you are doing with the return
I imagine if you did this:
if ($withCurrency) {
$currency =
(isset($GLOBALS['aitThemeOptions']->directory->currency)) ?
$GLOBALS['aitThemeOptions']->directory->currency :
'';
echo "<div>" . $currency . "</div> ";
}
It would work.
Is your code the end of a function?

Try using the :
{$lp|escape:false}

I would simply place the div in the $currency variable, so it will be added upon return. I would change this line of code:
$currency = (isset($GLOBALS['aitThemeOptions']->directory->currency)) ? $GLOBALS['aitThemeOptions']->directory->currency : '';
to this:
$currency = (isset($GLOBALS['aitThemeOptions']->directory->currency)) ? '<div>'.$GLOBALS['aitThemeOptions']->directory->currency.'</div>' : '';
or another method:
$currency = '<div>'.$currency.'</div>';
This way you are defining the html in the variable and not in the return. This is not tested, but it is the way I tend to code. :) Hope it helps!
Also one more thing, it is a bit of controversy among programmers, but I also recommend using single quotes when defining html in php.

Related

PHP: Create arrays from a function call inside a foreach loop and merge them

The purpose of this code is to identify all the image files from the folder in which the code is being invoked in order to create an image gallery. The images are listed in alphanumeric order but I require a specific order so reordering with a standard PHP array sorting function doesn't meet my needs.
I am using an if statement to place image collections into different arrays then merging the arrays into my required order.
When I run the code as part of my foreach loop it works fine. I want to put the if conditional into a function to reuse the code but I just get a blank page when I copy and paste the code into the function:
// echo statements are just for testing.
foreach(glob(IMAGEPATH."*.{jpg,png,gif,JPG,PNG,GIF}", GLOB_BRACE) as $var03){
$img_src03 = basename($var03);
$img_label03 = pathinfo($var03, PATHINFO_FILENAME);
// Assign specific values to the arrays as you cycle through $img_src03 values:
if (substr($img_src03, 0, 5) == 'ext_f'){
if (!isset($array33)) {
$array33 = array();
}
$array33[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 5) == 'ext_r'){
if (!isset($array33)) {
$array33 = array();
}
$array33[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 6) == 'ext_po'){
if (!isset($array34)) {
$array34 = array();
}
$array34[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 3) == 'bed'){
if (!isset($array35)) {
$array35 = array();
}
$array35[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 3) == 'bth'){
if (!isset($array36)) {
$array36 = array();
}
$array36[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
}
}
$arrayFinal = array_merge($array33, $array34, $array35, $array36);
echo 'This is $arrayFinal:<br><pre>'; print_r($arrayFinal); echo '</pre><br>';
When the exact same if conditional is placed inside function findImage03($img_src03, $img_label03), which is located outside the foreach loop, then called from inside the foreach loop the code fails to work.
foreach(glob(IMAGEPATH."*.{jpg,png,gif,JPG,PNG,GIF}", GLOB_BRACE) as $var03){
$img_src03 = basename($var03);
$img_label03 = pathinfo($var03, PATHINFO_FILENAME);
// Trying to use a function call to run the if conditional. Function is outside the foreach loop. Nothing returned.
findImage03($img_src03, $img_label03);
}
function findImage03($img_src03, $img_label03){
// Assign specific values to the arrays as you cycle through $img_src03 values:
if (substr($img_src03, 0, 5) == 'ext_f'){
if (!isset($array33)) {
$array33 = array();
}
$array33[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 5) == 'ext_r'){
if (!isset($array33)) {
$array33 = array();
}
$array33[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 6) == 'ext_po'){
if (!isset($array34)) {
$array34 = array();
}
$array34[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 3) == 'bed'){
if (!isset($array35)) {
$array35 = array();
}
$array35[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 3) == 'bth'){
if (!isset($array36)) {
$array36 = array();
}
$array36[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
}
}
$arrayFinal = array_merge($array33, $array34, $array35, $array36);
echo 'This is $arrayFinal:<br><pre>'; print_r($arrayFinal); echo '</pre><br>';
In researching this I think I have found a couple of issues so I'm not sure if this is down to not being a programmer or not.
I found a solution running foreach() in a function() rather than a function() within a foreach(). The new array is then created using the result of the function call.
array_merge() works only if all the arrays being merged hold values otherwise it fails. Not sure if that could be resolved with !isset.
So this is the code running the foreach() in a function() which can be located in an included file for reuse efficiency plus a single source to maintain/update:
// Define the function to get all the images in the current folder:
function getImages(){
foreach(glob(IMAGEPATH."*.{jpg,png,gif,JPG,PNG,GIF}", GLOB_BRACE) as $var){
$img_src = basename($var);
// Assign specific values to the array as you cycle through the collection. Order here will not affect final array order.
if (substr($img_src, 0, 5) == 'bed_1'){
if (!isset($arrayBed_1)) {
$arrayBed_1 = array();
}
$arrayBed_1[] = $img_src;
} elseif (substr($img_src, 0, 5) == 'bed_2'){
if (!isset($arrayBed_2)) {
$arrayBed_2 = array();
}
$arrayBed_2[] = $img_src;
} elseif (substr($img_src, 0, 5) == 'bed_3'){
if (!isset($arrayBed_3)) {
$arrayBed_3 = array();
}
$arrayBed_3[] = $img_src;
// continue for each type required.
} }
} //End of foreach().
// Create the pre final array for other arrays to push to which defines the sort order:
$arrayPreFinal = array();
if (isset($arrayExt_f)){ // ext_f = exterior front
foreach ($arrayExt_f as $val){
array_push($arrayPreFinal, $val);
}
}
if (isset($arrayExt_r)){ // ext_r = exterior rear
foreach ($arrayExt_r as $val){
array_push($arrayPreFinal, $val);
}
}
if (isset($arrayBed_1)){ // bed_1 = bedroom 1
foreach ($arrayBed_1 as $val){
array_push($arrayPreFinal, $val);
}
}
if (isset($arrayBed_2)){ // bed_2 = bedroom 2
foreach ($arrayBed_2 as $val){
array_push($arrayPreFinal, $val);
}
}
// continue for as many variances as require.
return $arrayPreFinal;
} // End of function()
The code run from the folder where the images are located:
// Set $iwd Image Working Directory:
// Required before gallery_ctrl_body.php inlcude as $iwd is needed before include.
// class IWD changes outside the class.
class IWD {
// Properties
public $iwd;
}
$var = new IWD();
$var->name = getcwd();
$iwd = $var->name;
// Script include needs to be run before function pid_text().
include_once ('../gallery_ctrl_body.php');
# Function to identify the property sub-division from directory path characters using preg_match()
# and define a Property ID Text:
function pid_text($sub_div) { //Need parameter/s in function():
$sub_div_abb = sub_div_abb($sub_div); //20200220 code
$psc = 'ORL'; // Start of Property System Code.
$str = getcwd();
if(preg_match("/{$sub_div}/i", $str)) {
$psc = $psc . $sub_div_abb . strtoupper(basename(__DIR__)); //sub_div_abb may already defined in UPPERCASE.
$pid_textname = constant("PROPERTY_TEXT") . $psc; //strtoupper($psc);
}
return $pid_textname; //Return value AFTER IF statement. REQUIRED!
}
$pid_text = pid_text($sub_div = 'test_200828');
// Define the imagepath and variables:
define('IMAGEPATH', dirname(__FILE__).'/'); // '/' is required.
$img_height = 'height: 400px'; // default image height for gallery.
$img_width = 'width: 600px'; // default image width for gallery.
$img_counter = 0;
$img_style = 'style="' . $img_width . '"'; // only needs to be set once here.
$img_total = 0;
// Create the final array with function call to get images:
$arrayFinal = getImages();
// Calculate total number of images before displaying <div>
// If calculated inside a foreach() $img_counter always equals $img_total.
$img_total = count($arrayFinal);
echo '<div class="property_text-container">';
echo 'Gallery for ' . $pid_text;
echo '</div>';
//<!-- Slideshow container -->
echo '<div class="slideshow-container">';
// foreach ($array1 as $value){
foreach ($arrayFinal as $value){
$img_counter ++; // Set before function() when using function call.
// Call the function located in gallery_ctrl_body:
createGallery($img_width, $img_style, $filename, $img_name, $img_src, $img_counter, $img_total, $value, $pid_text);
}
// <!-- Next and previous buttons -->
echo '<a class="prev" onclick="plusSlides(-1)">❮</a>';
echo '<a class="next" onclick="plusSlides(1)">❯</a> ';
echo '</div>';
// <!-- END: div class="slideshow-container"-->
echo '<br>';
// <!-- Dot container -->
echo '<div class="dot-container">';
// <!-- The dots/circles -->
echo '<div style="text-align:center">';
$slide_counter = 0;
while ($slide_counter < $img_total) {
$slide_counter ++;
echo '<span class="dot" onclick="currentSlide(' . $slide_counter . ')"></span> ';
}
echo '</div>';
echo '</div>';
// Script include needs to be run at end of page including it.
include_once ('../gallery_ctrl_script.php');
So, this code works but I'm not sure if it's the correct way or approach. If works for me as I have a set image naming convention and my galleries can be controlled from a single source file. It will work for any number of images, if they exist, in the folder where the function is being called from although my properties generally will have less than 50.

PHP: $_POST cannot retrieve existing inputs

I'm attempting to make a button that, based on the selected form/inputs on the page, will bring you to a page called "typeDefine.php?openness=3?conscientiousness=2?extroversion=1?agreeableness=2?neuroticism=1"(the numbers varying based on the selected inputs). However, $selectedNum- the variable that would ideally be containing the $_POST for each input- is returning an error immediately once the page is loaded, saying:
Undefined index
<?php
$typeWords = array("openness", "conscientiousness", "extroversion", "agreeableness", "neuroticism");
$typeLetters = array("o", "c", "e", "a", "n");
$typePath = "";
$correspondingLetters = array("I", "II", "III");
$isFirst = true;
foreach($typeWords as $typeWord)
{
$selectedNum = $_POST[$typeWord];//error here!!!
if(isset($selectedNum))//if got $typeWord in a form
{
$separationChar;
if($isFirst)
{
$separationChar = "?";
$isFirst = false;
}
else
{
$separationChar = "&";
}
$typePath = $typePath . $separationChar . $typeWord . "=" . $selectedNum;//e.g. $typePath = "?openness=3?conscientiousness=2?extroversion=1?agreeableness=2?neuroticism=1" for $_GET method after arriving on next page
}
}
echo 'search for type
<div>';
foreach($typeWords as $typeWord)
{
$typeLetter = substr($typeWord, 0, 1);
echo '<form method = "post" class = "column">';
for($i = 1; $i <= 3; $i++)
{
echo '<input type = "radio" name = "' . $typeWord . '" id = "' . $typeLetter . $i . '"><label for = "' . $typeLetter . $i . '">' . $correspondingLetters[$i - 1] . '</label>';//sets each input name to $typeWord for $_POST above
}
echo '<li class = "textHighlight">' . $typeWord . '</li>
</form>';
}
echo '</div>';
?>
What can I do to fix this error, in turn filling $typePath and making the script correctly bring you to the desired url upon the button's click?
Thanks in advance!
You should perform the isset() test on the $_POST element, not the variable that you set from it.
foreach ($typeWords as $typeWord)
{
if (isset($_POST[$typeWord])) {
$selectedNum = $_POST[$typeWord];
$typePath = $typePath . "?" . $typeWord . "=" . $selectedNum;
}
}
Note that multiple parameters need to be separated with &, ? should only be used at the beginning. There's a built-in function that will create a query string from an array, you can use that:
$typeArray = [];
foreach ($typeWords as $typeWord)
{
if (isset($_POST[$typeWord])) {
$selectedNum = $_POST[$typeWord];
$typeArray[$typeWord] = $selectedNum;
}
}
$typePath = $typePath . "?" . http_build_query($typeArray);
You can also replace the loop with:
$typeArray = array_intersect_key($_POST, array_flip($typeWords));
You execute your code immidiately without a present $_POST array.
You have to wrap your post related code with an if statement like this:
if ($_POST) {
$selectedNum = $_POST[$typeWord];
}

Concatenation & conditions in php

I'm learning PHP and i'm trying to show an " €" when and only when $autocollant_total_ht_custom isset.
This is what i wrote :
$euro = " €";
if (isset($autocollant_total_ht_custom)) {
$autocollant_total_ht_custom = $autocollant_total_ht_custom . $euro;
} else echo " ";
However my " €" is always showing even when $autocollant_total_ht_custom is not set.
I spent 75 minutes on it, trying and failing again and again despite researching.
I also tried with !is_null, !is_empty with the same result.
I'm fairly certain that my logic isn't wrong but the way to do it is.
Anyone to the rescue?
Have a nice Saturday everyone !
Mike.
Edit 1:
A little visual aid image
My goal was to only show the content of a cell if there was indeed something in it. By default i could see 0 in the empty cells.
if (!$autocollant_total_ht_lot10) {
$autocollant_total_ht_lot10 = " ";
} else echo "error ";
if (!$autocollant_total_ht_lot20) {
$autocollant_total_ht_lot20 = " ";
} else echo " ";
if (!$autocollant_total_ht_lot50) {
$autocollant_total_ht_lot50 = " ";
} else echo " ";
if (!$autocollant_total_ht_custom) {
$autocollant_total_ht_custom = " ";
} else echo " ";
I know my code must look primitive but it works and i don't see it making a conflict with what we are trying to achieve in the initial question.
Then, as asked, this is what i'm writing in the table row and table data :
<tr>
<td class=table_align_left>A partir de 100</td>
<td><?php echo $autocollant_prix ?></td>
<td><?php echo $autocollant_custom?></td>
<td><?php echo $autocollant_total_ht_custom?> </td>
</tr>
So in short, i'm trying to not show anything if there's no value to be shown (which is currently working) and then adding a " €" after the variable is there's something to be shown.
Edit 2 :
My primitive code : my_code
Edit 3 :
The $autocollant_total_ht_custom is already conditioned to be shown earlier in this statement :
} elseif($autocollant_quantité >= 90 && $autocollant_quantité <= 99){
$autocollant_quantité_lot50 = 2;
} elseif($autocollant_quantité >= 100 && $autocollant_quantité <= 1000){
$autocollant_custom = $autocollant_quantité;
} else echo "entrée invalide";
$autocollant_total_ht_custom = $autocollant_prix * $autocollant_custom;
$autocollant_total_ht_lot10 = $autocollant_prix_lot10 * $autocollant_quantité_lot10;
$autocollant_total_ht_lot20 = $autocollant_prix_lot20 * $autocollant_quantité_lot20;
$autocollant_total_ht_lot50 = $autocollant_prix_lot50 * $autocollant_quantité_lot50;
$pointeuse_total_ht = $pointeuse_prix * $pointeuse_quantité;
$pointeuse_autocollant_offert = $pointeuse_quantité * 10;
$pointeuse_autocollant_offert_total_ht = $pointeuse_autocollant_offert * $autocollant_prix;
$pointeuse_autocollant_offert_total_ht = $pointeuse_autocollant_offert * $autocollant_prix;
I posted my code if that can help.
Mike.
//$autocollant_total_ht_custom = null;
$autocollant_total_ht_custom = "something that isnt null";
//if you switch the variable assignment above you will see it behaves as expected.
$euro = "€";
if (isset($autocollant_total_ht_custom))
{
echo $autocollant_total_ht_custom = $autocollant_total_ht_custom . " " .$euro;
}
else
{
//$autocollant_total_ht_custom wouldn't be set at all if we reach this point, this is why im un-sure what your requirements are. Nothing would be echoed.
echo $autocollant_total_ht_custom;
}
Something like this maybe? It's hard to understand your exact requirements.
IsSet checks if a variable is set to something if its not null then it passes the test, and if you're manipulating strings at this variable then it will never be null, meaning the euro sign will always show up.
If the variable IS null then you fail the conditional test, hit else and echo nothing a null string.
If you can update your answer with what you would expect "$autocollant_total_ht_custom" to be set to, I can help better.
EDIT:
Seems to me you can simplify what you what, basically we are only concerned with echoing a string at all if there is something set, otherwise there's no point doing anything, so your checks could be as simple as
$autocollant_total_ht_lot10 = null;
$autocollant_total_ht_lot11 = "";
$autocollant_total_ht_custom = "1,000";
$euro = "€";
if (isset($autocollant_total_ht_custom))
{
echo 'ht custom';
echo TDFromString($autocollant_total_ht_custom, $euro);
}
//notice this doesnt output anything because it isnt set
if (isset($autocollant_total_ht_lot10, $euro))
{
echo 'lot 10';
echo TDFromString($autocollant_total_ht_lot10, $euro);
}
//notice this does because the string, while empty is something that isnt null
if (isset($autocollant_total_ht_lot11))
{
echo 'lot 11';
echo TDFromString($autocollant_total_ht_lot11, $euro);
}
//lets set it to null and see what happens
$autocollant_total_ht_lot11 = null;
if (isset($autocollant_total_ht_lot11))
{
echo 'lot 11 AGAIN';
echo TDFromString($autocollant_total_ht_lot11, $euro);
}
//it doesnt get printed!
//create a function that takes the string in question,
//and for the sake of your use case also the currency to output,
//that way you could change 'euro' to 'currency'
//and have the sign change based on what the value of the $currency
//string is, eg $currency = "£"
function TDFromString($string, $currency)
{
return '<td>' . $string . ' ' .$currency . '</td>';
}
Live example : https://3v4l.org/r5pKt
A more explicit example : https://3v4l.org/JtnoF
I added an extra echo to indicate (and newlines) which variable is being printed out you dont need it of course!
I'll just note the function name is a good example of a bad function name, as it not only returns a td around the string but also inserts the currency, you may want to name it a little better :)
EDIT EDIT:
A final edit outside the scope of your question, you should look into keeping your data in arrays and working on them instead.
Using the previous example we can reduce the code to just this !
$autocollant_total_ht_lot10 = null;
$autocollant_total_ht_lot11 = "";
$autocollant_total_ht_lot12 = "2,0000000";
$autocollant_total_ht_custom = "1,000";
$euro = "€";
//create an array, and stick all our strings in it, from now, if we need to do something to one of the strings(or all!), we do it through the array
$arrayofLots = array($autocollant_total_ht_lot10, $autocollant_total_ht_lot11, $autocollant_total_ht_lot12, $autocollant_total_ht_custom);
//go over each array 'entry' so the first time is '$autocollant_total_ht_lot10', then '$autocollant_total_ht_lot11' etc
foreach ($arrayofLots as $lot)
{
//and we've been over this bit :)
//$lot is a variable we set so we have something to refer to for the individual array entry we are on, we could just as easily name it anything else
if (isset($lot))
{
echo TDFromString($lot, $euro);
}
}
function TDFromString($string, $currency)
{
return '<td>' . $string . ' ' .$currency . '</td>';
}
Good day. It looks like you are missing the end brace
if (isset($autocollant_total_ht_custom)) {
$autocollant_total_ht_custom = $autocollant_total_ht_custom . $euro;
} else {
echo " ";
}

How to use if and declare variable in echo statement php

I have a condition within echo statement like this, how to adjust it to make it working:
echo "<option value="http://localhost/myproject/index.php?if(empty($_GET['view'])){echo "view=main-content";}else{$view=basename($_GET['view']);echo "view=".$view;}"></option>";
Many thanks
I noticed that you are using two times view, threfore if you condition enters the else your final url would be something like: ?view=whateverview=whatever2 which is clearly wrong.
$view = (empty($_GET['view']) ? 'main-content' : basename($_GET['view']));
echo "<option value='http://localhost/myproject/index.php?view=" . $view . "'></option>";
If you would like to add more parameters to the URL you'll need to use &.
is this your expected output?
$d = "<option value='http://localhost/myproject/index.php?";
if(empty($_GET['view']))
{
$d .= "view=main-content";
}
else
{
$view=basename($_GET['view']);
$d .="view=".$view;
};
$d .="'>Testing</option>";
echo "<select>". $d."</select>";
You could pass it to a variable, and concatenate it with the rest of your echo statement.
if(empty($_GET['view'])){
$res = 'main-content';
} else {
$res = basename($_GET['view']);
}
echo '<option value="http://localhost/myproject/index.php?view=' . $res . '"></option>';
And since no one else posted it:
echo '<option value="http://localhost/myproject/index.php?view='
. (empty($_GET['view']) ? 'main-content' : basename($_GET['view'])
. '"></option>';

Wordpress theme options - wp_localize_script();

I'm using wp_localize_script(); to alter the JS in a theme I'm building, like so:
wp_enqueue_script('custom', get_bloginfo('template_directory') . '/js/script.js');
$settings = get_option('bro_options');
wp_localize_script('custom','settings',$settings);
While this is good, the way this works is it writes inline JS right before the JS file in question loads, that way you can use certain variables to modify the JS and how it functions.
Problem is, I don't really like this option and how it outputs inline JS. Does anyone know of an alternative to this? I'm pretty sure this isn't how everyone does this with their theme options so there must be another way.
Any help whatsoever would be appreciated, please no links to plugins either.
In short: I'm looking for an alternative to using wp_localize_script(); or a better way of using it.
What I did was create a file called settings.js.php and in that I included the wp-config.php file.
Like so:
<?php
header("Content-type: application/x-javascript");
$c = 0;
while($exists !== true && $notfound !== true) {
$c++;
$str = $str . '../';
if(file_exists($str.'wp-config.php')) {
$exists = true;
include($str.'wp-config.php');
$i = 0;
$amount = sizeof($bro);
echo 'var settings={';
foreach($bro as $key => $value) {
$i++;
if($i<$amount) {
echo $key . ':"' . $value . '",';
} else {
echo $key . ':"' . $value . '"';
}
}
echo '};';
}
if($c === 20) {
$notfound = true;
}
}
?>
Which would output my options in a variable like this:
var settings = {
foo: true,
bar: false
};
I would enqueue this file before my custom.js file that way I could determine if some settings were on and off.
if(settings.foo === true) {
...
}
All this so I don't have inline JS, there may be some cache issues but I'd rather have that than it output all options inline.

Categories