Jump to content

nest php array in .js file?


davediamondprovo

Recommended Posts

I have a web application which uses dhtml tooltip message version 1.2. For one of the tooltips I have inserted (by hand) the length and characters for each of the arrays listed below. It works great but if I change any of the arrays or add any arrays to the list below, I also have to update the dhtml tooltip style.js file. This is a pain as you can imagine. I want to be able to parse the arrays below formatted nicely for display in the style.js file. Then, I only have to update the config.php file and the changes appear in the .js file. Viola!

[b]config.php code[/b]

[code]$ScriptLength[] = array('display'=>':05 or less', 'value'=>'4, 44');
$ScriptLength[] = array('display'=>':10 or less', 'value'=>'4, 88');
$ScriptLength[] = array('display'=>':20 or less', 'value'=>'4, 175');
$ScriptLength[] = array('display'=>':30 script', 'value'=>'325, 400');
$ScriptLength[] = array('display'=>':60 script', 'value'=>'725, 800');[/code]

[b]What I need to do is call these arrays and display them like the example below;[/b]

[code]:05 or less 4 to 44 characters
:10 or less 4 to 88 characters
:20 or less 4 to 175 characters
:30 or less 325 to 400 characters
:60 or less 725 to 800 characters[/code]

[b]inside this code below (specifically Text[8]);[/b]

[code]var FiltersEnabled = 0 // if your not going to use transitions or filters in any of the tips set this to 0

Text[0]=["Quick Help","<span class=credit>simply hover your mouse over each <font color='#014D7E'>blue</font> word below for a helpful tip.</span>"]
Text[1]=["Quick Help","<span class=error>name</span> <span class=credit>- please enter your full name.</span>"]
Text[2]=["Quick Help","<span class=error>email</span> <span class=credit>- please enter a valid email address.</span>"]
Text[3]=["Quick Help","<span class=error>email copy</span> <span class=credit>- please choose whether or not you'd like to receive a copy of your scripts via email.</span>"]
Text[4]=["Quick Help","<span class=error>order#</span> <span class=credit>- please enter the order# found in the receipt you received via email from my shop.</span>"]
Text[5]=["Quick Help","<span class=error>no. of scripts</span> <span class=credit>- please choose the number of scripts you would like to create.</span>"]
Text[6]=["Quick Help","<span class=error>choose size</span> <span class=credit>- please choose the expected size of this script.</span>"]
Text[7]=["Quick Help","<span class=error>current characters</span> <span class=credit>- displays the current number of characters in this script. only letters and numerals are counted against this number. spaces are also excluded. <font color='red'><i>Hint:</i></font> Place instructions regarding your script in (parentheses). they will not be included in your <font color='#71C271'><i>Current Characters</i></font> count.</span>"]
Text[8]=["Quick Help","<span class=error>selected size</span> <span class=credit>- this is the length you selected for this script.</span><br><br>
<span class=error>:05 or less</span> <span class=credit> 4 to 44 characters</span><br><span class=error>:10 or less</span> <span class=credit> 4 to 88 characters</span><br><span class=error>:20 or less</span><span class=credit> 4 to 175 characters</span><br><span class=error>:30 or less</span> <span class=credit> 325 to 400 characters</span><br><span class=error>:60 or less</span> <span class=credit> 725 to 800 characters</span>"]
Text[9]=["Quick Help","<span class=error>you entered</span> <span class=credit>- the total number of characters you entered for this script.</span>"]
Text[10]=["Quick Help","<span class=error>choose size</span> <span class=credit>- please choose the expected size of this script. For example, if you are requesting a simple voice over music commerical or promo; select a 'voice only' length that suits your needs.<br><br><span class=error>:05 or less</span> <span class=credit> 4 to 44 characters</span><br><span class=error>:10 or less</span> <span class=credit> 4 to 88 characters</span><br><span class=error>:20 or less</span> <span class=credit> 4 to 175 characters</span><br><span class=error>:30 or less</span> <span class=credit> 325 to 400 characters</span><br><span class=error>:60 or less</span> <span class=credit> 725 to 800 characters</span>"]
Text[11]=["Quick Help","<span class=error>you last entered</span> <span class=credit>- this is the number of characters you last submitted for this script.</span>"]
Text[12]=["Quick Help","<span class=credit>Click here to visit my website.</span>"]
Text[13]=["Quick Help","<span class=credit>Having trouble using Script Buddy? Please visit the Support Forum and tell me about it.</span>"]

Style[3]=["white","black","#B4C6DD","#FFFFFF","","","","","","","left","","","",300,"",1,2,10,10,"","","","",""]

applyCssFilter()[/code]

This would eliminate the need to update both files.

Can anyone offer any assistance with this? I'm not well versed in PHP but I know enough to be dangerous.

Dave
Link to comment
https://forums.phpfreaks.com/topic/32196-nest-php-array-in-js-file/
Share on other sites

Well I figured out how to parse the arrays as needed..

[code]<?
require ("config.php");
// open the table

echo ('<table border="0" cellpadding="2" cellspacing="0" style="border-collapse: collapse">');

// List all lengths and character counts

foreach($ScriptLength as $Item)
{
echo ('<tr><td width="30%"><span class="error">');
echo $Item['display'], "</span></td>";
echo ('<td width="70%"><span class="credit">');
echo ereg_replace(",", " to ", $Item['value']), "</span></td></tr>";
}

// close the table

echo ('</table>');
?>[/code]

My javascript errors out though when I place this code in the .js file. Anyone, anyone?
Remember that PHP executes on the server and Javascript on the client. By the time Javascript sees the code, PHP is long gone. Also, if the Javascript that contains this PHP code isn't in a file with an extension of ".php", the PHP code will not be processed.

Ken
Ken,

thanks for the reply. I have now renamed my style.js to style2.php and changed the reference to the file in my HTML to:

[code] <SCRIPT type="text/javascript" src="js/style2.php"></SCRIPT>[/code]

and yet, I still can't get the php to execute.

Here's the contents of the style.php file;

[code]/*
Please refer to readme.html for full Instructions

Text[...]=[title,text]

Style[...]=[TitleColor,TextColor,TitleBgColor,TextBgColor,TitleBgImag,TextBgImag,TitleTextAlign,TextTextAlign, TitleFontFace, TextFontFace, TipPosition, StickyStyle, TitleFontSize, TextFontSize, Width, Height, BorderSize, PadTextArea, CoordinateX , CoordinateY, TransitionNumber, TransitionDuration, TransparencyLevel ,ShadowType, ShadowColor]
*/

var FiltersEnabled = 0 // if your not going to use transitions or filters in any of the tips set this to 0

Text[0]=["Quick Help","<span class=credit>simply hover your mouse over each <font color='#014D7E'>blue</font> word below for a helpful tip.</span>"]
Text[1]=["Quick Help","<span class=error>name</span> <span class=credit>- please enter your full name.</span>"]
Text[2]=["Quick Help","<span class=error>email</span> <span class=credit>- please enter a valid email address.</span>"]
Text[3]=["Quick Help","<span class=error>email copy</span> <span class=credit>- please choose whether or not you'd like to receive a copy of your scripts via email.</span>"]
Text[4]=["Quick Help","<span class=error>order#</span> <span class=credit>- please enter the order# found in the receipt you received via email from my shop.</span>"]
Text[5]=["Quick Help","<span class=error>no. of scripts</span> <span class=credit>- please choose the number of scripts you would like to create.</span>"]
Text[6]=["Quick Help","<span class=error>choose size</span> <span class=credit>- please choose the expected size of this script.</span>"]
Text[7]=["Quick Help","<span class=error>current characters</span> <span class=credit>- displays the current number of characters in this script. only letters and numerals are counted against this number. spaces are also excluded. <font color='red'><i>Hint:</i></font> Place instructions regarding your script in (parentheses). they will not be included in your <font color='#71C271'><i>Current Characters</i></font> count.</span>"]
Text[8]=["Quick Help","<span class=error>selected size</span> <span class=credit>- this is the length you selected for this script.</span><br><br><? include ("config.php"); echo ('<table border=0 cellpadding=2 cellspacing=0'); foreach($ScriptLength as $Item) { echo ('<tr><td width=30%><span class=error>'); echo $Item['display'], "</span></td>"; echo ('<td width=70%><span class=credit>'); echo ereg_replace(",", " to ", $Item['value']), "</span></td></tr>"; } echo ('</table>'); ?>"]
Text[9]=["Quick Help","<span class=error>you entered</span> <span class=credit>- the total number of characters you entered for this script.</span>"]
Text[10]=["Quick Help","<span class=error>choose size</span> <span class=credit>- please choose the expected size of this script. For example, if you are requesting a simple voice over music commerical or promo; select a 'voice only' length that suits your needs.<br><br><? include ("config.php"); echo ('<table border=0 cellpadding=2 cellspacing=0'); foreach($ScriptLength as $Item) { echo ('<tr><td width=30%><span class=error>'); echo $Item['display'], "</span></td>"; echo ('<td width=70%><span class=credit>'); echo ereg_replace(",", " to ", $Item['value']), "</span></td></tr>"; } echo ('</table>'); ?>"]
Text[11]=["Quick Help","<span class=error>you last entered</span> <span class=credit>- this is the number of characters you last submitted for this script.</span>"]
Text[12]=["Quick Help","<span class=credit>Click here to visit my website.</span>"]
Text[13]=["Quick Help","<span class=credit>Having trouble using Script Buddy? Please visit the Support Forum and tell me about it.</span>"]

Style[3]=["white","black","#B4C6DD","#FFFFFF","","","","","","","left","","","",300,"",1,2,10,10,"","","","",""]

applyCssFilter()[/code]

Please see http://www.davediamondonline.com/scriptbuddy2/ and notice the [b]Choose Size[/b] and [b]Selected Size[/b] hyperlink tooltip on Steps 2 and 3 which should display the php code above but alas it does not.

Advice?

Dave

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.