Web Analytics Made Easy -
StatCounter inserting javascript into a php file? - CodingForum

Announcement

Collapse
No announcement yet.

inserting javascript into a php file?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • inserting javascript into a php file?

    Hi all-
    I am NOT a programmer, at all! so, if I explain things funny, please forgive me
    a while back i found this lovely little javascript that shows that pahses of the moon. There are a couple others out there but this one does just what i want it to do!
    I had it as the footer on my website- www.dragonmother.com.
    I make and sell magic wands, so the script made a cute little addition!
    Then i changed my site to a php based one, and placed the javascript in the footer agin- no problem.
    Now, however, i want to expand on the script a little and put it on its own page, which is www.dragonmother.com/moonphases.php I put the arguments half into a require() and left the java command part (?) in the page file. That worked.
    Then, i decided to switch some things around, and change where the picture of the moon sits on the page. I *swear* I didn't change anything in the language that frames the script/noscript part of the thing, but nowI can not get the thing to show! When you look at the page you will see how the table just drops off into nowhere. I've been beating my (miniscule) brains out over it, no avail.
    would any kind soul be willing to help me out?
    I
    Last edited by Dragonmom; Mar 3, 2004, 11:07 PM.
    -------------------------------------

    ... so what does it do?

  • #2
    the script;[quote]<!-- This script is featured at JavaScript Planet at
    http://www.intricate.com/javascript/ Check out
    JavaScript Planet for more scripts, links, etc.
    This script has been corrected for this millenium,
    and the images refurbished, by Shawn Longino
    Please leave this comment intact. -->

    <SCRIPT language="JavaScript" type="text/javascript">
    <!--
    function getMoonAge(year, month, day)
    {
    d = Math.floor(year/20)
    r = year-(d*20) //r is the remainder of (year/20)

    while (r>9)
    {
    r = r-19
    }

    r = r*11

    while (r>29)
    {
    r = r-30
    }

    if (month<3)
    {
    month = month+2
    }

    r = r+month+day

    if (year<100)
    {
    r = r-4
    }
    else
    {
    r = r-8.3
    }

    while(r>29)
    {
    r = r-30
    }

    while(r<0)
    {
    r = r+30
    }

    return r
    }

    function getMoonPhase(moonAge)
    {
    if (moonAge<2) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_NEW1;'); ?>"
    if (moonAge<5) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_WAXING_CRESCENT; ?>"
    if (moonAge<11) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_FIRST_QUARTER; ?>"
    if (moonAge<13) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_WAXING_GIBBEOUS; ?>"
    if (moonAge<16) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_FULL; ?>"
    if (moonAge<20) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_WANING_GIBBEOUS; ?>"
    if (moonAge<24) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_FIRST_QUARTER; ?>"
    if (moonAge<29) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_WANING_CRESCENT; ?>"
    if (moonAge<30) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_NEW2; ?>"
    }

    function getMoonImg(moonAge)
    {
    if (moonAge<2) return "/images1/moonnew"
    if (moonAge<5) return "/images1/waxingcresent"
    if (moonAge<11) return "/images1/firstquarter"
    if (moonAge<13) return "/images1/waxinggibbous"
    if (moonAge<16) return "/images1/moonfull"
    if (moonAge<20) return "/images1/waninggibbous"
    if (moonAge<24) return "/images1/lastquarter"
    if (moonAge<29) return "/images1/waningcresent"
    if (moonAge<30) return "/images1/moonnew"
    }


    monthNames = new Array(13)
    monthNames[1] = "Jan"
    monthNames[2] = "Feb"
    monthNames[3] = "March"
    monthNames[4] = "April"
    monthNames[5] = "May"
    monthNames[6] = "June"
    monthNames[7] = "July"
    monthNames[8] = "Aug"
    monthNames[9] = "Sept"
    monthNames[10] = "Oct"
    monthNames[11] = "Nov"
    monthNames[12] = "Dec"

    dayNames = new Array(8)
    dayNames[1] = "Sun"
    dayNames[2] = "Mon"
    dayNames[3] = "Tues"
    dayNames[4] = "Wed"
    dayNames[5] = "Thurs"
    dayNames[6] = "Fri"
    dayNames[7] = "Sat"

    function getLongDate(dateObj)
    {
    theDay = dayNames[dateObj.getDay()+1]
    theMonth = monthNames[dateObj.getMonth()+1]
    theDate = dateObj.getDate()
    theYear = dateObj.getFullYear()
    return ""+theDay+", "+theMonth+" "+theDate+", "+theYear
    }

    function getNextFull(moonAge)
    {
    currMilSecs = (new Date()).getTime()
    daysToGo = 15 - moonAge
    while(daysToGo<2)
    {
    daysToGo = daysToGo+29
    }
    milSecsToGo = daysToGo*24*60*60*1000
    nextMoonTime = currMilSecs+milSecsToGo
    nextMoonDate = new Date(nextMoonTime)
    return nextMoonDate
    }

    function getNextNew(moonAge)
    {
    currMilSecs = (new Date()).getTime()
    daysToGo = 29 - moonAge
    while(daysToGo<2)
    {
    daysToGo = daysToGo+29
    }
    milSecsToGo = daysToGo*24*60*60*1000
    nextMoonTime = currMilSecs+milSecsToGo
    nextMoonDate = new Date(nextMoonTime)
    return nextMoonDate
    }
    //-->

    <script language="JavaScript" type="text/javascript">
    <!--
    (theDate = new Date()
    theYear = theDate.getYear()
    theMonth = theDate.getMonth()+1
    theDay = theDate.getDate()
    theMoonAge = getMoonAge(theYear, theMonth, theDay)
    theMoonPhase = getMoonPhase(theMoonAge)
    document.write(\"<img src=/\"+(theMoonPhase)+\".gif\" align=\"right\" hspace=\"10\"
    >\")
    document.write(\"The moon is <strong>\"+theMoonPhase+\"<br>\")
    document.write(\"Next <strong>new</strong> moon: \")
    document.write(\"<P>\"+getLongDate( getNextNew(theMoonAge) )+\"&nbsp;&nbsp;\")
    document.write(\"Next <strong>full</strong> moon: \")
    document.write(\"+getLongDate( getNextFull(theMoonAge))+\"</p>\")
    document.write(\"<br clear=all>\")

    //--></script>

    [quote]

    The red area shows up on the source code- that seems to be reading as an error in the page file.
    Whyyy?
    -------------------------------------

    ... so what does it do?

    Comment


    • #3
      You use single quotes or double quotes for delimiting string literals in javascript. And it's recommended to use double quotes in HTML attributes. So the best solution is to use single quotes in the js string and double quotes in the HTML attributes.

      document.write('<img src="'+theMoonPhase+'.gif" align="right" hspace="10">');
      document.write('The moon is <strong>'+theMoonPhase+'<br>');
      document.write('Next <strong>new</strong> moon: ');
      document.write('<p>'+getLongDate( getNextNew(theMoonAge) )+' ');
      document.write('Next <strong>full</strong> moon: ');
      document.write(getLongDate( getNextFull(theMoonAge))+'</p>');
      document.write('<br clear="all">');

      If you want to use double quotes in the js string, the code should look like this:

      document.write("<img src=\""+theMoonPhase+".gif\" align=\"right\" hspace=\"10\">");
      Last edited by glenngv; Mar 3, 2004, 11:46 PM.
      Glenn
      vBulletin Mods That Rock!

      Comment


      • #4
        Thank you! Thta shows up in the source code as eror free, now. but the table still isn't showing on the page
        I don't see any php errors?
        -------------------------------------

        ... so what does it do?

        Comment


        • #5
          Well, I was able to find the parseing error that kept the table from showing- but my page shows the text below the script, and still doesn't show the script itself.

          One thing I am wondering about- could my problem be in this section?
          function getMoonPhase(moonAge)
          {
          if (moonAge<2) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_NEW1;'); ?>"
          if (moonAge<5) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_WAXING_CRESCENT; ?>"
          if (moonAge<11) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_FIRST_QUARTER; ?>"
          if (moonAge<13) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_WAXING_GIBBEOUS; ?>"
          if (moonAge<16) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_FULL; ?>"
          if (moonAge<20) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_WANING_GIBBEOUS; ?>"
          if (moonAge<24) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_FIRST_QUARTER; ?>"
          if (moonAge<29) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_WANING_CRESCENT; ?>"
          if (moonAge<30) return "<"php require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON . 'TEXT_NEW2; ?>"
          }
          I wonder if I have the php correct?
          The original script merely had the name of the phase return. What i want is to have several paragraphs of stuff per phase- and I don't want all those paragraphs showing up in the source! So, I thought, using the php require command would make only the text for one phase show up. Maybe I'm wrong?

          Should I move this request to the php forum?
          -------------------------------------

          ... so what does it do?

          Comment

          Working...
          X