如何在数组中创建href链接

时间:2021-10-19 21:19:57

Actualy I have to populate table data using php array to my dashboard. The table contains the URL. I need to set href which is used to redirect to the dynamic link.(below is my while loop code). I have tried creating href using below the single line echo php code.It worked and I got the clickable link, but it didn't return data inside the table. It showed the data in the table without any alignment.

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

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

But I want to create href inside array kindly anyone help us.

但是我想在数组中创建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 个解决方案

#1


2  

pls add this line

请添加这一行

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

#2


1  

Looks like you have the solution already. Just change how you assign 'label1' to the below.

看起来你已经有了解决方案。只需更改如何将“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);
}

And as the comments mentioned, avoid using mysql_* functions as they're no longer supported (which should be reason enough)

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

#1


2  

pls add this line

请添加这一行

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

#2


1  

Looks like you have the solution already. Just change how you assign 'label1' to the below.

看起来你已经有了解决方案。只需更改如何将“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);
}

And as the comments mentioned, avoid using mysql_* functions as they're no longer supported (which should be reason enough)

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