...

View Full Version : Writing javascript function with php



Sleeping_Troll
05-31-2009, 03:53 AM
I am "writing" a javascript function to my page with php:


<html>
<head>
<title>Just about everything</title>
<script type="text/javascript" src="Resources/prototype.js"></script>
<script type="text/javascript" src="Scripts/orbs.js"></script>
<script type='text/javascript'>
function InitialOrbData(){
<?php
for ($i=0;$i<50;$i++){
$offset_result = mysql_query( " Select Floor(Rand() * Count(*)) As `offset` From `Sites` ");
$offset_row = mysql_fetch_object( $offset_result );
$offset = $offset_row->offset;
$SQL="SELECT * FROM Sites LIMIT $offset, 1";
$OrbData = mysql_query($SQL);
$OrbRow = mysql_fetch_array($OrbData);
$Orb[i]= array(ID=>$OrbRow[0],TYPE=>$OrbRow[1],URL=>$OrbRow[2],KEYWORDS=>$OrbRow[3],BID=>$OrbRow[4]);
echo ("
Orb[".$i."].ID='".$Orb[i]['ID']."';
Orb[".$i."].TYPE='".$Orb[i]['TYPE']."';
Orb[".$i."].URL='".$Orb[i]['URL']."';
Orb[".$i."].KEYWORD='".$Orb[i]['KEYWORD']."';
Orb[".$i."].BID='".$Orb[i]['BID']."';
");
}
?>
}
</script>


The function appears quite normal on the page:


<html>
<head>
<title>Just about everything</title>
<script type="text/javascript" src="Resources/prototype.js"></script>
<script type="text/javascript" src="Scripts/orbs.js"></script>
<script type='text/javascript'>
function InitialOrbData(){

Orb[0].ID='9263517364090098';
Orb[0].TYPE='normal';
Orb[0].URL='http:// www.ringernation.com';
Orb[0].KEYWORD='';
Orb[0].BID='0.00';

Orb[1].ID='2147483647476592';
Orb[1].TYPE='sponsored';
Orb[1].URL='http:// www.ringernation.com';
Orb[1].KEYWORD='';
Orb[1].BID='0.00';

Orb[2].ID='9364555231142958';
Orb[2].TYPE='normal';
Orb[2].URL='http://www.ringernation.com';
Orb[2].KEYWORD='';
Orb[2].BID='0.00';

...

Orb[48].ID='2147483647476592';
Orb[48].TYPE='sponsored';
Orb[48].URL='http:// www.ringernation.com';
Orb[48].KEYWORD='';
Orb[48].BID='0.00';

Orb[49].ID='9263517364090098';
Orb[49].TYPE='normal';
Orb[49].URL='http:// www.ringernation.com';
Orb[49].KEYWORD='';
Orb[49].BID='0.00';
}
</script>

notice the script include in red in the first code section, it references the php written script as shown below:


var numOrbs=36; //number of Orbs
var InitialVelocity;
var Zeye; //perspective:distance of eye from box centre
var Zscreen; //perspective:distance of screen from box centre
var OrbBaseRadius
var OrbProximityFactor;
var Xmin,Ymin,Xmax,Ymax,Zmin,Zmax;
var Perspective;
var Dimensions=[];
var Orb=[];
InitialOrbData();


alas I get the error InitialOrbData is not defined, can anybody tell me why?

Kor
05-31-2009, 05:46 AM
You call a function which does not exist (was not loaded yet, as the loading process is sequential, from top to bottom). Switch the order of the external script codes. Or call the function onload


...
var Perspective;
var Dimensions=[];
var Orb=[];
onload=function(){InitialOrbData()}

Sleeping_Troll
05-31-2009, 05:53 AM
Yup! I think that's it! Thx.
That was absolutely correct and I was quite delighted however now I am getting 'Orb.0' is null or not an object

<html>
<head>
<title>Just about everything</title>
<script type="text/javascript" src="Resources/prototype.js"></script>
<script type='text/javascript'>
var Orb=[];
function InitialOrbData(){

Orb[0].ID='2147483647476592';
Orb[0].TYPE='sponsored';
Orb[0].URL='http:// www.ringernation.com';
Orb[0].KEYWORD='';
Orb[0].BID='0.00';

Orb[1].ID='2147483647476592';
Orb[1].TYPE='sponsored';
Orb[1].URL='http:// www.ringernation.com';
Orb[1].KEYWORD='';
Orb[1].BID='0.00';

Kor
05-31-2009, 06:29 AM
You should define your array as a combined 2nd type array of objects;


Orb[0]={};
Orb[0].ID='9263517364090098';
Orb[0].TYPE='normal';
Orb[0].URL='http:// www.ringernation.com';
Orb[0].KEYWORD='';
Orb[0].BID='0.00';

Orb[1]={};
Orb[1].ID='2147483647476592';
Orb[1].TYPE='sponsored';
Orb[1].URL='http:// www.ringernation.com';
Orb[1].KEYWORD='';
Orb[1].BID='0.00';
...

Kor
05-31-2009, 06:32 AM
or, in a JSON more simple way:


Orb[0]={
ID:'9263517364090098',
TYPE:'normal',
URL:'http:// www.ringernation.com',
KEYWORD:'',
BID:'0.00'
}
...

See JSON (http://json.org)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum