Simple guide

Simple guide

akmalkhonakmalkhon Posts: 15Questions: 0Answers: 0
edited January 2010 in General
Hello everyone
I have a simple question
I want to make a table of 9 columns with hidden details
1. I want to use search that is in the datatables, because server side search looks for only one word.
2. when i make Json output from php, i make a simple echo that passes json data from sql (JSONLint shows tha it is correct json)
Firefox shows correct, but ie doesn't want to show the table and gives error "No matching records found"

I want to ask what is the best way to make it
i spent 3 weeks to get it to work in IE but can't figure out how to do.

thank you.


MY PHP code is
[code]

<?php
/* MySQL connection */
include( "mysql.php" );

$gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
die( 'Could not open connection to server' );

mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
die( 'Could not select database '. $gaSql['db'] );

/* Paging */
$sQuery = "
SELECT *
FROM mindflash
WHERE MayBeListed NOT LIKE 'No'
AND WishBeListed NOT LIKE 'No'
AND GivePermission NOT LIKE 'No'
AND BasicStatus LIKE 'Basic'
AND AdvancedStatus LIKE 'Advanced'
";

$rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());

$sQuery = "
SELECT FOUND_ROWS()
";

$rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];

$sOutput = '{';
$sOutput .= '"aaData": [ ';
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$sOutput .= "[";
$sOutput .= '"'.addslashes($aRow['ID']).'",';
$sOutput .= '"'.addslashes($aRow['FirstName']).' '.addslashes($aRow['LastName']).'",';
$sOutput .= '"'.addslashes($aRow['Organization']).'",';
$sOutput .= '"'.addslashes($aRow['Address1']).' '.addslashes($aRow['Address2']).'",';
$sOutput .= '"'.addslashes($aRow['Country']).'",';
$sOutput .= '"'.addslashes($aRow['Region']).'",';
$sOutput .= '"'.addslashes($aRow['Telephone']).'",';
$sOutput .= '"'.addslashes($aRow['Email']).'",';
$sOutput .= '"'.addslashes($aRow['BasicStatus']).'"';
$sOutput .= "],";
}
$sOutput = substr_replace( $sOutput, "", -1 );
$sOutput .= '] }';

echo $sOutput;
[/code]


and html code
[code]
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": false,
"sAjaxSource": "server_post.php",
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
} );
}
} );

[/code]


Thank you for your help ;)

Replies

  • allanallan Posts: 63,695Questions: 1Answers: 10,500 Site admin
    Three weeks trying to get it to work in IE - ouch!

    Your PHP looks fine on a quick scan, so perhaps try the following:

    1. http://jsonlint.com - validate your return
    2. Use POST rather than GET - http://datatables.net/examples/server_side/post.html (fnServerData works for Ajax data source in 1.6.0 as well as server-side)
    3. Any JS errors or rendering issues?

    Regards,
    Allan
  • akmalkhonakmalkhon Posts: 15Questions: 0Answers: 0
    Hello Allan
    First of all thank you for your efforts

    Coming to POST/GET i don't know i thought i am using POST.
    I am copying the same php code and editing for my table structure.

    JSON gives no mistake
    [code]
    {
    "aaData": [
    [
    "ID",
    "Name ",
    "Address",
    "Street",
    "Country",
    "Region",
    "Phone",
    "email",
    "Certified"
    ] ,
    [
    "15182",
    "Omar Munaf Ismael",
    "Iraq Sat",
    "Iraq Baghdad ",
    "Iraq",
    "Asia",
    "00964 770 896 1018",
    "omar@iraq-sat.net",
    "Basic"
    ]
    ]
    }
    [/code]
  • akmalkhonakmalkhon Posts: 15Questions: 0Answers: 0
    edited January 2010
    By the way how to make server side search like in the regular search in datatables where you can search for two seperate strings. It is so user friendly.
  • akmalkhonakmalkhon Posts: 15Questions: 0Answers: 0
    whenever i change

    "bServerSide" to false,
    "sEcho" becomes 0,

    [code] {"sEcho": 0, "iTotalRecords": 551, "iTotalDisplayRecords": 1459, [/code]

    but if "bServerSide" : true,

    "sEcho" becomes 1,

    [code] {"sEcho": 1, "iTotalRecords": 551, "iTotalDisplayRecords": 1459, [/code]
  • allanallan Posts: 63,695Questions: 1Answers: 10,500 Site admin
    Re: using POST: Use fnServerData as I noted before ( http://datatables.net/examples/server_side/post.html ), but don't specify server-side processing, if you don't want it.

    Re: sEcho: sEcho is only used for server-side processing. This means that all the sorting / filtering etc is done by a script on the server, rather than in the browser. These two examples show the difference:
    http://datatables.net/examples/data_sources/server_side.html
    http://datatables.net/examples/data_sources/ajax.html

    Re: Two search strings: DataTables does this internally by breaking the search string up with a regular expression. My demo PHP code doesn't do this for the server-side processing because RexEx is not easy to work with in MySQL and can be quite an expensive operation, but it is certainly possible to do - the code just needs to be updated to take account of this.

    Regards,
    Allan
  • akmalkhonakmalkhon Posts: 15Questions: 0Answers: 0
    edited January 2010
    OK. Thank you very much for your help.

    I have figured out that it was only character encoding problem. It was not showing because of the unknown characters. But after using the code that was in the topic below helped to solve this problem.
    i have added:
    [code]mysql_query("SET character_set_results=utf8", $gaSql['link']);[/code]
    from:
    http://datatables.net/forums/comments.php?DiscussionID=675

    Thank you.
This discussion has been closed.