Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-31-2009, 03:53 AM   PM User | #1
Sleeping_Troll
New Coder

 
Join Date: May 2009
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Sleeping_Troll is an unknown quantity at this point
Writing javascript function with php

I am "writing" a javascript function to my page with php:
Code:
<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:
Code:
<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:
Code:
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?

Last edited by Sleeping_Troll; 05-31-2009 at 03:57 AM..
Sleeping_Troll is offline   Reply With Quote
Old 05-31-2009, 05:46 AM   PM User | #2
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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
Code:
...
var Perspective;
var Dimensions=[];
var Orb=[];
onload=function(){InitialOrbData()}
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 05-31-2009, 05:53 AM   PM User | #3
Sleeping_Troll
New Coder

 
Join Date: May 2009
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Sleeping_Troll is an unknown quantity at this point
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';

Last edited by Sleeping_Troll; 05-31-2009 at 06:07 AM..
Sleeping_Troll is offline   Reply With Quote
Old 05-31-2009, 06:29 AM   PM User | #4
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
You should define your array as a combined 2nd type array of objects;
Code:
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
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 05-31-2009, 06:32 AM   PM User | #5
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
or, in a JSON more simple way:
Code:
Orb[0]={
ID:'9263517364090098',
TYPE:'normal',
URL:'http:// www.ringernation.com',
KEYWORD:'',
BID:'0.00'
}
...
See JSON
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:27 AM.


Advertisement
Log in to turn off these ads.