0

Hell everyone i have datatables set up right now and i want to convert it over to server-side processing
Link to Script

right now i am using this code which seems to be very inefficient now that the database is getting larger. Would anyone want to help me convert this over to a server sided script like the one in the link above
also i would need to convert it to PDO because its better.

<?php
//connect to database
include 'Model/DBAdapter.php';
//create sql statement
$sql = "SELECT RMA_Number, Person_Calling, Company_Name, Unit_Serial_Number,
    CONCAT(Employee_First_Name,' ',Employee_Last_Name) AS Employee_Name,
    Call_Date,Received_Date,RMA_Status, Reason_For_Return, Notes, New_Install,
    New_Unit_Serial_Number, Terminal_ID, Account_Number, Account_Name FROM RMA
            JOIN Companies ON RMA.Company_ID  = Companies.Company_ID
            JOIN Employees ON RMA.Employee_ID  = Employees.Employee_ID";
//Retrieve data
$result = $connection->query($sql);
//process results
if ($result) {
    //loop through data
    while ($view = $result->fetch()) {
        $views[] = $view;
    }
    //display
    include 'View/View.html.php';
} else {
    //error message
    $output = "No RMA found in database.";
    include 'View/Template.html.php';
}



<body>
        <div id="wrapper">
            <h1 id="tableTitle">RMA's Submitted</h1>
            <table id="table">
                <thead>
                    <tr>
                        <th>RMA Number</th>
                        <th>Person Calling</th>
                        <th>Company ID</th>
                        <th>ISN</th>
                        <th>Employee</th>
                        <th>Call Date</th>    
                        <th>Received Date</th>
                        <th>RMA Status</th>   
                        <th>Reason for Return</th>
                        <th>Notes</th>
                        <th>New Install</th>
                        <th>New Serial Number</th>
                        <th>Terminal ID</th>
                        <th>Account Number</th>
                        <th>Account Name</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($views as $view): ?>
                        <tr>
                            <td><?php echo $view['RMA_Number']; ?></td>
                            <td><?php echo $view['Person_Calling']; ?></td>
                            <td><?php echo $view['Company_Name']; ?></td>
                            <td><?php echo $view['Unit_Serial_Number']; ?></td>
                            <td><?php echo $view['Employee_Name']; ?></td>
                            <td><?php echo $view['Call_Date']; ?></td>
                            <td><?php echo $view['Received_Date']; ?></td>
                            <td><?php echo $view['RMA_Status']; ?></td>
                            <td><?php echo $view['Reason_For_Return']; ?></td>
                            <td><?php echo $view['Notes']; ?></td>
                            <td><?php echo $view['New_Install']; ?></td>
                            <td><?php echo $view['New_Unit_Serial_Number']; ?></td>
                            <td><?php echo $view['Terminal_ID']; ?></td>
                            <td><?php echo $view['Account_Number']; ?></td>
                            <td><?php echo $view['Account_Name']; ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </body>
3
Contributors
5
Replies
39
Views
3 Years
Discussion Span
Last Post by Sijeveux
1

You're using 'Model/DBAdapter.php' as a wrapper/class for your DB manipulation. What does it use? mysql/mysqli/pdo?

The link you provide uses mysql. What exactly do you want to do. I can't really see how that script is any better from the code you've posted. Your snippet looks pretty standard.

Would anyone want to help me convert this over to a server sided script like the one in the link above

It already is a server-side script. So confused...

0

My 'Model/DBAdapter.php' is using PDO, The way im doing it is echoing out the table whcih i beleive is causing a performance decrease because the page itself is getting larger with all the html rows php is echoing out.

0

Ah, OK I see. I'd build up a string and then echo that out, e.g.

$output = '';
while(...){
    $output .= '...';
}
echo $output;

But as you're using PDO, you're creating a multidimensional array from your fetch not a resource, so you could use implode with certain 'glues' to create output, possibly obviating the need for a loop (e.g. for(each) or while).

0

I finally had the time to look over it fully and i completed it with this

<?php

     //connect to database
     include '../../Model/DBAdapter.php';


    $aColumns = array( 'RMA_Number', 'Person_Calling', 'Company_Name', 'Unit_Serial_Number', 'CONCAT(e.Employee_First_Name, " ", e.Employee_Last_Name)',
                        'Call_Date','Received_Date','RMA_Status','Reason_For_Return','Notes','New_Install',
                        'New_Unit_Serial_Number', 'Terminal_ID','Account_Number','Account_Name');

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

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

    //Join to use
   $sJoin = ' JOIN Companies ON RMA.Company_ID  = Companies.Company_ID';
   $sJoin .= ' JOIN Employees e ON RMA.Employee_ID  = e.Employee_ID';


    /*
     * Paging
     */
    $sLimit = "";
    if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
    {
        $sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] ).", ".
            intval( $_GET['iDisplayLength'] );
    }


    /*
     * Ordering
     */
    $sOrder = "";
    if ( isset( $_GET['iSortCol_0'] ) )
    {
        $sOrder = "ORDER BY  ";
        for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
        {
            if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
            {
                $sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
                    ".($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
            }
        }

        $sOrder = substr_replace( $sOrder, "", -2 );
        if ( $sOrder == "ORDER BY" )
        {
            $sOrder = "";
        }
    }


    /*
     * Filtering
     * NOTE this does not match the built-in DataTables filtering which does it
     * word by word on any field. It's possible to do here, but concerned about efficiency
     * on very large tables, and MySQL's regex functionality is very limited
     */
    $sWhere = "";
    if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
    {
        $sWhere = "WHERE (";
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" )
            {
                $sWhere .= $aColumns[$i]." LIKE '%".  htmlspecialchars( $_GET['sSearch'] )."%' OR ";
            }
        }
        $sWhere = substr_replace( $sWhere, "", -3 );
        $sWhere .= ')';
    }

    /* Individual column filtering */
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
        {
            if ( $sWhere == "" )
            {
                $sWhere = "WHERE ";
            }
            else
            {
                $sWhere .= " AND ";
            }
            $sWhere .= $aColumns[$i]." LIKE '%".  htmlspecialchars($_GET['sSearch_'.$i])."%' ";
        }
    }


    /*
     * SQL queries
     * Get data to display
     */
    $sQuery = "
        SELECT SQL_CALC_FOUND_ROWS " . str_replace(" , ", " ", implode(", ", $aColumns)) . "
        FROM   $sTable
        $sJoin
        $sWhere
        $sOrder
        $sLimit
    ";
try {
    $rResult = $connection->query($sQuery);
} catch (PDOException $e) {
    $error = 'Error getting data: ' . $e->getMessage();
    echo $error;
    exit();
}

/* Data set length after filtering */
    $sQuery = "
        SELECT FOUND_ROWS()
    ";
    try{
    $rResultFilterTotal = $connection->query($sQuery);
    $aResultFilterTotal = $rResultFilterTotal->fetch();
    $iFilteredTotal = $aResultFilterTotal[0];
    }catch (PDOException $e) {
    $error = 'Error getting found rows: ' . $e->getMessage();
    echo $error;
    exit();
}
    /* Total data set length */
    $sQuery = "
        SELECT COUNT(".$sIndexColumn.")
        FROM   $sTable
    ";
    $rResultTotal = $connection->query($sQuery);
    $aResultTotal = $rResultTotal->fetch();
    $iTotal = $aResultTotal[0];


    /*
     * Output
     */
    $output = array(
        "sEcho" => intval($_GET['sEcho']),
        "iTotalRecords" => $iTotal,
        "iTotalDisplayRecords" => $iFilteredTotal,
        "aaData" => array()
    );

    while ( $aRow =  $rResult->fetch())
    {
        $row = array();
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        { 
             if (!empty($aColumns[$i]) )
            {
                /* General output */
                $row[] = $aRow[ $aColumns[$i] ];
            }
        }
        $output['aaData'][] = $row;
    }

    echo json_encode($output);
0

Hi,
I'm implementing your code, which sounds ok, and thanks for sharing, but I get
Notice: Undefined index: sEcho on line 143 !!!
Can you please help me ?!!!!! I've spent over a week on datatables......
Many anticipate thanks

Edited by Sijeveux

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.