Welcome Guest, Not a member yet? Register   Sign In
Help in converting vanilla PHP to CI (AJAX + filter + infinity scroll)
#1
Question 

I'm trying to convert this PHP tutorial to CI Product Ajax Search Filter With Infinite Scroll More Using PHP And Mysqli. But so far, no luck. I "think" my problem is the Javascript code, specially this part. 

Code:
$.get("autoload.php?group_no="+total_record+"&brand="+brand+"&material="+material+"&size="+size,

I don't know how to make this JS code CI friendly. I tried something like this, but it didn't work.
Code:
$.get(baseURL+'/index.php/home/filtered'+group_no="+total_record+"&brand="+brand+"&material="+material+"&size="+size)

I'm after a kind of AJAX search/checkboxes filter with infinite scroll, but I found none implemented in CI. So, I'm trying to convert this tutorial to CI. 

Any tips or suggestions on how to do this?

Regards,
Castle
Reply
#2

https://www.codeigniter.com/user_guide/g...ur-methods

Code:
$.get(baseURL+'/index.php/home/filtered/'+total_record+'/'+brand+'/'+material+'/'+size)
PHP Code:
<?php
class Home extends CI_Controller {

 
       public function filtered($group_no$brand$material$size)
 
       {
 
               echo $group_no;
 
               echo $brand;
 
               echo $material;
 
               echo $size;
 
       }

Reply
#3

Thanks @jreklund. I'm wondering if would be better if I use JSON(serialize) to pass the data from AJAX to the controller?
Reply
#4

That's something I would do. It's easier to extend.
Reply
#5

Could you give an example?
Reply
#6

(This post was last modified: 03-23-2018, 08:23 AM by jreklund.)

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Json extends CI_Controller {
    
    public function 
return_json()
    {
        if( !
$this->input->is_ajax_request() )
        {
            exit;
        }
        
        
$json $this->input->raw_input_stream;
        
$json json_decode($json);
        
/**
        $json now looks like
            stdClass Object
            (
                [group_no] => 1
                [brand] => brand
                [material] => material
                [size] => size
            )
            
        Access it like
        echo $json->group_no;
        **/
        
        // Process json...
        
        // Data to send back
        
$data = array('success' => 'true');
        
        
// Send back json
        
$this->output
                
->set_content_type('application/json')
                ->
set_output(json_encode($data));
    }
    
    public function 
return_html()
    {
        if( !
$this->input->is_ajax_request() )
        {
            exit;
        }
        
        
$json $this->input->raw_input_stream;
        
$json json_decode($json);
        
// Process json...
        
        // Data to send back
        
echo '<article class="col-md-4 col-sm-6">...</article>';
    }

Code:
$.ajax({
    url : "/json/return_json/",
    type: "POST",
    data: JSON.stringify(
        {group_no: 1, brand: 'brand', material: 'material', size: 'size'}
    ),
    contentType: "application/json; charset=utf-8",
    dataType   : "json",
    success    : function( returnedJson ){
        console.log("Pure jQuery Pure JS object");
        console.log( returnedJson );
    }
});

$.ajax({
    url : "/json/return_html/",
    type: "POST",
    data: JSON.stringify(
        {group_no: 1, brand: 'brand', material: 'material', size: 'size'}
    ),
    contentType: "application/json; charset=utf-8",
    dataType   : "html",
    success    : function( returnedHtml ){
        console.log("Normal html");
        console.log( returnedHtml );
    }
});
Reply
#7

Thank you.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB