Reputation: 8970
I have a data set being returned from a database and the keys are the names of the fields I need to print on a table.
For example, if I have a array of 5 keys (fields), I need to create an HTML table that had a heading with each of the keys on it. I would then need to print the results to the table.
Esentially, I need to create an HTML table from an array and the keys in the first result can act as the headers.
Here is my data:
SimpleXMLElement Object
(
[data] => SimpleXMLElement Object
(
[primaryFields] => SimpleXMLElement Object
(
[primary] => Array
(
[0] => SimpleXMLElement Object
(
[Project_Title] => Test
[Project_Description_Preview] => Test Desc
)
[1] => SimpleXMLElement Object
(
[Project_Title] => Blueprint Development Project
[Project_Description_Preview] => We are continuing to improve Blueprint as a tool by collecting feedback from our internal users and developing new features.
)
What would be the best way to go about getting the keys
from the first result in the array? Should this be two separate loops; first one loops to create the headers from the keys and then the second one prints the data?
Project_Title
& Project_Description_Preview
would be the table headers in this case.
Upvotes: 1
Views: 70
Reputation: 1922
Note: this will work for any number of database columns, whether they are known or not.
You should be able to build the whole table like this:
//where $obj = your output...
//get the column names, convert to an object and store inside an array of one element
$keys = array(json_decode(json_encode(array_keys(get_object_vars($obj[0])))));
//create the output array by merging the keys as the first element with the existing dataset into a new array
$output = array_merge($keys, $obj);
//here is the output step
echo "<table><thead>";
foreach($output as $record_num => $record) {
echo '<tr>';
$cells = get_object_vars($record);
foreach ($cells as $column => $cell) {
echo '<td class="' . $column . '">' . $cell . '</td>';
}
echo '</tr>';
//if this is the first element of the array, it is the column names. After these are output, the rest should be in the tbody tag
if ($record_num == 0) { echo '</thead><tbody>';}
}
echo "</tbody></table>";
This also adds a class name to each cell that corresponds to its database column, which can be used for vertical css styling or javascript selectors after the fact for UI considerations. This can be removed if not needed though.
Upvotes: 0
Reputation: 2454
My typical approach would be just to grab the keys when you first descend into the data on your first iteration, assuming you're looping over it.
Looping like so (as a bonus lets extract the keys just in case):
$primary = $obj->data->primaryFields->primary; //an array
$first = true;
$keys = array();
foreach ($primary as $obj) {
if ($first) {
$first = false;
$keys = get_array_keys((array)$obj));
make_headers($obj,$keys); //do header things here
}
make_table_stuff($obj,$keys); //do table stuff
}
But, you could always do:
$keys = get_object_vars($obj->data->primaryFields->primary[0]);
Upvotes: 0
Reputation: 2847
I will leave it to you to parse the data you get back. From the look of the object get to primary.
<?php
//You will need to parse the object to get this.
$primary = array(
array(
"Project_Title" => "Test",
"Project_Description_Preview" => "Test Desc"
),
array(
"Project_Title" => "Blueprint Development Project",
"Project_Description_Preview" => "We are continuing to improve Blueprint as a tool by collecting feedback from our internal users and developing new features."
)
);
echo "<table>";
for($i = 0; $i < count($primary); $i++){
//Only create the head on your first object
if($i==0){
echo "<thead>";
//build header here
echo "<tr>";
foreach($primary[$i] as $key => $value){
echo "<th>" . $key . "</th>";
}
echo "</tr>";
echo "</thead><tbody>";
}
//Then create the rows and data like all php...
echo "<tr>";
foreach($primary[$i] as $value){
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</tbody></table>";
?>
Upvotes: 1
Reputation: 9583
you have to browse your array from a certain place, say
$arr->data->primaryFields->primary
as $primary
.
considering the array from primary
.
foreach($primary as $values){
foreach($values as $key => $ value){
echo $key." & ".$value;
}
}
Upvotes: 0