1

I have two seperate arrays I am using in my php page.

The first one holds all of the field names that I will be using to create my html table headers on the UI.

The array of data for this looks like so:

Array
(
[0] => Array
    (
        [fieldID] => 2
        [fieldName] => Project Title
        [fieldAlias] => initiativeTitle
    )

[1] => Array
    (
        [fieldID] => 4
        [fieldName] => Project Description (preview)
        [fieldAlias] => initiativeDescriptionPreview
    )

)

Next, I have a data set of all the records I need to print to the table. The key in this array matches the fieldAlais from the header array.

My goal here is to loop over the header array and get the fieldAlias, then loop over the data and when the fieldAlias from the header row matches a the key in the data row, it prints it out.

Here is how I populate the header array:

$primaryArray = Array();
if(isset($dashboardDetails->results->primary->fields)){
  foreach($dashboardDetails->results->primary->fields as $p){
     $primaryArray[] = array(
        'fieldID' => (int)$p->fieldID,
        'fieldName' => (string)$p->fieldName,
        'fieldAlias' => (string)$p->alias
     ); 
  }
}

This is an example of the data object:

SimpleXMLElement Object
(
[data] => SimpleXMLElement Object
    (
        [initiativeDescriptionPreview] => This is a test description
        [initiativeTitle] => Test
    )

Here is the mess I am working with on the HTML table:

<table class="table table-hover table-striped">
        <thead>
           <tr>
           <?php
              // Loop over the primary fields
              for ($i = 0; $i < count($primaryArray); ++$i) {
                 echo '<th class="small">'.$primaryArray[$i]['fieldName'].'</th>';
              }
           ?>
           </tr>
        </thead>
        <tbody>
           <?php
              // For each field in our primary array
              for ($i = 0; $i < count($primaryArray); ++$i) {

                 // Set our alais
                 $a = $primaryArray[$i]['fieldAlias'];

                 echo '<tr>';
                    // Loop over all of the records
                    foreach($dashboard->data as $value){
                       foreach($value as $key => $val){
                          if($key == $a){
                             echo '<td class="small">'.$val.'</td>';
                          }
                       }
                    }
                 echo '</tr>';
              }
           ?>
        </tbody>
</table>

The result of this is that its printing two rows of data when this should be the same row:

enter image description here

The short end of this is: I have two separate objects, headers and data. I need to print the table headers and then print the data from the other array to its corresponding header.

2
  • Which key in the data object is fieldAlias supposed to match - initiativeTitle? Commented Feb 18, 2016 at 14:58
  • @LloydBanks Yeah, the first td would be initiativeTitle, the second is description Commented Feb 18, 2016 at 15:08

3 Answers 3

1

First of all it isn't clear whether or not the data array, is an array of arrays, which it should be.

You can then loop the data array, the value of which is the row you're working with. Then you can loop the headers array, and print out the element of the row array who's key matches the value of the 'fieldAlias' element of the headers array element that your currently in.

An example:

$headers = Array(
    Array(
        'fieldID' => 2,
        'fieldName' => 'Project Title',
        'fieldAlias' => 'initiativeTitle'
    ),
    Array(
        'fieldID' => 4,
        'fieldName' => 'Project Description (preview)',
        'fieldAlias' => 'initiativeDescriptionPreview'
    )
);

$results = new StdClass();
$results->data = Array(Array('initiativeDescriptionPreview' => 'This is a test description', 'initiativeTitle' => 'Test'));

?>
<table cellspacing="10" cellpadding="10">
    <tr>
        <?php foreach($headers as $headerField): ?>
            <th style="border:1px solid red">
                <?php echo $headerField['fieldName']; ?>
            </th>
        <?php endforeach; ?>
    </tr>
    <?php foreach($results->data as $row): ?>

    <tr>
        <?php foreach($headers as $headerField): ?>
            <td>
                <?php echo $row[$headerField['fieldAlias']]; ?>
            </td>
        <?php endforeach; ?> 
    </tr>
    <?php endforeach; ?>
</table>

enter image description here

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

Comments

0

Looks to me that you're creating a new <tr> element for each of your columns.

I think your HTML looks something like this:

<table class="table table-hover table-striped">
  <thead>
    <tr>
      <th class="small">some name</th>
      <th class="small">some other name</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>value of column 1</td>
    </tr>
    <tr>
      <td>value of column 2</td>
    </tr>
  </tbody>
</table>

Please notice that you probably would expect only one <tr> element in your <tbody>

Comments

0

I tried to create the arrays and objects, which you have described with direct PHP Code, so that I can test my approach. I think you don't need $primaryArray at all. Here is my complete solution:

<?php
$headers = array(
    array("fieldID" => 2, "fieldName" => "Project Title", "fieldAlias" => "initiativeTitle"),
    array("fieldID" => 4, "fieldName" => "Project Description (preview)", "fieldAlias" => "initiativeDescriptionPreview"),
);

class SimpleXMLElementObject {
    public
        $data;
}

$mySimpleXMLElementObject1 = new SimpleXMLElementObject;
$mySimpleXMLElementObject2 = new SimpleXMLElementObject;

$mySimpleXMLElementObject1->data = array(
    "initiativeDescriptionPreview" => "This is a test description",
    "initiativeTitle" => "Test"
);

$mySimpleXMLElementObject2->data = array(
    "initiativeDescriptionPreview" => "This is a test description2",
    "initiativeTitle" => "Test2"
);

$mySimpleXMLElementObjects = array();
$mySimpleXMLElementObjects[] = $mySimpleXMLElementObject1;
$mySimpleXMLElementObjects[] = $mySimpleXMLElementObject2;
?>

<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <table class="table table-hover table-striped">
            <thead>
<?php foreach($headers as $header) { ?>
                <th class="small"><?= $header["fieldName"] ?></th>
<?php } ?>
            </thead>
            <tbody>
<?php foreach($mySimpleXMLElementObjects as $anObject) { ?>
                <tr>
<?php foreach($headers as $header) { ?>
                    <td class="small"><?= $anObject->data[$header["fieldAlias"]] ?></th>
<?php } ?>
                </tr>
<?php } ?>
            </tbody>
        </table>
    </body>
</html>

The resulting table is below:

<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <table class="table table-hover table-striped">
            <thead>
                <th class="small">Project Title</th>
                <th class="small">Project Description (preview)</th>
            </thead>
            <tbody>
                <tr>
                    <td class="small">Test</th>
                    <td class="small">This is a test description</th>
                </tr>
                <tr>
                    <td class="small">Test2</th>
                    <td class="small">This is a test description2</th>
                </tr>
            </tbody>
        </table>
    </body>
</html>

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.