简体   繁体   中英

php - map table row with a href link dynamically

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM