如何在数组内创建href链接

时间:2015-09-18 13:36:58

标签: php mysql matomo

Actualy我必须使用php数组将表数据填充到我的仪表板中。该表包含URL。我需要设置href用于重定向到动态链接。(下面是我的while循环代码)。我尝试使用单行echo php代码创建href。它工作,我得到了可点击的链接,但它没有返回表内的数据。它显示表中的数据没有任何对齐。

echo '<html><head></head><a href="'.$data['link'].'" target="_blank">'.$data['link'].'</a></html>';  

但我想在数组中创建href 请任何人帮助我们。

public function getTemperatures()
{
    $mysql_hostname = "localhost";
    $mysql_user     = "root";
    $mysql_password = "";
    $mysql_database = "new";
    $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Oops some thing went wrong");
    mysql_select_db($mysql_database, $bd) or die("Oops some thing went wrong");// we are now connected to database

    $result = mysql_query("SELECT * FROM website"); // selecting data through mysql_query()
    $pie = mysql_query("select count(*) from website where status = 'Error'");

    while($data = mysql_fetch_array($result))
    {
        //echo '<html><head></head><a href="'.$data['link'].'" target="_blank">'.$data['link'].'</a></html>';
        $temperatures[] = array(
            'label1' => $data['link'],
            'label2' => $data['time'],
            'label3' => $data['os'],
            'label4' => $data['browser'],
            'label5' => $data['status'],
            'label6' => $data['location'],
            array('label6' => $data['widget_load_time'])
        );
    }    
    return DataTable::makeFromIndexedArray($temperatures);
}

2 个答案:

答案 0 :(得分:2)

请添加此行

'label1' => '<a href="'.$data['link'].'" target="_blank">'.$data['link'].'</a>',

答案 1 :(得分:1)

看起来你已经有了解决方案。只需更改将“label1”分配给下面的方式即可。

public function getTemperatures()
{
    $mysql_hostname = "localhost";
    $mysql_user     = "root";
    $mysql_password = "";
    $mysql_database = "new";
    $bd =  mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Oops some thing went wrong");
    mysql_select_db($mysql_database, $bd) or die("Oops some thing went wrong");// we are now connected to database

    $result = mysql_query("SELECT * FROM website"); // selecting data through mysql_query()
     $pie = mysql_query("select count(*) from website where status = 'Error'");

    while($data = mysql_fetch_array($result))
    {
        $temperatures[] = array(
            'label1' => '<a href="'.$data['link'].'" target="_blank">'.$data['link'].'</a>',
            'label2' => $data['time'],
            'label3' => $data['os'],
            'label4' => $data['browser'],
            'label5' => $data['status'],
            'label6' => $data['location'],
            array('label6' => $data['widget_load_time'])
        );
    }    
    return DataTable::makeFromIndexedArray($temperatures);
}

正如所提到的评论,避免使用mysql_ *函数,因为它们不再受支持(这应该是足够的理由)