Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    May 2009
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •