如何在服务器端主动搜索数据表中的内部联接? - php

我对服务器端数据表有一些问题。当数据使用内部联接时,数据表不起作用,但是如果我使用查询select * from table,则此工作有效。它的数据表控制器:

<?php

require_once '../config/config.php'; // Use require. Can not use INCLUDE function
// storing  request (ie, get/post) global array to a variable  
$requestData = $_REQUEST;

$columns = array(
// datatable column index  => database column name
    0 => 'id_hanca',
    1 => 'id_detail_po',
    2 => 'ukuran',
    3 => 'jumlah_hanca',
    4 => 'status_hanca',
    5 => 'id_user',
    6 => 'id_vendor'
);


// getting total number records without any search
$sql = "SELECT * ";
$sql.=" FROM hanca";
$query = $db->query($sql);
$totalData = $query->num_rows;
$totalFiltered = $totalData;  // when there is no search parameter then total number rows = total number filtered rows.

$sql = "SELECT
model.nm_model, hanca.id_hanca, hanca.ukuran, hanca.jumlah_hanca, user.name_usr, vendor.nama_vendor
FROM hanca
INNER JOIN po_detail ON po_detail.id_detail_po = hanca.id_detail_po
INNER JOIN model ON model.id_model = po_detail.id_model
INNER JOIN user ON user.id_usr = hanca.id_user
INNER JOIN vendor ON vendor.id_vendor = hanca.id_vendor WHERE 1=1";
if (!empty($requestData['search']['value'])) {   // if there is a search parameter, $requestData['search']['value'] contains search parameter
    $sql.=" AND ( po_detail LIKE '%" . $requestData['search']['value'] . "%' ";
    $sql.=" OR model LIKE '%" . $requestData['search']['value'] . "%' )";
}
$query = $db->query($sql);
$totalFiltered = $query->num_rows; // when there is a search parameter then we have to modify total number filtered rows as per search result. 
$sql.=" ORDER BY " . $columns[$requestData['order'][0]['column']] . " " . $requestData['order'][0]['dir'] . "  LIMIT " . $requestData['start'] . " ," . $requestData['length'] . "   ";

$query = $db->query($sql);

$data = array();
$no = 1;
foreach ($query as $row) {
     
    $nestedData = array();
    $nestedData[] = $no++;
    $nestedData[] = $row['nm_model'];
    $nestedData[] = $row['ukuran'];
    $nestedData[] = $row['jumlah_hanca'];
    $nestedData[] = $row['name_usr'];
    $nestedData[] = $row['nama_vendor'];
    // Input Hiddden to include value for update cart
    // Add html button for action
    $nestedData[] = "<a href='#' class=\" btn btn-info btn-xs btn-flat\" onClick=\"detailBelanja('$row[id_hanca]');\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"Detail Belanja\"><span class=\"glyphicon glyphicon-search\"></span> Detail</a>";
    $data[] = $nestedData;
}

$json_data = array(
    "draw" => intval($requestData['draw']), // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
    "recordsTotal" => intval($totalData), // total number of records
    "recordsFiltered" => intval($totalFiltered), // total number of records after searching, if there is no searching then totalFiltered = totalData
    "data" => $data   // total data array
);

echo json_encode($json_data);  // send data as json format 

此文本虚拟对象需要更多详细信息
Lorem ipsum dolor坐下,一直保持良好状态。 Curabitur et arcu aliquet,congue metus eu,congue sapien。 Nam suscipit efficitur elit,ac maximus felis tincidunt eu。 Pellentesque居民morbi tristique senectus et netus et malesuada成名ac turpis egestas。 Duis Porttitor risus sed erat dignissim varius

参考方案

试试这个:

HTML和JS:

<html>
    <head>
        <link href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css" />

        <script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
        <script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>

        <script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.1/js/dataTables.buttons.min.js"></script>
        <script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.1/js/buttons.flash.min.js"></script>
        <script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js"></script>
        <script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js"></script>
        <script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.1/js/buttons.html5.min.js"></script>
        <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.2.1/css/buttons.dataTables.min.css">

        <script type="text/javascript" language="javascript" class="init">
        $(document).ready(function() {
            var count = 0;
            $('#data_table').dataTable({
                "sServerMethod": "POST", 
                "bProcessing": true,
                "bServerSide": true,
                "sAjaxSource": "get_data.php",

                dom: 'Bfrtip',
                buttons: ['pdf', 'csv'],
            });

            // To edit
            $(document).on('click', '.edit', function(){
                var id = $(this).attr('id');
                alert(id);
                // Editing code here
            });

            // To delete
            $(document).on('click', '.delete', function(){
                var id = $(this).attr('id');
                if(confirm('Are you sure'))
                {
                    alert('Pending');
                    // deleting code here
                }
            });
        });
        </script>

        <style>
        .odd{
            background-color: #FFF8FB !important;
        }
        .even{
            background-color: #DDEBF8 !important;
        }
        </style>
    </head>
    <body>
    <div>
    <table id="data_table">
        <thead>
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
                <th>Position</th>
                <th>Department</th>
                <th>Expertise</th>
            </tr>
        </thead>

        <tbody>
        <!-- Dynamic Body -->
        </tbody>

    </table>
    </body>
    </div>
</html>

Php:

<?php
mysql_connect("localhost", "root", "root") or die('Connection Error');
mysql_select_db("testing_db") or die("Database Connection Error");

$start  = $_REQUEST['iDisplayStart'];
$length = $_REQUEST['iDisplayLength'];
$sSearch = $_REQUEST['sSearch'];

$col = $_REQUEST['iSortCol_0'];

$arr = array(0 => 't1.first_name',1 => 't1.last_name', 2 => 't1.email');

$sort_by = $arr[$col];
$sort_type = $_REQUEST['sSortDir_0'];

$position_filter = '';
$position = substr($_REQUEST['sSearch_3'], 1, -1);
if($position != '')
{
    $position_filter = "and t1.position LIKE '%".$position."%'";
}

$qry = "select t1.id, t1.first_name, t1.last_name, t1.email, t1.position, t1.office, t2.department, t3.expertise from datatables_demo t1 JOIN datatable_dept t2 ON t1.id = t2.emp_id JOIN datatable_expertise t3 ON t1.id = t3.emp_id where (first_name LIKE '%".$sSearch."%' or last_name LIKE '%".$sSearch."%' or email LIKE '%".$sSearch."%') ".$position_filter." ORDER BY ".$sort_by." ".$sort_type." LIMIT ".$start.", ".$length;    // join defined here

$res = mysql_query($qry);
while($row = mysql_fetch_assoc($res))
{
    $data[] = $row;
}

$qry = "select count(id) as count from datatables_demo";
$res = mysql_query($qry);

while($row =  mysql_fetch_assoc($res))
{
    $iTotal = $row['count'];
}

$rec = array(
    'iTotalRecords' => $iTotal,
    'iTotalDisplayRecords' => $iTotal,
    'aaData' => array()
);

$k=0;
if (isset($data) && is_array($data)) {

    foreach ($data as $item) {
        $rec['aaData'][$k] = array(
            0 => ucwords(strtolower($item['first_name'])),
            1 => ucwords(strtolower($item['last_name'])),
            2 => ucwords(strtolower($item['email'])),
            3 => ucwords(strtolower($item['position'])),
            4 => ucwords(strtolower($item['department'])),
            5 => ucwords(strtolower($item['expertise'])),
        );
        $k++;
    }
}

echo json_encode($rec);
?>

代码未在服务器目录php中创建文件 - php

我正在尝试使用以下代码将新文件写入服务器error_reporting(E_ALL); ini_set('display_errors', 1); if($_SERVER['REQUEST_METHOD'] == "POST") { $html = $_POST['html'];…

CodeIgniter更新查询被执行两次 - php

我正在使用CodeIgniter 2.2。每次访问页面时,我都必须用+1更新数据库。代码可以工作,但是每次都会增加+2。示例:如果是total views=2,则在单击页面后total views应该是3,但是数据库中的值是4。我确定我在控制器中仅调用一次模型add_one_to_view_image。控制者 function view(){ $view_i…

故障排除“警告:session_start():无法发送会话高速缓存限制器-标头已发送” - php

我收到警告:session_start()[function.session-start]:无法发送会话缓存限制器-标头已发送(错误输出开始如果我将表单数据提交到其他文件进行处理,则可以正常工作。但是,如果我将表单数据提交到同一页面,则会出现此错误。请建议<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0…

通过php表单修改我的xml文件 - php

这是我的xml文件和下面的php代码。我输入了一个输入类型,它将按名字搜索学生。然后将显示有关特定学生的信息,并且将显示另一个按钮更新。问题是我想在那之后修改信息。如何通过标签名称获取元素,以便可以修改有关特定学生的信息?<students> <student> <firstname>John</firstname&…

在ajax之后将页面内容复制到同一页面 - php

我有简单的注册公式,我希望首先使用ajax发送,而不刷新页面以控制是否插入正确的数据,然后仅重定向到其他页面。问题是,当我通过ajax将其发送到同一页面后,一切正常,但是页面内容重复,我可以看到两次...这是我的ajaxfunction registruj () { var name = $('#meno').val(); var pri…