tags:

views:

34

answers:

1

I have an array which i use to dynamically create a table. I have a name=value pair (Server=server.alias) which values are being extracted and would like to make it hyperlinked to another webpage.

What I need help is figuring out the code to map the alias name with a specific href link which I think I will have to hard code. The href link is different for each alias, so there is no pattern there.

Would an if statement be appropriate for this? Any other suggestions to do this? Thank you.

Expected Output:

Server  
----------------
server1.alias  <-- hreflink = http://server1.name.com:/9999
server2.alias  <-- hreflink = http://server2.name.colo.com:/2999

My code so far generating the dynamic rows look like this:

$keys = array('Server');                                                              
echo '<table><tr>';                             
foreach ($keys as $column)
   echo '<th>' . $column . '</th>';
    echo '</tr>';

foreach ($data as $row){
   echo '<tr>';                                         
     foreach ($keys as $column)                 
        if (isset($row[$column])){                                                                   
          echo '<td>' . $row[$column];
          } else {
          echo '<td>' . '' . '</td>';
        }
}
echo '</table>';
+1  A: 

I think that the key to solving your problem is that you will need more information to be able to create the list that you want with a foreach. My suggestion would be to use something like this :

$server_array = array(
  'server1' => array(
    'alias' => 'server1.alias',
    'href' => 'http//server1.name.com'
  ),
  'server2' => array(
    'alias' => 'server2.alias',
    'href' => 'http//server2.name.colo.com'
  )
);

You definitely need all info in your array, otherwise, you'll never be able to do what you want.

EDIT:

With the above array, the foreach loop would be like this :

foreach($server_array as $server_id => $server_info)
{
  echo 'Server ID: '.$server_id;
  echo 'Server Alias: '.$server_info['alias'];
  echo 'Server URL: '.$server_info['href'];
}

The formatting code is missing, but you get the idea.

Gabriel
so essentially, $server_array will feed into the foreach loop? so basically do a compare of the name=value as a key to the href? thanks.
cjd143SD
I edited my answer to add an example of the foreach that you would need to print everything. Let me know if it helps.
Gabriel