0

I have the following table:

foreach ($forwards as $data):
    ?>

    <tr style="cursor: pointer" class="main_row">

        <td><?php echo $data['Offer']['id']; ?> </td>
        <td><?php echo $data['Offer']['name'];?> </td>
        <td><?php echo round($data['Stat']['ltr'],2)."%"; ?></td>
        <td><?php echo round($data['Stat']['cpc'],2)."%"; ?></td>
        <td><?php echo $data['Category']['name']?></td>
        <td><?php echo $data['Country']['name'] ?></td>
    </tr>
    <?php
     endforeach; ?>

now the idea is that when ever you click one of the main_row it should redirect to a new url. the problem is that the url contains an id for instance the url could look like www.example.com/details/2 where 2 is the id.

This id is like all of the other data stored in a php variable: $data['Offer']['id'];

Now my question how can i redirect using both php and javascript? is there a work around? .

Please do note that i am fully aware that php i server side and javascript is not. it is because of this i am asking this question.

4

4 回答 4

2
    <table>     
       <?php  foreach ($forwards as $data): ?>                        
                <tr data-link="http://www.example.com/details/<?php echo $data['Offer']['id']; ?>">    
                    <td><?php echo $data['Offer']['id']; ?> </td>
                    <td><?php echo $data['Offer']['name'];?> </td>
                    <td><?php echo round($data['Stat']['ltr'],2)."%"; ?></td>
                    <td><?php echo round($data['Stat']['cpc'],2)."%"; ?></td>
                    <td><?php echo $data['Category']['name']?></td>
                    <td><?php echo $data['Country']['name'] ?></td>
                </tr>
         <?php  endforeach; ?>
    </table>

   <script>
    $('table tr').click(function(){
         document.location.href = $(this).data('link');
    });
   </script>

如果你使用 jQuery。

于 2013-08-29T18:21:16.740 回答
1

如果您使用 jQuery,请执行以下操作:

$('.main_row').click(function(){
   window.location.href = "YOUR_URL"+$(this).attr('id');
});

并将该行的 html 修改为如下所示:

<tr style="cursor: pointer" class="main_row" id="<?php echo $id ?>">

这样 - 任何时候您单击该行,它都会将行 ID 附加到 url 并将您重定向到那里。

于 2013-08-29T18:23:28.620 回答
1

您可以从该行的第一列获取 ID:

$(".main_row").click(function() {
    var id = $(this).find("td:first-child").text();
    window.location.href = 'www.example.com/details/' + id;
});
于 2013-08-29T18:25:24.130 回答
1

如果我正确理解您的问题,那么您不是在寻找重定向。要在 php 中重定向,必须在将任何内容回显到浏览器之前完成。

因为你想在你的显示被渲染后“重定向”,你应该使用锚。

<td><a href="www.example.com/details/<?php echo $data['Offer']['id']; ?>">
    <?php echo $data['Offer']['id']; ?> 
</a></td>

锚点会将浏览器定向到 href url。

于 2013-08-29T18:25:33.693 回答