NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP parsing to Javascript questions

webmaster at vbplusme.com webmaster at vbplusme.com
Sat Jul 18 07:27:29 EDT 2009


Hello Michele,

Saturday, July 18, 2009, 6:58:59 PM, you wrote:

> This is a long post.

> When you say dynamically I think of ajax.

> It sounds like you only need this at runtime though.

> You could javascript eval and php json formatted variable.

> The downside is the json contents are visible when the user views source.

> Do you really need to place the links into javascript to display them?

> Why not generate the html in the html file as follows.  It's added to the
> dom and you can still manipulate it the javascript.

> <?php for ($cnt = 0; $cnt < sizeof($the_json_array); $cnt++) { 
>   echo "<a name='links[]' id='links".$cnt."'
> href='".$the_json_array[$cnt]->link."'>".$the_json_array[$cnt]->display."</a
>>";
} ?>>

> Michele
>> -----Original Message-----
>> From: talk-bounces at lists.nyphp.org [mailto:talk-bounces at lists.nyphp.org]
>> On Behalf Of webmaster at vbplusme.com
>> Sent: Saturday, July 18, 2009 6:24 AM
>> To: talk at lists.nyphp.org
>> Subject: [nycphp-talk] PHP parsing to Javascript questions
>> 
>> Hello talk,
>> 
>> Hello and greetings.
>> 
>> I have a standalone javascript that I have customized to do a banner
>> display and I have a PHP script that loads link information into a
>> MySQL. The javascript uses these links currently in its display but
>> the links are manually entered into the javascript data definition,
>> for example:
>> [code]
>> 
>> var pausecontent=new Array()
>> pausecontent[0]='<a href=""/></a>'
>> pausecontent[1]='<a href=""/></a>'
>> pausecontent[2]='<a href=""/></a>'
>> pausecontent[3]='<a href=""/></a>'
>> pausecontent[4]='<a href=""/></a>'
>> 
>> //the rest of the javascript that processes the array is here
>> 
>> [/code]
>> 
>> I would like to generate this array information in PHP and insert it
>> into the javascript dynamically. Javascript doesn't have an include
>> and the ones that I have been able to find are pretty ugly.
>> 
>> I was thinking that I could use a PHP script to dynamically generate
>> the whole script and storing it or possibly split the javascript into
>> pieces, generate the array (array.js) part of the script dynamically and
>> store
>> the run part(main.js) of it with out the array definition then use php to
>> put
>> them together at run time.    For example ( I haven't tested this yet
>> so I don't know if it works):
>> 
>> [code]
>> // contents of allscripts.php
>> <?php
>> header("Content-Type: text/javascript");
>> include "main.js";
>> include "array.js";
>> ?>
>> [/code]
>> 
>> // this call would replace the one I am using now
>> <script type="text/javascript" src="allscripts.php"></script>
>> 
>> 
>> The links that populate the array periodically change so I use PHP and
>> MySQL to track and store the most current versions. I want to be able
>> to do a query to populate the array and do a seamless merge into the
>> javascript to do the display.
>> 
>> This all seams pretty kludgy to me so I am open to any suggestions
>> about how to do it more elegantly.
>> 
>> TIA for any ideas.
>> 
>> 
>> --
>> Best regards,
>> 
>> 
>> _______________________________________________
>> New York PHP User Group Community Talk Mailing List
>> http://lists.nyphp.org/mailman/listinfo/talk
>> 
>> http://www.nyphp.org/show_participation.php

> _______________________________________________
> New York PHP User Group Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk

> http://www.nyphp.org/show_participation.php

> __________ Information from ESET NOD32 Antivirus, version of virus
> signature database 4256 (20090718) __________

> The message was checked by ESET NOD32 Antivirus.

> http://www.eset.com



Sorry for the long post, I have been thinking about the problem for a
couple of days so my question mushroomed on me, lol.

The javascript is loaded in the at runtime inside the head tags from a
.js file that contains the arrays and script. I hacked it so that I
could run it inside a vBulletin template. The original script only had
two or three links and I modified it to accommodate 20. The problem I
ended up with is that I have now twenty links inside the javascript
and because they need to be "modifiable" because they are sponsor
links that occasionally change, I wanted to move them into the
database instead of the javascript for easier management. I have to
think about how I would implement the json solution and yes, I think
ajax might be the real answer but because I do almost all of my mod
work in PHP, I was leaning in that direction.

thanks for the reply.

Michael





More information about the talk mailing list