【问题标题】:click on a link in a table, then check in what row the link was单击表格中的链接,然后检查链接所在的行
【发布时间】:2017-04-20 16:21:56
【问题描述】:

我想做的是这样的:

我正在写博客。现在我想在我的 mysql-database 中进行查询,以查找其中的所有信息。之后,它应该在表格中回显我网站上博客的标题。

然后我想让人们选择他们想看哪个博客。因此,当他们点击第一个标题时,他们将看到该博客的内容。

所以现在的问题是我不知道如何在每个标题上都添加href,这些标题都转到另一个php文件。在该文件中,它应该知道单击了哪一行/标题,然后它应该在数据库中进行另一个查询,以找到该标题的内容。然后它应该在网站上回显它。

我把桌子弄好了。我也知道如何使href 指向其他页面。我唯一需要知道的是点击了哪个标题/href。

是否可以仅使用 php 来实现?如果是,请解释一下。

我希望我清楚自己想要做什么。

编辑:这是我目前的代码:

 <?php
session_start();
    $connectie = mysql_connect('localhost', 'root', 'usbw');
    if ($connectie == false){
        echo 'Er is iets fout gegaan met de connectie van de database';
    }

    if (mysql_select_db('dldatabase', $connectie) == false) {
    echo 'Er kon geen verbinding met de database gemaakt worden';
    }

    $query = "Select *
        from forum";
    $resultaat = mysql_query($query, $connectie);
    echo "<table border='1'>
    <tr>
    <th>Tijd</th>
    <th>Gebruikersnaam</th>

    <th>Titel</th>
    </tr>";


`while($row = mysql_fetch_array($resultaat))
  {
  $_SESSION['row'] = $row;
  echo "<tr>";
  echo "<td>" . $row['Tijd'] . "</td>";
  echo "<td>" . $row['Gebruikersnaam'] . "</td>";
  echo "<td>" . '<a class="formtitellink" href="forumdocument.php">' . $row['Titel'] . '</a>' . "</td>";
  echo "</tr>";
  }
echo "</table>";`

这是博客页面

这就是博客内容的所在±

session_start();
$row = $_SESSION['row'];
$row = $row + 1;
$query = "Select titel, inhoud
        from forum
        where `ID` = '$row'";
$resultaat = mysql_query($query, $connectie);
echo "$resultaat";

【问题讨论】:

  • 你能给我们看看代码吗?到目前为止你做了什么?
  • 想一想 URL 上的 GET 参数是如何工作的,以及如何在 PHP 脚本中使用它们。一旦您可以发布一些显示基本理解的代码,您可能会得到一些人的帮助。
  • 请澄清您想要的是整个博客的代码,还是只是点击带有链接的 td 背后的逻辑,该链接调用另一个显示内容的脚本。您想要的一切似乎都可以使用 php,但为了帮助我需要在问题中更加具体
  • 我在问题中发布了我的代码:)。

标签: php href html-table


【解决方案1】:

您始终可以使用像 Wordpress 这样的框架,但如果您想从头开始使用这种结构,那么您应该阅读更多有关 URL 操作和使用 GET 参数的信息。

最简单的解决方案是:

  1. 创建一个页面说 post.php

  2. 现在,每次您希望显示帖子时,使用 get 发送 范围。例如:“page.php?id=hello-world”

  3. 在 post.php 上,您可以读取此“id”参数,然后触发 相应地查询。
  4. 然后在page.php上打印结果数据

【讨论】:

    【解决方案2】:

    您需要将第一页和第二页等页面作为超链接打开。 下面是示例代码:

    <?php
    
                    echo '<table width="650"><p><tr><th style="margin-left: 10px; padding-right: 70px;"></th> 
                                                            <th style="width: 350px; ">Item for sale</th> <th>Location</th> <th>Posted</th></tr></table>';
    
    
                            $query1 = "select id from tblads order by priority desc LIMIT 10 OFFSET 1";
                            $result1 = mysql_query($query1);
                            $i1 = 0;
    
    
                            while ($row1 = mysql_fetch_row($result1)) 
                            {   
    
                                $count1 = count($row1);
                                $y1 = 0;
    
                                while ($y1<$count1)
                                {
                                $c_row1 = current($row1);
    
                                $id = $c_row1;
    
    
                                next($row1);
                                $y1 = $y1 + 1;
    
    
    
                                                    //////TABLE     
                                                echo'<style> th{padding:0;}</style> ';
    
    
                                                echo '<table style="  table-layout:fixed; order-collapse: collapse; " width="650">';
                                    echo '<tr> <th> </th> <th style="width:350px;"> </th> <th style=""></th> <th style="width:100px;"></th></tr>';
    
                                                $query = "select image,details, location, date from tblads where id=$id";
                                                $result = mysql_query($query);                                                                                  
                                                if (!$result) {$message = 'ERROR:' . mysql_error();return $message;}
                                                else
                                                {   $i = $i1;
    
    
                                                $i = 0;
                                                while ($row = mysql_fetch_row($result)) 
                                                {                                                                                                                                               
                                                echo '<tr>  ';
                                                $count = count($row);
                                                $y = 0;
                                                while ($y < $count)
                                                {
                                                    $c_row = current($row);                                                                 
                                                    echo '<td style=""><a href="view.php?id='.$id.'">' . $c_row . '</a></td>';
                                                    next($row);
                                                        $y = $y + 1;
                                                }
                                                echo ' </tr>';
                                                $i = $i + 1;                        
                                                }
    
                                                }                                                       
                                    }
    
                                echo'</table>';     //Content TABLE CLOSE
    
    
                            }   
    
                    mysql_free_result($result); mysql_close($con);                                              
                ?>
    

    然后是通过超链接打开的第二个页面view.php:

    <?php
                    $id=$_GET['id'];
    
    
    
    
                $query = "select image, details, location, price, date, id from tblads where id=$id";
                            $result = mysql_query($query);
            if (!$result) 
                {
                    $message = 'ERROR:' . mysql_error();
                    return $message;
                }
                else
                {   $i = 0;
                    echo '<table><p><tr>';
                        while ($i < mysql_num_fields($result))
                        {
                            $meta = mysql_fetch_field($result, $i);
                            echo '<th>' . $meta->name . '</th>';
    
                            $i = $i + 1;
    
                        }
                        echo '<th>View</th></tr>';
    
                        $i = 0;
                        while ($row = mysql_fetch_row($result)) 
                        {
                            echo '<tr>';
                            $count = count($row);
                            $y = 0;
                            while ($y < $count)
                        {
                            $c_row = current($row);
                            echo '<td>' . $c_row . '</td>';
                            next($row);
                            $y = $y + 1;
                        }
                            echo '<td></td> </tr>';
                            $i = $i + 1;
    
                        }
                            echo' </p></table>';
                            mysql_free_result($result);
    
    
                        mysql_close($con); 
    
                        }
    
    
                ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-05
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多