【问题标题】:Output 1 Row only of PHP Array as Hyperlink仅输出 1 行 PHP 数组作为超链接
【发布时间】:2020-10-04 16:52:56
【问题描述】:

我正在输出 MySQL 查询的结果,其中包含有关用户和所选主题的信息。其中一行包含一个电子邮件地址。我希望 php 仅将此行输出为超链接,但将其余部分保留为文本。我该怎么做?

$resultSet = $db->query ("...my query");  
echo $resultSet -> num_rows;
While($rows = $resultSet ->fetch_assoc())
{
   $subject [1] = $rows['First_Name'];
   $subject [2] = $rows['Surname'];
   $subject [3] = $rows['Email_Address'];
   $subject [4] = $rows['Subject1'];
   $subject [5] = $rows['Subject2'];
   $subject [6] = $rows['Subject2'];

  $subjectOutput = array_filter($subject, 'strlen'); 

  $subjectString = implode($subjectOutput, '<br/>'); 

  $output .= "<p> ". $subjectString."  </p>";

}

【问题讨论】:

    标签: php mysql arrays hyperlink


    【解决方案1】:

    试试这个

    $resultSet = $db->query ("...my query");  
    echo $resultSet -> num_rows;
    While($rows = $resultSet ->fetch_assoc())
    {
       $subject [1] = $rows['First_Name'];
       $subject [2] = $rows['Surname'];
       $subject [3] = "<a href='mailto:" . $rows['Email_Address'] . ">" . $rows['Email_Address'] . "</a>";
       $subject [4] = $rows['Subject1'];
       $subject [5] = $rows['Subject2'];
       $subject [6] = $rows['Subject2'];
    
      $subjectOutput = array_filter($subject, 'strlen'); 
    
      $subjectString = implode($subjectOutput, '<br/>'); 
    
      $output .= "<p> ". $subjectString."  </p>";
    }
    

    【讨论】:

      【解决方案2】:

      检查Email_Address 是否为空,如果它不为空 - 将其包装在&lt;a&gt; 标签中:

      $subject [3] = $rows['Email_Address'] 
          ? ('<a href="#">' . $rows['Email_Address'] . '</a>') 
          : '';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-17
        • 2015-08-21
        • 2014-10-03
        • 1970-01-01
        • 1970-01-01
        • 2012-04-27
        • 1970-01-01
        • 2021-07-18
        相关资源
        最近更新 更多