fnServerData and aoData.push

fnServerData and aoData.push

asdrenasdren Posts: 20Questions: 0Answers: 0
edited September 2013 in General
Right now I can access my table by visiting mysite.com/index.php?id=111
This will display only the row on my table which has id 111.
But the problem I now have is if i dont enter any id on the url but simply use mysite.com/index.php
then no results are displayed on my table.
What do I need to add to the code below to be able to display entire table when url mysite.com/index.php is used?
[code]
"fnServerData" : function ( sSource, aoData, fnCallback ) {
// push parameter onto the aoData array.
aoData.push( { "name": "id", "value": <?php echo $id; ?> } );

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

My second question is, is it possible to somehow maybe use fnServerData and aoData.push to filter the table to only display results where for example field name "fruits" is empty. filtering by using url mysite.com/index.php?fruit=empty to display all records where field fruit is empty or mysite.com/index.php?fruit=hasvalue to display record where field fruit has any value.

Thanks in advance

Replies

  • allanallan Posts: 63,383Questions: 1Answers: 10,449 Site admin
    Perhaps you could use PHP's `isset()` method to check if the id is available, and if not then set the value to `null` which your server-side script would see and know to not add a filter.

    > My second question is, is it possible to somehow maybe use fnServerData and aoData.push to filter the table to only display results where for example field name "fruits" is empty

    It sounds like you want the filtering to be done at the server, so your PHP script is what will need to do the filtering - perhaps with a `where` condition in the SQL (assuming you are using SQL).

    Allan
  • asdrenasdren Posts: 20Questions: 0Answers: 0
    Hi Allan, thanks for the reply, I added this to my server_processing.php

    [code]// if id is specified, add it to the WHERE clause
    if ( isset($_GET['id']) && $_GET['id'] != "" ) {
    if ($sWhere) $sWhere .= " AND id=".mysql_real_escape_string( $_GET['id'] );
    else $sWhere = " WHERE id=".mysql_real_escape_string( $_GET['id'] );
    } [/code]

    and on my site I now use this isset method:
    [code] if ( isset($_GET['id'])) {
    $id=$_GET['id'];
    }else{
    $id='null';
    } [/code]

    that took care of the id.

    But could you help me out with the second part because I have not managed to get it to work.

    I have added a second aoData.push to my fnServerData like so:

    [code]"fnServerData" : function ( sSource, aoData, fnCallback ) {
    // push parameter onto the aoData array.
    aoData.push( { "name": "id", "value": <?php echo $id; ?> } ),
    aoData.push( { "name": "fruit", "value": 'novalue' } );[/code]

    How would the WHERE clause on server_processing.php look like
    if user enters url mysite.com/index.php?fruit=novalue
    then only display result with rows where fruit column is empty. if mysite.com/index.php is used (no ?fruit=novalue) then display all results.

    and how would the isset method for the fruit field look like on my index.php?


    Thanks in advance
  • allanallan Posts: 63,383Questions: 1Answers: 10,449 Site admin
    Perhaps something like:

    [code]
    aoData.push( { "name": "id", "value": "<?php echo isset( $_GET['fruit'] ) ? $_GET['fruit'] : ''; ?>" } );
    [/code]

    which will result in:


    [code]
    aoData.push( { "name": "id", "value": "{id}" } );
    // or
    aoData.push( { "name": "id", "value": "" } );
    [/code]

    So your server-side processing script would simply check for it being an empty string or not before applying the WHERE condition.

    Allan
This discussion has been closed.