2

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
  • 1
    What is your problem? You have a syntax error in your code. Is this the problem? Can a server (like 'server1.domain') have multiple links? If not, why don't you create your array like 'server1.domain' => 'https://server1.domain.com:8080' ? Commented Sep 13, 2010 at 22:00
  • @Felix - yes, my code for the array of servers is what i need help with. No, multiple links. My data for the server will be either one of those links listed. So if I have a table row of 'server1.domain', then display it as a hyperlink. How do I print it out in my foreach loop? Commented Sep 13, 2010 at 22:05

1 Answer 1

2

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>';
Sign up to request clarification or add additional context in comments.

2 Comments

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: 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.

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.