can php be used to make a javascript statement

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
prophious
Forum Newbie
Posts: 2
Joined: Mon Oct 29, 2007 1:30 pm

can php be used to make a javascript statement

Post by prophious »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


i was woundering if php can be used to make a javascript statement (ie an array) pulled from a .txt document 


like this example code here

within members.php

Code: Select all

<script language="JavaScript"><!--
function makeItem(date,user,loc,web) {
    this.date=date;
    this.user=user;
    this.loc=loc;
    this.web=web;
    return this;
}


theItem = new Array();
var b = -1

<?php
$file = "members.txt";

if (fopen($file, "r")) {
$fil = "members.txt";
$fo = fopen ($fil, "r");
$con = fread ($fo,filesize ($fil));
fclose ($fo);
echo "$con";
} else {
echo "<h3>Sorry there was an error please contact us <h3>";
}
?>
</script>

later in members.php

Code: Select all

<script language="JavaScript"><!--
Text = '';

for (i = 0, n=theItem.length;i<n;i++) {
var cd = '';
var cd = theItem[i].date;
var cu = '';
var cu = theItem[i].user;
var cl = '';
var cl = theItem[i].loc;
var cw = '';
var cw = theItem[i].web;

if ((cl == null) || (cl == '')) {
var cl = '&nbsp';
}

if ((cw == null) || (cw == '')) {
var cw = '&nbsp';
}

if ((cd == null) || (cd == '')) {
var cd = '&nbsp';
}

if ((cu == null) || (cu == '')) {
i++;
} 
else {
     Text += '<tr><td><center><table width="100%" border="1" cellpadding="0" cellspacing="0"><td width="10%">' + cd + '<td width="20%">' + cu + '<td width="20%">' + cl + '<td width="50%">' + cw + '</table></center>';
}}
Text += '';
document.write(Text);
//--></script>
within members.txt

Code: Select all

theItem[(b=b+1)] = new makeItem('6-3-07','Jimmy','LA','');
theItem[(b=b+1)] = new makeItem('6-3-07','juaco','panama','');

is the above even possible if not how would i do something like this without a database


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
seppo0010
Forum Commoner
Posts: 47
Joined: Wed Oct 24, 2007 4:13 pm
Location: Buenos Aires, Argentina

Post by seppo0010 »

Will this work for you?

Code: Select all

<script language="JavaScript"><!--
function makeItem(date,user,loc,web) {
    this.date=date;
    this.user=user;
    this.loc=loc;
    this.web=web;
    return this;
}


theItem = new Array();
<?php readfile('members.txt'); ?>
var b = -1;
prophious
Forum Newbie
Posts: 2
Joined: Mon Oct 29, 2007 1:30 pm

Post by prophious »

thanks the code i provided was just a dummy code unknown if it will work but i'm guessing because of the reply php can be put inside of javascript tags


thanks
User avatar
seppo0010
Forum Commoner
Posts: 47
Joined: Wed Oct 24, 2007 4:13 pm
Location: Buenos Aires, Argentina

Post by seppo0010 »

Yeah, PHP is executed on the server, and javascript on the client, so the client gets what PHP sent, it doesn't matter if it's js code, html or whatever...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Is Unit Testing involved in this topic?
Post Reply