Link in tr of table

Asked

Viewed 3,842 times

1

Well, I need every line tr of my table be a link for when the person click redirects to the related page.

I tried to do it this way:

while($linha = mysql_fetch_array($query))
{
    $timestamp = strtotime($linha['DAT_ULTIM_ATUAL']);
    $data = date('d/m/Y H:i', $timestamp);
    $titulo = $linha['TXT_TITUL_PUBLI'];
    $cdPublic = $linha['COD_IDENT_PUBLI'];
    echo "<a href='noticias.php?jr=$cdPublic'><tr>";
    echo "<td>$linha[TXT_TITUL_PUBLI]</td>";
    echo "<td>$linha[TXT_RESMO_PUBLI]</td>";
    echo "<td>$data</td>";

echo "</tr></a>";
}

However this not giving link, how to do ?

4 answers

2


The solution is:

Add

$(document).ready(function(){
    $('table tr').click(function(){
        window.location = $(this).data('url');
        returnfalse;
    });
});

and at the time of calling simply add tbem to tr the tag data-url, being like this:

<tr data-url="seu-link-aqui.html">

The code where the tag is a can take out of the code.

--- Adding conhescimento.

To add the mouse pointer when passing over the tr, just add this line:

style='cursor:pointer'

2

Good!! In these situations I recommend you to use the default events of the tags. In that case you can use: onclick and then you pass the Location.href that receives the link.

Look at my solution

while($linha = mysql_fetch_array($query))
{
    $timestamp = strtotime($linha['DAT_ULTIM_ATUAL']);
    $data = date('d/m/Y H:i', $timestamp);
    $titulo = $linha['TXT_TITUL_PUBLI'];
    $cdPublic = $linha['COD_IDENT_PUBLI'];
    echo "<tr onclick=location.href='*'>";
    echo "<td>$linha[TXT_TITUL_PUBLI]</td>";
    echo "<td>$linha[TXT_RESMO_PUBLI]</td>";
    echo "<td>$data</td>";

echo "</tr>";
}

0

$(document).ready(function(){
    $('tr').click(function(){
        location.href = "noticias.php?jr=" + $(this).attr("data-destino")
    });
});

while($linha = mysql_fetch_array($query))
{
    $timestamp = strtotime($linha['DAT_ULTIM_ATUAL']);
    $data = date('d/m/Y H:i', $timestamp);
    $titulo = $linha['TXT_TITUL_PUBLI'];
    $cdPublic = $linha['COD_IDENT_PUBLI'];
    echo "<tr data-destino='$cdPublic'>";
    echo "<td>$linha[TXT_TITUL_PUBLI]</td>";
    echo "<td>$linha[TXT_RESMO_PUBLI]</td>";
    echo "<td>$data</td>";
    echo "</tr>";
}

0

So it gets simple and works without using js.

$link = "https://google.com"; $link2 = "http://youtube.com"; echo "
<tr>"; echo "
  <td><a href='$link'> Google</a>
  </td>"; echo "
  <td><a href='$link2' target='_blank'> Youtube</a>
  </td>"; echo "</tr>";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.