1

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.

1
  • 1
    Would foreach($array as $key => $value) not do what you need? Commented Feb 18, 2016 at 17:12

4 Answers 4

1

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>";

?>
Sign up to request clarification or add additional context in comments.

2 Comments

I think the problem he's having is that $primary isn't an array of arrays, it's an array of objects.
@jhaagsma based on the title it is about getting key from an array to make headers on the first pass.
0

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;
    }
}

Comments

0

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]);

2 Comments

Hmm, I guess I'm not sure how you would create the headers/results all within one loop.
I added a looping example, which also extracts the keys by type casting the obj as an array. Though I just remembered you can actually iterate over an objects properties so that might not be necessary.
0

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.

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.