We help IT Professionals succeed at work.

PHP echo displaying in the wrong place

Jonathan Greenberg
on
834 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!
Comment
Watch Question

Commented:
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.
Fixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Jonathan GreenbergWeb Developer

Author

Commented:
Thank you, Dave! I'll try this and post back as soon as I can.
Jonathan GreenbergWeb Developer

Author

Commented:
Excellent! Thanks so much for the help, Dave.
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
You're welcome.  It was an interesting question.  Required a little extra thinking to puzzle it out.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.