php - 动态地使用 href 链接映射表行

发布于 2024-09-15 07:18:31 字数 1116 浏览 1 评论 0原文

我有一个数组,用于动态创建表。 我有一个名称=值对 (Server=server.alias),正在提取其中的值,并希望将其超链接到另一个网页。

我需要帮助的是找出将别名与特定 href 链接映射的代码,我认为我必须对其进行硬编码。每个别名的 href 链接都不同,因此没有模式。

if 语句适合于此吗?还有其他建议可以做到这一点吗?谢谢。

预期输出:

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

到目前为止,我生成动态行的代码如下所示:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

燃情 2024-09-22 07:18:31

我认为解决您问题的关键是您需要更多信息才能使用 foreach 创建您想要的列表。我的建议是使用这样的东西:

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

你肯定需要数组中的所有信息,否则,你将永远无法做你想做的事。

编辑:

对于上面的数组,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'];
}

缺少格式化代码,但您明白了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文