【问题标题】:Load content of PHP echoed link in div on same page在同一页面的 div 中加载 PHP 回显链接的内容
【发布时间】:2017-12-10 15:03:08
【问题描述】:

我的数据库中有一个表中的SELECT 数据。这些数据带有链接。我想要的是,当我单击链接时,它会在 div 中加载更多数据,而不是转到不同的页面。每个链接都带有一个id,从中可以知道要从数据库中加载div的数据。下面是代码

<div class="pricepop"></div>
<div class="pricetab">
<table class="table">
                <thead>
                    <tr>
                      <th>COMMODITIES</th>
                      <th>UNITS</th>
                      <th>INTERNATIONAL PRICE($)</th>
                      <th>TERMS</th>
                      <th>LOCAL PRICE (&#8358;)</th>
                      <th>AS AT</th>
                      <th>MARKET</th>
                      <th>Full Details</th>
                    </tr>
                </thead>
                <tbody>
                <style>
                .table > thead > tr > th, .table > tbody > tr > td {
                    font-size: 10px !important;
                    font-family: 'Lato', sans-serif;
                    font-weight: bold;
                }
                </style>
                  <?php
                  $sql = $db->prepare("SELECT * FROM commodityprices");
                  $result = $sql->execute();
                  while ($row = $result->fetchArray(SQLITE3_ASSOC))
                  {
                      $id = $row['id'];
                      $_SESSION['id'] = $id;
                      $name = $row['name'];
                      $unit = $row['unit'];
                      $iprice = $row['iprice'];
                      $lprice = $row['lprice'];
                      $irate = $row['irate'];
                      $lrate = $row['lrate'];
                      $terms = $row['cterms'];
                      $asat = date('d/M/Y');
                      $market = $row['market'];

                          echo '<tr>
                          <td>'.$name.'</td>
                          <td>'.$unit.'</td>';
                          if ($irate == "Down")
                          {
                              echo '
                              <td style="background: #ef5a66; color: #fff"><i class="fa fa-caret-down"> </i> '.$iprice.'</td>';
                          }
                          else
                          {
                            echo '
                            <td style="background: #28bc88; color: #fff"><i class="fa fa-caret-up"> </i> '.$iprice.'</td>';
                          }
                          echo '<td>'.$terms.'</td>';
                          if ($lrate == "Down")
                          {
                              echo '
                              <td style="background: #ef5a66; color: #fff"><i class="fa fa-caret-down"> </i> '.$lprice.'</td>';
                          }
                          else
                          {
                            echo '
                            <td style="background: #28bc88; color: #fff"><i class="fa fa-caret-up"> </i> '.$lprice.'</td>';
                          }
                          echo '<td>'.$asat.'</td>
                          <td>'.$market.'</td>
                          <td><a class="comprice" href="pricedetails.php?id='.$id.'">View more</a></td>
                          </tr>';
                  }                 
                  ?>
                </tbody>
                </table>
</div>

下面我有一个javascript,它在点击链接时将数据库中的数据加载到.pricepop

$(document).ready(function() {
    $(".comprice").on("click", function (e) {
        e.preventDefault();
        $(".pricepop").load("pricedetails.php");
    });
});

下面是获取剩余价格详情的priceetails.php代码。

                  <thead>
                    <tr>
                      <th>COMMODITIES</th>
                      <th>TERMS</th>
                      <th>LOCAL PRICE (&#8358;)</th>
                      <th>AS AT</th>
                      <th>MARKET</th>
                      <th>Price/bag</th>
                    </tr>
                </thead>
                <tbody>
                <style>
                .table > thead > tr > th, .table > tbody > tr > td {
                    font-size: 10px !important;
                    font-family: 'Lato', sans-serif;
                    font-weight: bold;
                }
                </style>
                  <?php
                  $priceId = $_SESSION['id'];
                  $sql = $db->prepare("SELECT * FROM commodityprices WHERE id = ?");
                  $sql->bindParam(1, $priceId, SQLITE3_INTEGER);
                  $result = $sql->execute();
                  while ($row = $result->fetchArray(SQLITE3_ASSOC))
                  {
                      $id = $row['id'];
                      $name = $row['name'];
                      $iprice = $row['iprice'];
                      $lprice = $row['lprice'];
                      $lrate = $row['lrate'];
                      $terms = $row['cterms'];
                      $asat = date('d/M/Y');
                      $market = $row['market'];
                      $priceperbags = $row['priceperbags'];

                          echo '<tr>
                          <td>'.$name.'</td>';
                          echo '<td>'.$terms.'</td>';
                          if ($lrate == "Down")
                          {
                              echo '
                              <td style="background: #ef5a66; color: #fff"><i class="fa fa-caret-down"> </i> '.$lprice.'</td>';
                          }
                          else
                          {
                            echo '
                            <td style="background: #28bc88; color: #fff"><i class="fa fa-caret-up"> </i> '.$lprice.'</td>';
                          }
                          echo '<td>'.$asat.'</td>
                          <td>'.$market.'</td>
                          <td class="comprice">'.$priceperbags.'</td>
                          </tr>';
                  }
                  ?>
                </tbody>
                </table> 
</div>

现在的问题是,无论我从回显的php 数据中单击哪个链接,.pricepop 中显示的数据都是最后一个echo 链接的详细信息。

我该如何解决这个问题,当我点击一个链接时,从该链接的id 获得的详细信息是echo 并显示在.pricepop 中。

【问题讨论】:

    标签: javascript jquery html css sql


    【解决方案1】:

    你必须使用 AJAX POST 或 GET statemant 来获得你需要的东西。简单的 jQuery .load() 帮不了你。

    基本上,您必须进行某种分页来加载或附加新数据。

    这里是例子:

    http://itsolutionstuff.com/post/php-infinite-scroll-pagination-using-jquery-ajax-exampleexample.html http://phppot.com/php/ajax-pagination-with-php/

    【讨论】:

    • 谢谢,无限滚动如何解决这个问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-14
    • 2011-05-05
    相关资源
    最近更新 更多