简体   繁体   English

php-动态映射带有href链接的表行

[英]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. 我有一个名称=值对(Server = server.alias),正在提取其中的值,并希望使其超链接到另一个网页。

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. 我需要帮助的是弄清楚将别名映射到具有特定href链接的代码,我认为我必须将其硬编码。 The href link is different for each alias, so there is no pattern there. 每个别名的href链接都不同 ,因此那里没有模式。

Would an if statement be appropriate for this? if语句是否适合于此? 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. 我认为解决问题的关键是,您将需要更多信息才能使用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循环将如下所示:

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. 缺少格式代码,但是您知道了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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