tags:

views:

52

answers:

1

Need some php help in figuring out how use this array I created, but first of all I'm not sure if I done this right?? Its an array with names and href links that I would like to map for given server. Pls let me know if I constructed this properly:

  $server_array = array(
        'server1.domain' => array(
        'href' => 'https://server1.domain.com:8080'
  ),
        'server2.domain' => array(
        'href' => 'https://server2.domain.com:8080'
  ),
        'server3.domain' => array(
        'href' => 'https://server3.domain.com:9999'
 ...
);

I want to map the data from my key Server to one of these links. So far, I've created a table with the server names and all I want to do is map that server name to one of the above hyper links within the table.

Can someone show me how to tweak my print code to do this? Thanks.

Code to display the table with servername:

$keys = array('Server', Target','Set','Time', 'Length','Size','Status');
echo '<table id="stats_1"><tr>';
foreach ($keys as $column)
   echo '<th>' . $column . '</th>';
    echo '</tr>';

$counter=0;
foreach ($data as $row){
  $counter ++;
    $class = $counter % 2 === 0 ? 'alt1' : 'alt2';
    echo '<tr class="' . $class . '">';
     foreach ($keys as $column){
        if (isset($row[$column])){
          echo '<td>' . $row[$column] . '</td>';
        } elseif ($column == 'Status') {
          echo '<td> Check Logs </td>';
        } elseif ($column == 'Length') {
          echo '<td> n/a </td>';
        } elseif ($column == 'Size') {
          echo '<td> n/a </td>';
        } else {
          echo '<td> </td>';
        }
     }
}
echo '</table>';
+2  A: 

If I got the question correctly, I would create the array like:

$server_array = array(
        'server1.domain' => 'https://server1.domain.com:8080',
        'server2.domain' => 'https://server2.domain.com:8080',
        ...
);

And for creating the link you would have to do (assuming $row['Server'] would contain the name like 'server5.domain'):

if ($column == 'Server'){
   echo '<td> <a href="' . $server_array[$row[$column]] . '">' . $row[$column] . '</a></td>';
}

Full code:

$keys = array('Server', 'Target','Set','Time', 'Length','Size','Status');
echo '<table id="stats_1"><tr>';
foreach ($keys as $column) {
   echo '<th>' . $column . '</th>';
}
echo '</tr>';

$counter=0;
foreach ($data as $row){
  $counter ++;
  $class = $counter % 2 === 0 ? 'alt1' : 'alt2';
  echo '<tr class="' . $class . '">';
  foreach ($keys as $column){
     if (isset($row[$column])){
         if ($column == 'Server'){
            echo '<td> <a href="' . $server_array[$row[$column]] . '">' . $row[$column] . '</a></td>';
         } else {
            echo '<td>' . $row[$column] . '</td>';
         }
     } elseif ($column == 'Status') {
         echo '<td> Check Logs </td>';
     } elseif ($column == 'Length') {
         echo '<td> n/a </td>';
     } elseif ($column == 'Size') {
         echo '<td> n/a </td>';
     } else {
         echo '<td> </td>';
     }
  }
}
echo '</table>';
Felix Kling
@Felix - thanks. I will try this. Yes, you understood my question correctly. So the match occurs within the array? If, 'server2.domain' was called, it would do a lookup?
cjd143SD
@cjd143SD: Yes, as `$server_array` is an associative array and `$row['Server']` contains the name of a server, which is also a key of the array, `$server_array[$row['Server']]` will be the same as e.g. `$server_array['server1.domain']` and this will return the URL.
Felix Kling
@Felix - thank you! works great. :-)
cjd143SD