Solved

PHP Syntax for Javascript question

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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)

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now