NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP parsing to Javascript questions

Michele Waldman mmwaldman at nyc.rr.com
Sat Jul 18 06:58:59 EDT 2009


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




More information about the talk mailing list