[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP echo displaying in the wrong place

Posted on 2011-04-24
5
Medium Priority
?
499 Views
Last Modified: 2012-05-11
I have a php function that echos an html table td 5 times. I'm calling the function from within the tr tags of a table. But it displays above the opening tr tag in the web page source code, and it's causing the table to display incorrectly.

The function is getStudRateTRs(). Here's my html table, along with the php:
<?php 

function getStudRateTRs($n) {
	foreach ($n as $key => $value) {
		echo "
    <td class='right'>test</td>";
	}
}
?>

<table border="1" cellpadding="4" cellspacing="0">
  <tr>
    <td class="h-center">&nbsp;</td><?php 
foreach ($term as $value) { echo "
    <td class='h-center'>" . $value . "</td>";
} ?>

  </tr>
  <tr>
    <td class="h-right-caps">Student</td>
    <td colspan="<?php echo count($term); ?>" class="right"></td>
  </tr><?php 
foreach ($rangeStud as $key => $value) {
	echo "
  <tr>
    <td class='h-right'>" . $value . "</td>
	" . getStudRateTRs($term) . "
  </tr>";
}
?>

</table>

Open in new window

The resulting web page source code, in Firefox, looks like this:
<table border="1" cellpadding="4" cellspacing="0">
  <tr>
    <td class="h-center">&nbsp;</td>
    <td class='h-center'>Annual</td>
    <td class='h-center'>Semi-Annual Session 1</td>
    <td class='h-center'>Spring/Summer</td>
    <td class='h-center'>Semi-Annual Session 2</td>
    <td class='h-center'>Summer</td>
  </tr>
  <tr>
    <td class="h-right-caps">Student</td>
    <td colspan="5" class="right"></td>
  </tr>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
  <tr>
    <td class='h-right'>00-24</td>
	
  </tr>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
  <tr>
    <td class='h-right'>25-30</td>
	
  </tr>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
  <tr>
    <td class='h-right'>31-40</td>
	
  </tr>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
  <tr>
    <td class='h-right'>41-99</td>
	
  </tr>
</table>

Open in new window

But I want it to look like this:
<table border="1" cellpadding="4" cellspacing="0">
  <tr>
    <td class="h-center">&nbsp;</td>
    <td class='h-center'>Annual</td>
    <td class='h-center'>Semi-Annual Session 1</td>
    <td class='h-center'>Spring/Summer</td>
    <td class='h-center'>Semi-Annual Session 2</td>
    <td class='h-center'>Summer</td>
  </tr>
  <tr>
    <td class="h-right-caps">Student</td>
    <td colspan="5" class="right"></td>
  </tr>
  <tr>
    <td class='h-right'>00-24</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
  </tr>

    <td class='right'>test</td>
  <tr>
    <td class='h-right'>25-30</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
  </tr>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
  <tr>
    <td class='h-right'>31-40</td>
  </tr>
  <tr>
    <td class='h-right'>41-99</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
    <td class='right'>test</td>
  </tr>
</table>

Open in new window

The td lines generated by getStudRateTRs() are showing up before the opening tr, even though I've placed the function within the tr tags. What am I doing wrong?

Thanks!
0
Comment
Question by:Jonathan Greenberg
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:svgmuc
ID: 35457179
You need to cycle through $term, again. Your loop ends in line 16, after that it probably has no proper value anymore and get StudRateTRs($term) returns an empty value.
0
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 total points
ID: 35457186
That was Interesting.  Turns out that the 'echo' in the function was occurring Before the 'echo' in the line that called it.  So I split that line into 3 lines as shown below so that they occurred in the right order.  I added a bunch of stuff to see what was doing what so you'll have to revise it for your needs.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP Foreach Table</title>
</head>
<body>
<?php 
$rangeStud = array(1,3,5,7,9);
$term = array('A', 'B', 'C', 'D');

function getStudRateTRs($n) {
	foreach ($n as $key => $value) {
		echo "<td class='right'>".$key."test</td>\n";
		}
}
?>

<table border="1" cellpadding="4" cellspacing="0">
  <tr>
    <td class="h-center">&nbsp;</td>
<?php 
foreach ($term as $value) { 
	echo "<td class='h-center'>" . $value . "</td>\n";
	} 
?>
  </tr>
  <tr>
    <td class="h-right-caps">Student</td>
    <td colspan="<?php echo count($term); ?>" class="right"></td>
  </tr>
<?php 
foreach ($rangeStud as $key => $value) {
	echo "<tr><td class='h-right'>*" . $value . "</td>";
	getStudRateTRs($term); 
	echo "</tr>\n";
}
?>

</table>
</body>
</html>

Open in new window

0
 

Author Comment

by:Jonathan Greenberg
ID: 35457196
Thank you, Dave! I'll try this and post back as soon as I can.
0
 

Author Comment

by:Jonathan Greenberg
ID: 35457255
Excellent! Thanks so much for the help, Dave.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 35457300
You're welcome.  It was an interesting question.  Required a little extra thinking to puzzle it out.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month18 days, 16 hours left to enroll

834 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