如何从表中的查询创建可链接数据

时间:2016-09-07 07:43:43

标签: javascript php

如何使我的查询中的数据可链接/可点击,以便我可以在单击时运行某个功能。需要可链接/可点击的数据是日期,点击后将运行一个函数。

以下是我在PHP中的代码:

if (isset($_POST['go'])){
    if (((!empty($_POST['from'])&&!empty($_POST['to'])))&&((!empty($_POST['from'])&&!empty($_POST['to'])))){
        $from = $_POST['from'];
        $to = $_POST['to'];
        $from =explode('/',$from);
        $from = "$from[2]-$from[0]-$from[1]";
        $to = explode ('/',$to);
        $to = "$to[2]-$to[0]-$to[1]";
        $query= mysqli_query($con,"SELECT Date, Time, Latitude,Longitude,Depth,Magnitude 
                                   FROM bulletin WHERE Date between '$from' and '$to';");
        $the_array  = array();
        while($row = mysqli_fetch_array($query)){
            $date = $row['Date'];
            $time = $row['Time'];
            $latitude= $row['Latitude'];
            $longitude= $row['Longitude'];
            $depth =$row['Depth'];
            $magnitude = $row['Magnitude'];
            $the_arraypei[] = array($row['Date'] ); //added
            echo '<tr class="normalRow">
                     <td onClick="document.location.href="http://www.yoursite.com";">'.$date.'</a></td>
                     <td border="1">'.$time.'</td>
                     <td border="1">'.$latitude.'</td>
                     <td border="1">'.$longitude.'</td>
                     <td border="1">'.$depth.'</td>
                     <td border="1">'.$magnitude.'</td>
                  </tr>';
        }
    $js_array1 =$the_arraypei;//added               
}

2 个答案:

答案 0 :(得分:0)

您似乎没有在td中打开锚标记<a>

另外,避免在PHP echo语句中编写HTML。

<?php 
 if (isset($_POST['go'])){
    /* Other code */
    while($row = mysqli_fetch_array($query)){
      /* Loop statements... */ 
      $the_arraypei[] = array($row['Date'] ); 
?>
<tr class="normalRow">
    <td> 
       <a href="http://www.yoursite.com";"><?php echo $date; ?></a>
    </td>
    <td border="1"><?php echo $time ?> </td>
    <!-- Other tds.... -->
    <td border="1"><?php echo $magnitude; ?></td>
</tr>

<?php } 
   js_array1 = $the_arraypei; //added
 }
?>

答案 1 :(得分:0)

我认为你必须修改你的代码:

echo '<tr class="normalRow"><td><a href="http://www.google.it" target="_blank">'.$date.'</a></td><td border="1">'.$time.'</td><td border="1">'.$latitude.'</td><td border="1">'.$longitude.'</td><td border="1">'.$depth.'</td><td border="1">'.$magnitude.'</td></tr>';

如果你想在没有javascript的情况下打开一个新窗口。否则,如果你想执行一个javascript函数,你可以这样做:

echo '<tr class="normalRow"><td><button onClick="executeMe()">'.$date.'</button></td><td border="1">'.$time.'</td><td border="1">'.$latitude.'</td><td border="1">'.$longitude.'</td><td border="1">'.$depth.'</td><td border="1">'.$magnitude.'</td></tr>';

在页面上你应该添加

<script type="text/javascript">
    function executeMe() {
        console.log('Hello World!');
    }
</script>

试试here