Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Been researching this for an hour. I give up. I'm trying to create an array after populating the array from a MySQL table.

I then want to output the array text elements across the top of a table (table headings)

$sqlRotations="SELECT id, rotationName FROM sched_rotations";
$resultRotations=mysql_query($sqlRotations);
$arrayRotations = array();
while($row_Rotation=mysql_fetch_assoc($resultRotations)){ 
  $arrayRotations[]=$row_Rotation;
} 

...then I'm trying to print out the "rotationName" as table column headings:

<table>
<tr>
<?
foreach( $arrayRotations as $key => $value){
echo "<td>Id: $key, Rotation:". $arrayRotations[$value]." </td>";
}
?>
</tr>

Unfortunately, this gives me the following output in < t d > format:

Id: 0, Rotation: Id: 1, Rotation: Id: 2, Rotation: Id: 3, Rotation: Id: 4, Rotation: Id: 5, Rotation: Id: 6, Rotation: Id: 7, Rotation: Id: 8, Rotation: Id: 9, Rotation: Id: 10, Rotation:


Furthermore if I change the for key value echo to this:

foreach( $arrayRotations as $key => $value){
echo "<td>Id: $key, Rotation:". $value." </td>";

}

then I get this output:

Id: 0, Rotation:Array Id: 1, Rotation:Array Id: 2, Rotation:Array Id: 3, Rotation:Array Id: 4, Rotation:Array Id: 5, Rotation:Array Id: 6, Rotation:Array Id: 7, Rotation:Array Id: 8, Rotation:Array Id: 9, Rotation:Array Id: 10, Rotation:Array

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Is this what you are looking for?

<table>
<tr>
<th>Id</th>
<th>Rotation</th>
</tr>
<?php 
foreach($arrayRotations as $rotation)
{
   printf("<tr><td>%s</td> <td>%s</td></tr>", $rotation['id'], $rotation['rotationName');
}
?>
</table>
share|improve this answer

You're using the foreach() array incorrectly.

<?php
foreach($arrayRotations as $line){
   echo "<td>Id: " . $line['id'] . ' Rotation: '. $line['rotationName'] . '</td>';
}
?>
share|improve this answer
1  
My updated answer is more accurate. –  David Sep 9 '12 at 4:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.