【问题标题】:How can I get a Jquery Mobile table to echo out in PHP?如何让 Jquery Mobile 表在 PHP 中回显?
【发布时间】:2016-08-23 11:23:45
【问题描述】:

我正在尝试在viewsessions.php 文件中回显一个 JQM 表。我已经研究了这个主题并尝试了这个Jquery Mobile Table in PHP code 解决方案,但是页面没有加载我的代码,所以我把它剥离回表格的工作位置。

任何想法如何做到这一点?

viewsessions.php

<?php

$servername = "";
$username = "";
$password = "";

// This code creates a connection to the MySQL database in PHPMyAdmin named 'ibill'
$con=mysqli_connect('localhost','root','cornwall','ibill');

// The connection is then checked, if it fails, an echo is sent back to the page stating a connection error.
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// This is the query that is sent to the ibill database.
$viewsessions = "SELECT typeofactivity, employer, date, time, amount FROM session_details";
$result = $con->query($viewsessions);

if ($result->num_rows > 0) {
    echo "<table><tr>
                    <th>Type of Activity</th>
                    <th>Employer</th>
                    <th>Date</th>
                    <th>Time</th>
                    <th>Amount (GBP)</th>
                </tr>";
// output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr>
                <td>".$row["typeofactivity"]."</td>
                <td>".$row["employer"]."</td>
                <td>".$row["date"]."</td>
                <td>".$row["time"]."</td>
                <td>".$row["amount"]."</td>
            </tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}
$con->close();
?>

我在一个 html 文件中使用多个页面,所以刚刚发布了带有以下相关代码的页面..

ma​​in.php#viewsessions

  <!--**********************************************VIEW SESSIONS PAGE*******************************************************************************-->
  <!--***********************************************************************************************************************************************-->

  <!--********************************HEADER**************************************************************************************-->
  <div data-role="page" id="viewsessions">
      <div data-role="header" data-id="foo1" data-position="fixed">
      <div class='cssmenu'>
        <ul>
           <li class='active'><a href='#home'>Home</a></li>
           <li><a href='#sessionrecord'>Record a Session</a></li>
           <li><a href='#viewsessions'>View Sessions</a></li>
           <li><a href='#invoice'>Create an Invoice</a></li>
        </ul>
      </div>
    </div><!-- /header -->
  <!--********************************HEADER***************************************************************************************-->

  <!--********************************MAIN*****************************************************************************************-->
    <div data-role="main" class="ui-content">
      <p><span class="logout"><a href="logout.php" data-role="button">Sign Out</a></span></p>
      <div class="loginphp">
        <?php
        if (isset($_SESSION['user_session']) and $_SESSION['user_session']!=""){
        echo '<p>Signed in as:&nbsp;<span class="uname">' . $_SESSION['user_session'] . '</span></p>';      }
        else {
        echo 'not working';
        }
        ?>
      </div>



      <img class="mainlogo" src="/projects/ibill_v3/img/ibill logo.png" alt="iBill Logo" width="200" height="152">
          <h1>Recorded Sessions</h1>  
          <section class="maincontent">
             <?php
              include "viewsessions.php";
             ?>
          </section>
    </div>
  <!--********************************MAIN******************************************************************************************-->

  <!--********************************FOOTER****************************************************************************************-->
    <div data-role="footer">
      <footer class="footer">
        <p>awilliams&copy;</p>
      </footer>
    </div>
  </div>
  <!--********************************FOOTER*****************************************************************************************-->

  <!--**********************************************************END OF VIEW SESSIONS PAGE***************************************************************-->
  <!--**************************************************************************************************************************************************-->

【问题讨论】:

  • 您的服务器代码看起来不错。你能把相关的html代码也贴出来吗?
  • 我已更新我的问题以包含相关的 html 代码

标签: php jquery-mobile html-table echo


【解决方案1】:

如果您从服务器更新表,您需要使用( ".selector" ).table( "rebuild" ); 重建它 否则你可能只需要重新初始化表

$( ".selector" ).table({
  defaults: true
});

此外,您需要在表格标签中包含data-role="table" 属性 我认为这可能是你的问题,因为你没有插入它 尝试包含该标签,如果它不起作用,请使用以下代码

if ($result->num_rows > 0) {
    echo '<table data-role="table" class="selector" data-mode="reflow"> <tr>
                    <th>Type of Activity</th>
                    <th>Employer</th>
                    <th>Date</th>
                    <th>Time</th>
                    <th>Amount (GBP)</th>
                </tr>';
// output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr>
                <td>".$row["typeofactivity"]."</td>
                <td>".$row["employer"]."</td>
                <td>".$row["date"]."</td>
                <td>".$row["time"]."</td>
                <td>".$row["amount"]."</td>
            </tr>";
    }
    echo '</table><script> $( ".selector" ).table({
      defaults: true
    });</script>';
} else {
    echo "0 results";
}

【讨论】:

  • 嗨 Hatem Ahmed,我刚刚尝试了这段代码,但没有成功。为了以防万一,我还替换了表格属性上的“ for ”,但它仍然没有用。有什么想法吗?
  • 发送缓冲的HTML,我试试看
  • 解析错误:语法错误,意外 '" ).table({ ' (T_CONSTANT_ENCAPSED_STRING),在 C:\xampp\htdocs\projects\ibill_v3\html\viewsessions 中需要 ',' 或 ';' .php 在第 39 行
  • 更新错字重试
  • " 而不是 ' 在 jquery 重建部分对不起 typi
【解决方案2】:

您的 PHP 代码看起来不错,您的 HTML 也是如此。请注意,如果您的 PHP 代码工作正常,表格将作为 HTML 文档的一部分生成 jQuery Mobile 样式和增强功能之前。请检查您的 PHP 代码是否确实返回了所需的 HTML 代码。这可以通过将代码复制/粘贴到单独的脚本并直接运行来完成。

还可以考虑添加以下检查以获得良好做法。原因是 $con-&gt;query() 可能返回 FALSE,而 $result-&gt;num_rows 需要 mysqli_result 对象。

// This is the query that is sent to the ibill database.
$viewsessions = "SELECT typeofactivity, employer, date, time, amount FROM session_details";
$result = $con->query($viewsessions);

//Check if a result was returned
if($result === FALSE){
    echo mysqli_error($con);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多