Adding the Where Clause to Server_side processing

Adding the Where Clause to Server_side processing

emelianenkoemelianenko Posts: 18Questions: 0Answers: 0
edited August 2011 in General
Hello

Yes, I have seen that question asked and finely answered, yet, because my code doesnt exactly look like his, I am still wrestling.

I want to introduce a Where clause that restricts for examples results where town_shop = "Krakow" but not sure how exactly

It is just about tweaking 2 lines but ...

His code is this and so is the fnServerData function used to add that extra item...

[code]
$(document).ready(function() {
oTable = $('#example').dataTable( {
"fnServerData" : function ( sSource, aoData, fnCallback ) {
// push parameter onto the aoData array.
aoData.push( { "name": "id", "value": id } ); // <--- replace id with your id

// send request to server, use default fnCallback to process returned JSON
$.ajax( {
"dataType": 'json',
"url": sSource,
"data": aoData,
"success": fnCallback
} );
}
} );


[/code]

but mine goes like this:

[code]
$(document).ready(function() {
$('#shops').dataTable( {
"bProcessing": true,
"bServerSide": true,
"bJQueryUI": true,
"sAjaxSource": "datatables/php/server_processing_shops.php"
} );
} );

[/code]

So, I went

[code]

$(document).ready(function() {
$('#shops').dataTable( {


"fnServerData" : function ( sSource, aoData, fnCallback ) {
// push parameter onto the aoData array.
aoData.push( { "name": "town_shop", "value": Krakow } );

$.ajax( {
"dataType": 'json',
"url": sSource,
"data": aoData,
"success": fnCallback
} );
"bProcessing": true,
"bServerSide": true,
"bJQueryUI": true,
"sAjaxSource": "datatables/php/server_processing_shops.php"
} );
} );


[/code]

and then I am completely lost at the server_side processing page

I know it has to go right after this:

[code]

$sWhere = "";
if ( $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    [code]
    if ( isset($_GET['town_shop']) && $_GET['town_shop'] != "" ) {
    $town_shop = mysql_real_escape_string( $_GET['town_shop'] );
    if ($sWhere) $sWhere .= " AND town_shop='$town_shop'";
    else $sWhere = " WHERE town_shop='$town_shop'";
    }
    [/code]

    because town_shop is a string, put it in single quotes in your WHERE clause
  • emelianenkoemelianenko Posts: 18Questions: 0Answers: 0
    Hello


    I appreciate very much your input. Hmm, I am not seeing the "Krakow" word in your code. Town_shop is the column heading and Krakow happens to be in that column at different rows. By including krakow in the Where clause, only those rows having that value should show. I also dont have the isset part you include.

    this is the full stuff I have

    [code]





    $(document).ready(function() {
    $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "datatables/php/server_processing_shops.php",
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    /* Add some extra data to the sender */
    aoData.push( { "name": "town_shop", "value": "Krakow" } );
    $.getJSON( sSource, aoData, function (json) {
    /* Do whatever additional processing you want on the callback, then tell DataTables */
    fnCallback(json)
    } );
    }
    } );
    } );




    [/code]



    ======================= SERVER SIDE SCRIPT ===============


    [code]



    /* Individual column filtering */
    for ( $i=0 ; $i
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    Krakow will be a value that is passed to the server script. it will be INSIDE the variable $town_shop (which we got from $_GET['town_shop'])

    you won't see the word 'Krakow' in the code, but it will be in the program's variable if that is what is sent to the script.
This discussion has been closed.