1

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

0

2 Answers 2

2

You're using the foreach() array incorrectly.

<?php
foreach($arrayRotations as $line){
   echo "<td>Id: " . $line['id'] . ' Rotation: '. $line['rotationName'] . '</td>';
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.