SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Feb 2014
    Posts
    12
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    PHP, MySQL, AJAX Filter

    I am trying to create a filter in PHP for a set of data that is passed over to another php file and the results ar then passed back to the original file.

    I have followed a tutorial by Pullo which is just working great for me all except the dynamic table is then created via jQuery and i want to do all this via a php while loop. So ideally:

    1.) The user clicks a checkbox
    2.) Ajax is then run
    Code:
    $.ajax({
    			type: "POST",
    			url: "trading.immediatelist.report_proc.php",
    			dataType : 'json',
    			cache: false,
    			data: {filterOpts: Options},
    			success: function(records){
    				$('#ImmediateList tbody').html(CreateTable(records));
    			}
    		});
    3.) The ajax then passes the array of data over to the trading.immediatelist.report_proc.php file for the SQL to work its magic.
    4.) The SQL is run and the results and then passed back to the original php file
    5.) Results are then put into a PHP while loop for me to play with

    I can get so far, but i can not get the results back into a PHP While Loop. I think the success: part in the ajax needs to be changed, but not a clue how to.

  2. #2
    SitePoint Zealot bronze trophy xMog's Avatar
    Join Date
    Mar 2011
    Posts
    145
    Mentioned
    3 Post(s)
    Tagged
    1 Thread(s)
    The piece of code in the success part has a method called CreateTable().
    Can you paste this method here? I would guess it's another Ajax method calling another PHP script?

    If you didn't know, here's what this line means
    Code:
    $('#ImmediateList tbody').html(CreateTable(records));
    This : $('#ImmediateList tbody').html('blahblah') will write 'blahblah' inside the HTML element "tbody" which has for parent an element with the ID "ImmediateList"
    For example, this will probably be a table like this:

    Code:
    <table id="ImmediateList">
    <tbody>
      </tbody>
    </table>
    Since CreateTable(records) is passed as a parameter to the .html() method, I would guess that the CreateTable() method would call the PHP script passing the "records" as an argument.
    And, since the .html() methods writes directly into the TBODY tag, the PHP script should output <tr><td>...</td></tr> type of code.

    But I still don't understand why you would need 2 different PHP call to do this but anyway... If you paste more code we'll be able to help you a little bit more

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    41
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    @Danian create a wrapper for trading.immediatelist.report_proc.php.

    Say you have a file do-complex-stuff.php that fetches a list of names from a database, JSON encodes it, and echos it to the client; and I want to remove all the names except the ones that end in -ie or -y. Then wrapper.php would want to look something like this:
    PHP Code:
    ob_start();
    require_once 
    'do-complex-stuff.php';
    $json ob_get_contents();
    ob_end_clean();

    $allNames json_decode($json);
    $niceNames array_filter($allNames, function($n) { return preg_match('/(y|ie)$/'$n); });
    echo 
    json_encode($niceNames); 
    Of course the ideal is to find a smaller portion of do-complex-stuff.php that could be called directly from wrapper.php so that you don’t have to output-buffer, decode, and re-encode the JSON. You might find such a callable thing required from another file within trading.immediatelist.report_proc.php

  4. #4
    SitePoint Member
    Join Date
    Feb 2014
    Posts
    12
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you didn't know, here's what this line means
    Code:
    $('#ImmediateList tbody').html(CreateTable(records));
    Since CreateTable(records) is passed as a parameter to the .html() method, I would guess that the CreateTable() method would call the PHP script passing the "records" as an argument.
    And, since the .html() methods writes directly into the TBODY tag, the PHP script should output <tr><td>...</td></tr> type of code.

    But I still don't understand why you would need 2 different PHP call to do this but anyway... If you paste more code we'll be able to help you a little bit more [/QUOTE]

    OK,

    Sorry for taking so long to reply. THe full jQuery is below:

    Code:
    function CreateTable(data){
    	var Stripe = true;
    	var tbl_body = "";
    
    	$.each(data, function(k, v) {
    		var tbl_row = "",
    		currRecord = this;
    
    		$.each(this, function(k , v) {
    			//	EDIT/DELETE LINKS
    			tbl_row += "<td align='center'>"+v+"</td>";
    		})
    		
    		tbl_body += "<tr>"+tbl_row+"</tr>";
    	})
    
    	return tbl_body;
    }
    
    function GetFilterOptions(){
    	var opts = [];
    	$checkboxes.each(function(){
    		if(this.checked){
    			opts.push(this.id);
    		}
    	});
    
    	return opts;
    }
    
    function UpdateFilter(Options){
    	$.ajax({
    		type: "POST",
    		url: "trading.immediatelist.report_proc.php",
    		dataType : 'json',
    		cache: false,
    		data: {filterOpts: Options},
    		success: function(records){
    			$('#ImmediateList tbody').html(CreateTable(records));
    		}
    	});
    }
    
    function handleDebug(debugInfo){
    	$("#Debug").html("<pre>" + debugInfo + "</pre>");
    }
    
    var $checkboxes = $("input:checkbox");
    $checkboxes.on("change", function(){
    	var opts = GetFilterOptions();
    	UpdateFilter(opts);
    });
    
    $checkboxes.trigger("change");
    The issue is the:

    Code:
    $('#ImmediateList tbody').html(CreateTable(records));
    part. It returns the results to a jQuery function called CreateTable(records) when really i want it return the results to a php function called CreatePHPRable(data).

    How do i do this? How do i tell it to use the php function and not the jQuery function?

  5. #5
    SitePoint Member
    Join Date
    Feb 2014
    Posts
    12
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK,

    So i have managed to progress and i am now able to use PHP to write my table by using:

    Code:
    //	BUILD TABLES
    function GetFilterOptions(){
    	var opts = [];
    	$checkboxes.each(function(){
    		if(this.checked){
    			opts.push(this.id);
    		}
    	});
    
    	return opts;
    }
    
    function UpdateFilter(Options){
    	$.ajax({
    		type: "POST",
    		url: "trading.immediatelist.report_proc.php",
    		dataType : 'html',
    		cache: false,
    		data: {filterOpts: Options},
    		success: function(data){
    			$('#ImmediateList tbody').html(data);
    		}
    	});
    }
    
    var $checkboxes = $("input:checkbox");
    $checkboxes.on("change", function(){
    	var opts = GetFilterOptions();
    	UpdateFilter(opts);
    });
    
    $checkboxes.trigger("change");
    However, to debug i need to know what SQL is being run, how do i echo back to screen what SQL is being run at the same time?

  6. #6
    SitePoint Zealot bronze trophy xMog's Avatar
    Join Date
    Mar 2011
    Posts
    145
    Mentioned
    3 Post(s)
    Tagged
    1 Thread(s)
    I wrote a post on this subject last week on my blog:http://www.mogosselin.com/6-debuggin...s-should-know/

    The fastest trick would be to log something in your default log file with error_log (...)

    The best, most useful long term method would be to install xDebug or phpdbg. IIt'll take more time to install it at first, but it's good practice and easier to debug your code when you can use breakpoints.

    I'm on my mobile, so I'm sorry if there's any weird grammar error

    PS The fast but ugly way would be to output what you want in another row in your table. For example, after looping on your result, add a row that just output the SQL query.

    Note that I recommend investing time right now in a good solution as it will make you more efficient long term (and a better programmer)


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •