2014-09-05 7 views
13

का उपयोग कर 'WHERE' खंड का उपयोग कैसे करें ठीक है, इसलिए मैं jQuery डेटाटेबल (DataTables.net) का उपयोग करके अपने डेटाबेस से जानकारी प्रदर्शित करने की कोशिश कर रहा हूं। मैं इसे पूरी तालिका 'नोट्स' को प्रदर्शित करने के लिए ठीक काम करने के लिए प्राप्त कर सकता हूं, लेकिन मैं सिर्फ उन नोट्स को प्रदर्शित करना चाहता हूं जिन्हें पढ़ा नहीं गया है। तो मुझे किसी भी तरह के WHERE खंड को शामिल करने की आवश्यकता है, लेकिन मैं इसके बारे में जाने का सबसे अच्छा तरीका अस्पष्ट हूं।ssp.class.php डेटाटेबल्स

यहाँ कैसे मैं वर्तमान में पूरे तालिका प्रदर्शित है:

// DB table to use 
$table = 'Notes'; 

// Table's primary key 
$primaryKey = 'CID'; 

// Array of database columns which should be read and sent back to DataTables. 
// The `db` parameter represents the column name in the database, while the `dt` 
// parameter represents the DataTables column identifier. In this case simple 
// indexes 
$columns = array(
array('db' => 'CID', 'dt' => 0), 

array(
    'db'  => 'CID', 
    'dt'  => 0, 
    'formatter' => function($d, $row) { 
     return '<a href="profile.php?search='.$d.'" target="_Blank">'.$d."</a>"; 
    } 
), 

array('db' => 'Title', 'dt' => 1), 
array('db' => 'Name', 'dt' => 2), 
array(
    'db'  => 'Date', 
    'dt'  => 3, 
    'formatter' => function($d, $row) { 
     return date('jS M y', strtotime($d)); 
     } 
    ) 
); 

// SQL server connection information 
$sql_details = array(
'user' => '*DB_USER*', 
'pass' => '*Password*', 
'db' => '*DatabaseName*', 
'host' => 'localhost' 
); 
require('ssp.class.php'); 

echo json_encode(
    SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns) 
); 

मैं SELECT * FROM Notes WHERE Status ='Unread'

+0

क्या आप इस लिंक को पढ़ते हैं? https://datatables.net/forums/discussion/20953/where-clause-using-ssp-class-php-solved। उम्मीद है कि काम करते हैं। –

उत्तर

25

आप परिवर्तन करना होगा DataTables इस क्रिया को करने के लिए डिफ़ॉल्ट कार्य!

उपयोग इस ssp.class.php अनुकूलित वर्ग

Link

कैसे उपयोग करने के लिए?

इस उदाहरण की तरह उपयोग करें:

require('ssp.class.php'); 
$where = "Status ='Unread'"; 
echo json_encode(
    SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns,$where) 
); 

यदि आप पैरामीटर $ सेट जहां, इस वर्ग को जोड़ने जहां बयान को चुनने के लिए!

अद्यतन:

2015 में

DataTables जोड़ने जटिल विधि

इस नई विधि क्वेरी में जहां खंड सेट कर सकते हैं!

+2

आपके कोड में कोई त्रुटि है, यह होना चाहिए: एसएसपी :: कॉम्प्लेक्स ($ _GET, $ sql_details, $ table, $ primaryKey, $ कॉलम, $ जहां) जटिल विधि वह है जिसने समर्थन जोड़ा जहां एसएसपी का उपयोग करते हुए खंड: सरल आपके द्वारा अभी जोड़े गए प्रश्न को अनदेखा कर देगा। मैंने आपका जवाब संपादित कर लिया है। दस्तावेज़ीकरण की जांच करें: https://github.com/DataTables/DataTablesSrc/blob/master/examples/server_side/scripts/ssp.class.php#L276 –

+0

पहले उत्तर पढ़ें! मैं एक कस्टम क्लास बनाता हूं और इसका उपयोग करता हूं (जिथब लिंक देखें), जैसा कि मैंने बताया है कि 2014 में कोई जटिल विधि नहीं है, इसलिए मैंने उस वर्ग को लिखा है! –

+0

तब मेरा बुरा! इसके बारे में खेद है कि मैंने सोचा था कि एक टाइपो था, यह वास्तव में बल्कि भ्रमित था। शायद आप एक अच्छा विचार होगा यदि आप अद्यतन अनुच्छेद के तहत जटिल कोड जोड़ते हैं, तो शायद भ्रम को साफ़ कर देगा। –

2

के बराबर की आवश्यकता होगी आप उपयोग कर सकते हैं जहां इस तरह खंड;

$data = SSP::sql_exec($db, $bindings, 
    "SELECT SQL_CALC_FOUND_ROWS ".implode(", ", SSP::pluck($columns, 'db'))." 
    FROM $table where Status = 'Unread' // <--where clause here 
    $where 
    $order 
    $limit" 
); 
+1

'echo json_encode ( एसएसपी :: सरल ($ _GET, $ sql_details, $ table, $ primaryKey, $ कॉलम) ) के साथ इसका उपयोग कैसे करें; क्योंकि मुझे json_encode – NewUser

8

अच्छा .. आप SSP को संपादित या विस्तारित नहीं कर सकते हैं। यह कॉपी किया कोड के बहुत से बहुत बुरा शैली है, लेकिन SSP बेहतर अनुकूलन की अनुमति नहीं है ...

class SSPCustom extends SSP 
{ 
    /** 
    * @param array $request Data sent to server by DataTables 
    * @param array $sql_details SQL connection details - see sql_connect() 
    * @param string $table SQL table to query 
    * @param string $primaryKey Primary key of the table 
    * @param array $columns Column information array 
    * @param string $whereCustom Custom (additional) WHERE clause 
    * @return array   Server-side processing response array 
    */ 
    static function simpleCustom ($request, $sql_details, $table, $primaryKey, $columns, $whereCustom = '') 
    { 
     $bindings = array(); 
     $db = self::sql_connect($sql_details); 

     // Build the SQL query string from the request 
     $limit = self::limit($request, $columns); 
     $order = self::order($request, $columns); 
     $where = self::filter($request, $columns, $bindings); 

     if ($whereCustom) { 
      if ($where) { 
       $where .= ' AND ' . $whereCustom; 
      } else { 
       $where .= 'WHERE ' . $whereCustom; 
      } 
     } 

     // Main query to actually get the data 
     $data = self::sql_exec($db, $bindings, 
      "SELECT SQL_CALC_FOUND_ROWS `".implode("`, `", self::pluck($columns, 'db'))."` 
      FROM `$table` 
      $where 
      $order 
      $limit" 
     ); 

     // Data set length after filtering 
     $resFilterLength = self::sql_exec($db, 
      "SELECT FOUND_ROWS()" 
     ); 
     $recordsFiltered = $resFilterLength[0][0]; 

     // Total data set length 
     $resTotalLength = self::sql_exec($db, 
      "SELECT COUNT(`{$primaryKey}`) 
      FROM `$table` 
      WHERE " . $whereCustom 
     ); 
     $recordsTotal = $resTotalLength[0][0]; 


     /* 
     * Output 
     */ 
     return array(
      "draw"   => intval($request['draw']), 
      "recordsTotal" => intval($recordsTotal), 
      "recordsFiltered" => intval($recordsFiltered), 
      "data"   => self::data_output($columns, $data) 
     ); 
    } 
} 

यह कॉल के साथ:

echo json_encode(
    SSPCustom::simpleCustom($_GET, $sql_details, $table, $primaryKey, $columns, "Status ='Unread'") 
); 

Untested

+0

धन्यवाद वाला डेटा मिल रहा है! यह मेरे लिए काम किया। –

0

मैं भी इस समस्या को हल करने में सक्षम था लेकिन ssp.class.php फ़िल्टर फ़ंक्शन में कुछ कोड डालने में सक्षम था। नीचे एक उदाहरण कस्टम के साथ समारोह के लिए लिस्टिंग है जहां खंड डाला गया। उस वर्ग से "सरल" फ़ंक्शन बिना किसी जूरी-रिगिंग के काम करेगा। लाभ यह है कि यह डेटाटेबल की टेक्स्ट-सर्च फीचर के साथ अच्छा खेलेंगे।

static function filter ($request, $columns, &$bindings) 
{ 
    $globalSearch = array(); 
    $columnSearch = array(); 
    $dtColumns = self::pluck($columns, 'dt'); 

    if (isset($request['search']) && $request['search']['value'] != '') { 
     $str = $request['search']['value']; 

     for ($i=0, $ien=count($request['columns']) ; $i<$ien ; $i++) { 
      $requestColumn = $request['columns'][$i]; 
      $columnIdx = array_search($requestColumn['data'], $dtColumns); 
      $column = $columns[ $columnIdx ]; 

      if ($requestColumn['searchable'] == 'true') { 
       $binding = self::bind($bindings, '%'.$str.'%', PDO::PARAM_STR); 
       $globalSearch[] = "`".$column['db']."` LIKE ".$binding; 
      } 
     } 
    } 

    // Individual column filtering 
    for ($i=0, $ien=count($request['columns']) ; $i<$ien ; $i++) { 
     $requestColumn = $request['columns'][$i]; 
     $columnIdx = array_search($requestColumn['data'], $dtColumns); 
     $column = $columns[ $columnIdx ]; 

     $str = $requestColumn['search']['value']; 

     if ($requestColumn['searchable'] == 'true' && 
     $str != '') { 
      $binding = self::bind($bindings, '%'.$str.'%', PDO::PARAM_STR); 
      $columnSearch[] = "`".$column['db']."` LIKE ".$binding; 
     } 
    } 

    // Combine the filters into a single string 
    $where = ''; 

    if (count($globalSearch)) { 
     $where = '('.implode(' OR ', $globalSearch).')'; 
    } 

    if (count($columnSearch)) { 
     $where = $where === '' ? 
      implode(' AND ', $columnSearch) : 
      $where .' AND '. implode(' AND ', $columnSearch); 
    } 

     //------------------------------------------------------------ 
     //############################################################ 
     //EXAMPLE ADDITIONAL WHERE CONDITIONS HERE. THIS IS EQUIVALENT 
     //TO "WHERE id = 1" 
     $where = ($where === '') ? 
      "id = ".self::bind($bindings, 1, PDO::PARAM_INT) : 
      $where ." AND "."id = ".self::bind($bindings, 1, PDO::PARAM_INT); 
     //############################################################ 
     //############################################################ 
     //------------------------------------------------------------ 


    if ($where !== '') { 
     $where = 'WHERE '.$where; 
    } 

    return $where; 
} 
संबंधित मुद्दे