2014-05-15 77 views
1

我在數據庫(MySql)中有兩個表,一個是國家名稱,另一個是用戶。現在,當一些用戶被添加到他/她選擇的網站時,他/她是很好的。但是,由於用戶數據庫增長到2000個用戶,我需要強制更改爲服務器端數據表,以顯示所有用戶腳本強制凍結屏幕或網站。現在我看不到每個用戶所在國家的名稱,只有ID ......所以我的問題需要添加或更改以查看國家名稱而不是ID。顯示名稱行而不是DataTable服務器端的ID

我遵循這些帖子:

  1. how-relating-two-tables-to-show-name-row-and-not-the-id-in-mysqlpdo
  2. server-side

這裏是我的數據庫:

|----------USERS-------------| 
|---id-----------int---------| 
|---nombres---varchar(150)---| is the user name 
|---fnac-------datetime------| is the birthday 
|---direccion----text--------| is the address 
|---paisid--------int(3)-----| is the country id 
|---foto-------varchar(50)---| is the user photo 
|----------------------------| 

|-----------PAISES-----------| 
|---id---------int(11)-------| is the country id 
|---paises---varchar(100)----| is the country name 
|----------------------------| 

這裏我的代碼(現在我不使用它因爲我不知道我需要插入它的位置):

<?php $query = "SELECT U.paisid, P.pais, 
(SELECT pais FROM PAISES WHERE id = paisid) 
AS pais 
FROM USERS U 
INNER JOIN PAISES P 
ON U.paisid = P.id"; 
$stmt = $conn->prepare($query); 
$stmt->execute(); 
$rows = $stmt->fetchAll(); 
foreach($rows as $row) 
{ ?><?php echo $row['pais']; ?><?php } ?> 

的JS:(其中I改變示例數據部分)

$('#userTabla').dataTable({ 
    "bProcessing": true, 
    "bServerSide": true, 
    "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span12'i><'span12 center'p>>", 
    "sAjaxSource": "includes/server_process.php", 
    "aoColumns": [ 
     { "mData": "id" }, 
     { "mData": "nombres" }, 
     { "mData": "paisid" }, 
     { 
      "mData": null, 
      "sClass": "center", 
      "sDefaultContent": "", 
      "fnRender": function (o) { 
      return '<a href="perfil.php?id=' + o.aData[0] + '" class="btn btn-success"><i class="icon-user icon-white"></i> Ver perfil</a> <a href="editar.php?id=' + o.aData[0] + '" class="btn btn-info"><i class="icon-edit icon-white"></i> Editar</a> <a id="' + o.aData[0] + '" class="btn btn-danger" href="#"><i class="icon-trash icon-white"></i> Borrar</a>' 
     }, 
     "aTargets": [3] 
     } 
    ], 
    "sPaginationType": "bootstrap", 
    "oLanguage": { 
    "sLengthMenu": "_MENU_ registros por pag" 
    } 
}); 

的server_process.php

$aColumns = array('id', 'nombres', 'paisid'); 

/* Indexed column (used for fast and accurate table cardinality) */ 
$sIndexColumn = "id"; 

/* DB table to use */ 
$sTable = "USERS"; 

/* Database connection information */ 
$gaSql['user']  = "dbuser"; 
$gaSql['password'] = "XXXXXX"; 
$gaSql['db']   = "dbmega"; 
$gaSql['server']  = "localhost"; 

回答

0
<?php 
$query = "SELECT USERS.* , PAISES.`paises` as countryName 
FROM USERS 
INNER JOIN PAISES 
ON paisid = id"; 

$stmt = $conn->prepare($query); 
$stmt->execute(); 
$rows = $stmt->fetchAll(); 

$returnArray = array(); 
foreach($rows as $row) 
{ 
returnArray[] = array($row['id'],$row['nombres'],$row['countryName']); 
} 

請變更paisid類型爲int

var myDataTable = $('#userTabla').dataTable({ 
    "bProcessing": true, 
    "bServerSide": true, 
    "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span12'i><'span12 center'p>>", 
    "sAjaxSource": "includes/server_process.php", 
    "aoColumns": [ 
     { "mData": "id" }, 
     { "mData": "nombres" }, 
     { "mData": "paisid" }, 
     { 
      "mData": null, 
      "sClass": "center", 
      "sDefaultContent": "", 
      "fnRender": function (o) { 
      return '<a href="perfil.php?id=' + o.aData[0] + '" class="btn btn-success"><i class="icon-user icon-white"></i> Ver perfil</a> <a href="editar.php?id=' + o.aData[0] + '" class="btn btn-info"><i class="icon-edit icon-white"></i> Editar</a> <a id="' + o.aData[0] + '" class="btn btn-danger" href="#"><i class="icon-trash icon-white"></i> Borrar</a>' 
     }, 
     "aTargets": [3] 
     } 
    ], 
    "sPaginationType": "bootstrap", 
    "oLanguage": { 
    "sLengthMenu": "_MENU_ registros por pag" 
    } 
}); 

myDataTable.fnAddData(<? echo json_encode(returnArray[]); ?>); 
+0

嗨,@ volkinc是的,這是寫錯誤,已經是int(我現在將編輯它的表)...但我怎麼能顯示國家的名稱,而不是id當dataTable服務器端顯示所有表中的用戶? – user3236149

+0

嗨。數據不足。什麼是o.aData [0]?什麼是perfil.php? – volkinc

+0

我更新了答案,請看看 – volkinc

相關問題