如何从表中的查询生成可链接的数据


How to make linkable data from a query in a table

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

下面是我的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               
}

看起来您还没有在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
 }
?>

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

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>

在这里试试