Solved

PHP Syntax for Javascript question

Posted on 2013-01-22
1
337 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

809 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