2015-12-21 44 views
0

託尼,下面顯示的更新後的代碼,它不會讓我評論這個,所以我編輯了我以前的帖子,下面的代碼,不會返回任何結果或錯誤,它只是重新加載頁面。我可以看到的代碼唯一的問題是,我需要能夠搜索,所有表頭,評級,名稱,區域等我有一個數據表腳本,實時更新表結果,但這並不'用PHP表t工作,我不知道如何我的意思,使其與數據表工作,因爲我以前從未使用過它:實時更新可搜索的數據庫MYSQL和PHP

updated code: 

<html> 
<head> 
<body> 
<style> 
table { 
color: #333; /* Lighten up font color */ 
font-family: Helvetica, Arial, sans-serif; /* Nicer font */ 
width: 100%; 
border-collapse: 
collapse; border-spacing: 0; 
} 

td, th { border: 1px solid #00000; height: 30px; } /* Make cells a bit taller */ 

th { 
background: #F3F3F3; /* Light grey background */ 
font-weight: bold; /* Make sure they're bold */ 
} 

td { 
background: #FAFAFA; /* Lighter grey background */ 
text-align: center; /* Center our text */ 
} 
</style> 

<script type="text/javascript" language="javascript" src="http://wcfcourier.com/app/special/data_tables/media/js/jquery.js"></script> 
<script type="text/javascript" language="javascript" src="http://wcfcourier.com/app/special/data_tables/media/js/jquery.dataTables.min.js"></script> 

<script type="text/javascript" charset="iso-8859-1"> 



$(document).ready(function(){ 
    $('#five_year').dataTable({ 
    "iDisplayLength": 300, 
    "aLengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]] 
    }); 
$(document).ready(function() { 
    // Setup - add a text input to each footer cell 
    $('#five-year tfoot th').each(function() { 
     var title = $('#five_year thead th').eq($(this).index()).text(); 
     $(this).html('<input type="text" placeholder="Search '+title+'" />'); 
    }); 

    // DataTable 
    var table = $('#five_year').DataTable(); 

    // Apply the search 
    table.columns().eq(0).each(function (colIdx) { 
     $('input', table.column(colIdx).footer()).on('keyup change', function() { 
      table 
       .column(colIdx) 
       .search(this.value) 
       .draw(); 
     }); 
    }); 
}); 

}); 


$().ready(function() { 
    var regEx = /(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)/; 

    $("table td").filter(function() { 
     return $(this).html().match(regEx); 
    }).each(function() { 
     $(this).html($(this).html().replace(regEx, "<a href=\"mailto:$1\">$1</a>")); 
    }); 
}); 
</script> 
<form action="index.php" method="post"> 
    <input type="text" name="search" placeholder="Search...." /> 
    <input type="submit" value=">>" /> 
</form> 
<?php 
$con=mysqli_connect("localhost","root","windows11","main"); 
// Check connection 
if (mysqli_connect_errno()) 
{ 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 


$result = mysqli_query($con,"SELECT * FROM `users` ORDER BY ID asc, Name asc") or die(mysqli_error($con)); 

echo 
"<table border=1> 
<tr> 
<th>Rating</th> 
<th>Name</th> 
<th>Discipline</th> 
<th>Rate</th> 
<th>Area</th> 
<th>Number</th> 
<th>Email</th> 
</tr>"; 

while($row = mysqli_fetch_array($result)){ 

echo "<tr>"; 
echo "<td>" . $row['Rating'] . "</td>"; 
echo "<td>" . $row['Name'] . "</td>"; 
echo "<td>" . $row['Discipline'] . "</td>"; 
echo "<td>" . $row['Rate'] . "</td>"; 
echo "<td>" . $row['Area'] . "</td>"; 
echo "<td>" . $row['Number'] . "</td>"; 
echo "<td>" . $row['Email'] . "</td>"; 
echo "</tr>"; 
echo "</form>"; 
} 
// If there is a search variable try to search database 
if(isset($_POST['search'])) { 

       $searchq = $_POST['search']; 
       $searchq = preg_replace("#[^0-9a-z]#i", "", $searchq); 
       $sql = "SELECT * FROM `users` WHERE `Rating` LIKE '%$searchq%';"; 

       if ($result = mysqli_query($conn, $sql)) { 
        if (mysqli_num_rows($result) > 0) { 

         echo ' 
         <table class="hoverTable"> 
          <tr> 
           <th>Rating</th> 
           <th>Name</th> 
           <th>Discipline</th> 
           <th>Rate</th> 
           <th>Area</th> 
           <th>Number</th> 
           <th>Email</th> 
          </tr>'; 

          while($row = $result->fetch_assoc()) { 
           echo " 
           <tr> 
            <td>".$row["rating"]."</td> 
            <td>".$row["name"]."</td> 
            <td>".$row["discipline"]."</td> 
            <td>".$row["rate"]."</td> 
            <td>".$row["area"]."</td> 
            <td>".$row["number"]."</td> 
            <td>".$row["email"]."</td> 
           </tr>"; 
          } 

         echo ' 
         </table>'; 

        } else { 
         $message = "0 results"; 
        } 
       } 
       mysqli_free_result($result); 
      } 
     $conn->close(); 
?> 



</body> 
</head> 
</html> 

這也是數據表腳本與工程HTML表(所有數據拆分到錶行使用HTML不是由PHP腳本回顯出來:

<script type="text/javascript" language="javascript" src="http://wcfcourier.com/app/special/data_tables/media/js/jquery.js"></script> 
<script type="text/javascript" language="javascript" src="http://wcfcourier.com/app/special/data_tables/media/js/jquery.dataTables.min.js"></script> 

<script type="text/javascript" charset="iso-8859-1"> 



$(document).ready(function(){ 
    $('#five_year').dataTable({ 
    "iDisplayLength": 300, 
    "aLengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]] 
    }); 
$(document).ready(function() { 
    // Setup - add a text input to each footer cell 
    $('#five-year tfoot th').each(function() { 
     var title = $('#five_year thead th').eq($(this).index()).text(); 
     $(this).html('<input type="text" placeholder="Search '+title+'" />'); 
    }); 

    // DataTable 
    var table = $('#five_year').DataTable(); 

    // Apply the search 
    table.columns().eq(0).each(function (colIdx) { 
     $('input', table.column(colIdx).footer()).on('keyup change', function() { 
      table 
       .column(colIdx) 
       .search(this.value) 
       .draw(); 
     }); 
    }); 
}); 

}); 


$().ready(function() { 
    var regEx = /(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)/; 

    $("table td").filter(function() { 
     return $(this).html().match(regEx); 
    }).each(function() { 
     $(this).html($(this).html().replace(regEx, "<a href=\"mailto:$1\">$1</a>")); 
    }); 
}); 
</script> 

我可以看到這個數據表的代碼需要一個表ID來引用,但我不能分配一個表ID到PHP代碼,因爲它只是當我嘗試時崩潰的腳本和頁面。

感謝您的幫助。

+0

是的,它是可能的,檢查[數據表](http://datatables.net/ )它有你需要做的所有事情...... – Naruto

+0

儘管取決於數據量,你可能想要爲存儲的數據類型設置一個特定字段的MyISAM表,並鏈接到原始記錄速度。假設您想要異步搜索數百萬條記錄中的任何內容。 – ash

+0

我已經看過數據表,但不能看到如何實現這與我的表.. 在原始的HTML表,我能夠包括一個表id,然後jscript的工作,但以這種方式,如果我在任何地方添加表id,它不起作用,腳本崩潰。 – Mouseman85

回答

0

我在之前的類項目上設置了一個簡單的搜索,在我的數據庫中查詢一個表並返回結果。你可以修改這個,如果你認爲它可能有幫助,試試這個:

把搜索放到表單中。

// If there is a search variable try to search database 

如果(isset($ _ POST [ '搜索'])){

   $searchq = $_POST['search']; 
       $searchq = preg_replace("#[^0-9a-z]#i", "", $searchq); 
       $sql = "SELECT * FROM `Customers` WHERE `Client` LIKE '%$searchq%';"; 

       if ($result = mysqli_query($conn, $sql)) { 
        if (mysqli_num_rows($result) > 0) { 

         echo ' 
         <table class="hoverTable"> 
          <tr> 
           <th>Rating</th> 
           <th>Name</th> 
           <th>Discipline</th> 
           <th>Rate</th> 
           <th>Area</th> 
           <th>Number</th> 
           <th>Email</th> 
          </tr>'; 

          while($row = $result->fetch_assoc()) { 
           echo " 
           <tr> 
            <td>".$row["rating"]."</td> 
            <td>".$row["name"]."</td> 
            <td>".$row["discipline"]."</td> 
            <td>".$row["rate"]."</td> 
            <td>".$row["area"]."</td> 
            <td>".$row["zipcode"]."</td> 
            <td>".$row["email"]."</td> 
           </tr>"; 
          } 

         echo ' 
         </table>'; 

        } else { 
         $message = "0 results"; 
        } 
       } 
       mysqli_free_result($result); 
      } 
     $conn->close(); 

?>

+0

這是否需要搜索成爲新頁面?我需要搜索與表格在同一頁面上,我也很新,如果這是在同一頁面上工作,我需要將它插入到我的代碼中?如果我在<?php代碼中插入任何內容,則會破壞代碼,並且出現錯誤500. – Mouseman85

+0

您可以在同一頁面上運行它。只需將結果回顯到您已設置的表格中即可。 – Tony

+0

HI Tony,在那裏我插入了這段代碼,它只是打破了數據庫代碼,我在底部插入了mysqli_close($ con)行之後,並且導致了500錯誤,我試過把它放在之前,在頂部,在中部,在其標籤,它只是每次導致500錯誤。有什麼建議麼?感謝advace – Mouseman85