2016-04-28 62 views
1

我正試圖在viewsessions.php文件中回顯JQM表。我已經研究過這個主題並嘗試了這個Jquery Mobile Table in PHP code解決方案,但是這個頁面並沒有加載我的代碼,所以我已經將它剝離回表格的工作位置。我怎樣才能讓Jquery Mobile表在PHP中回顯?

任何想法如何做到這一點?

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文件,所以剛貼有以下相關的代碼頁..

main.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***************************************************************--> 
    <!--**************************************************************************************************************************************************--> 
+0

您的服務器代碼看起來不錯。你能否發佈相關的html代碼? –

+0

我已更新我的問題,包括相關的html代碼 – asharoo85

回答

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"; 
} 
+0

嗨Hatem艾哈邁德,我只是試過這段代碼,它不起作用。我也在表格屬性中替換了「for」以防萬一,但它仍然無效。任何想法? – asharoo85

+0

發送緩衝的HTML並且我會試一試 –

+0

解析錯誤:語法錯誤,意外的'「).table。 ({'(T_CONSTANT_ENCAPSED_STRING),期待','或';'在第39行C:\ xampp \ htdocs \ projects \ ibill_v3 \ html \ viewsessions.php – asharoo85

1

您的PHP代碼看起來不錯,您的HTML也是如此。請注意,如果您的PHP代碼工作正確,該表格將作爲之前的HTML文檔的一部分生成,然後在之前生成jQuery Mobile樣式和增強。請檢查您的PHP代碼是否實際返回了所需的HTML代碼。這可以通過將代碼複製/粘貼到單獨的腳本並直接運行來完成。

還考慮添加以下檢查的良好做法。原因是$con->query()可能返回FALSE,而$result->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); 
}