我找到了一种连接到我的数据库并检索数据的方法,但是当我尝试隐藏/显示我的表格列时,它似乎不起作用,我什至不知道 Ajax 是否可行。
我的HTML:
<table class="footable" id="masterChart">
<thead><tr><th data-class="expand" class="account">Account Number</th><th>Account Description</th><th>Level 01</th><th>Level 02</th><th>Level 03</th><th>Level 04</th><th>Tax</th><th>YTD - Current</th><th>YTD - Prior</th><th>MTD - Current</th><th>MTD - Prior</th></tr></thead>
<?php $index = 0?>
<?php while ($row = mysql_fetch_assoc($result)):?>
<tr<?php echo $index++ % 2 ? ' class="even"' : ''?>>
<td><?php echo $row['accountNumber']?></td>
<td><?php echo $row['accountDescription']?></td>
<td><?php echo $row['accountLevel1']?></td>
<td><?php echo $row['accountLevel2']?></td>
<td><?php echo $row['accountLevel3']?></td>
<td><?php echo $row['accountLevel4']?></td>
<td><?php echo $row['id']?></td>
<td><?php echo $row['id']?></td>
<td><?php echo $row['id']?></td>
<td><?php echo $row['id']?></td>
<td><?php echo $row['id']?></td>
</tr>
<?php endwhile?>
</table>
我的 dbconn.php:
<?php
include_once('../classes/profile.class.php');
$host = "localhost";
$user = "root";
$pass = "root";
$databaseName = "accounting";
$tableName = "login_table_display";
//--------------------------------------------------------------------------
// 1) Connect to mysql database
//--------------------------------------------------------------------------
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);
//--------------------------------------------------------------------------
// 2) Query database for data
//--------------------------------------------------------------------------
$user = $profile->getField('user_id');
$result = mysql_query("SELECT * FROM '$tableName' WHERE user = '$user'"); //query
$array = mysql_fetch_row($result);
echo json_encode($array);
?>
我使用 profile.class.php 来获取 user_id。
我的阿贾克斯:
$(function ()
{
//-----------------------------------------------------------------------
// 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
//-----------------------------------------------------------------------
$.ajax({
url: 'dbconn.php', //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php
//for example "id=5&parent=6"
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var user = data[1]; //get id
var table = data[2]; //get table name
var column = data[3]; //get column
var show = data[4]; //display or hide
//--------------------------------------------------------------------
// 3) Update html content
//--------------------------------------------------------------------
if (show == 0){
$(table +'td:nth-child('+ column +'),th:nth-child('+ column +')').hide();
//recommend reading up on jquery selectors they are awesome
// http://api.jquery.com/category/selectors/
}
}
});
});
我将上面的 ajax.js 文件包含到我的文件中:
<script src="ajax/ajax.js" type="text/javascript"></script>
如果有人可以提供帮助或帮助,我将不胜感激!如果有人有另一种获取数据库信息并为用户隐藏特定列、表的替代方法,我也将不胜感激。