• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 578
  • Last Modified:

PHP echo displaying in the wrong place

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
Jonathan Greenberg
Asked:
Jonathan Greenberg
  • 2
  • 2
1 Solution
 
svgmucCommented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Jonathan GreenbergAuthor Commented:
Thank you, Dave! I'll try this and post back as soon as I can.
0
 
Jonathan GreenbergAuthor Commented:
Excellent! Thanks so much for the help, Dave.
0
 
Dave BaldwinFixer of ProblemsCommented:
You're welcome.  It was an interesting question.  Required a little extra thinking to puzzle it out.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now