Link in table tr

1

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

I tried to do this:

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>";
}

But you're not linking, how do you do?

    
asked by anonymous 02.07.2015 / 22:02

4 answers

2

The solution is:

Add

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

At the time of calling, just add tr to tag data-url , thus:

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

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

--- Adding insight.

To add the mouse pointer by going over tr , simply add this line:

style='cursor:pointer'
    
02.07.2015 / 22:08
2

Good !! In these situations I recommend that you use the standard tag events. In this case you can use onclick and then you pass the location.href that receives the link.

See 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>";
}
    
03.07.2015 / 17:52
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>";
}
    
03.07.2015 / 18:03
0

This makes it simple and works without 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>";
    
03.07.2015 / 18:13