Solved

PHP Syntax for Javascript question

Posted on 2013-01-22
1
338 Views
Last Modified: 2013-01-22
Hi all,

I have the follow show/hide script that works correctly.

<html>
<head>
<title>my javascript show script</title>

<script type="text/javascript">
      function showStuff(id) {
      if (document.getElementById(id).style.display == 'block')
            document.getElementById(id).style.display = 'none';
            else
            document.getElementById(id).style.display = 'block'
      }
</script>

</head>
<body>

<p><a href="#" onclick="showStuff('answer1'); return false;">What price are your apples?</a><br>
<span id="answer1" style="display: none;">Our apples are 30 pence each. If you buy 10 or more we can sell them at a discounted bulk rate of 25 pence each.</span></p>

</body>
</html>

Open in new window


However when I want to add a PHP loop into the file it does not work. Perhaps I have the syntax wrong for this?

Top of PHP part:

$html = array('total_meditations_today' => '<p>Meditations Today: __  </p>',
              'total_journals_today' => '<p>Journal Entries Today: __ </p>',
			  'total_exercises_today' => '<p>Exercises Logged: Today: __  </p>',
			  'total_dyads_today' => '<p>Dyads Completed Today: __ </p>',
			  'total_ebooks_downloaded_today' => '<p>Ebooks Downloaded: __ </p>',
			  'total_ebooks_shelf_today' => '<p>Ebooks On Shelf: __ </p>'
			  );

$res = array('total_meditations_today' => $total_meditations_today,
             'total_journals_today' => $total_journals_today,
			 'total_exercises_today' => $total_exercises_today,
			 'total_dyads_today' => $total_dyads_today,
			 'total_ebooks_downloaded_today' => $total_ebooks_downloaded_today,
			 'total_ebooks_shelf_today' => $total_ebooks_shelf_today,
			 );
arsort($res, SORT_NUMERIC); //Sort an array in reverse order

?>


Body: 

<title>Untitled Document</title>
<script type="text/javascript">
      function showStuff(id) {
      if (document.getElementById(id).style.display == 'block')
            document.getElementById(id).style.display = 'none';
            else
            document.getElementById(id).style.display = 'block'
      }
</script>
</head>

<body>
<p><a href="#" onclick="showStuff('answer1'); return false;">Today:</a><br>
<span id="answer1" style="display: none;"><?php foreach ($res as $k => $v) {
    echo str_replace('__', $v, $html[$k]) . PHP_EOL;
} ?>
</span></p>

</body>
</html>

Open in new window


Any help would be appreciated.

D
0
Comment
Question by:TLN_CANADA
1 Comment
 
LVL 12

Accepted Solution

by:
sivagnanam chandrakanth earned 500 total points
ID: 38808653
Change your body like this

<body>
<p><a href="#" onclick="showStuff('answer1'); return false;">Today:</a><br></p>
<span id="answer1" style="display: none;">
<?php 
foreach ($res as $k => $v) {
    echo str_replace('__', $v, $html[$k]) . PHP_EOL;
} ?>
</span>

</body>
</html>

Open in new window


I have removed the </p> after </span> and placed it before <span>..

The problem is </p> tag breaks and creates <p> for all lines within foreach statement which in turn moves the content outside span
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
reverse engineer .sql from php files 11 35
PHP Mail error 3 28
Phone Does Not Abide By CSS Breakpoint For Navigation Controls 6 22
Send form to asp server side 6 10
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question