Page 1 of 1

can php be used to make a javascript statement

Posted: Mon Oct 29, 2007 1:45 pm
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]

Posted: Mon Oct 29, 2007 1:50 pm
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;

Posted: Mon Oct 29, 2007 1:55 pm
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

Posted: Mon Oct 29, 2007 1:58 pm
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...

Posted: Mon Oct 29, 2007 3:06 pm
by feyd
Is Unit Testing involved in this topic?