Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP Syntax for Javascript question

Posted on 2013-01-22
1
Medium Priority
?
342 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

824 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