Loading while scrolling on Codeigniter

2019-08-15 10:54发布

问题:

EDITS: I have eliminated the variable var path that I had to indicated the URL to the controller and used the url itself, now the path error is gone but I get no data. Something in the communication of the files is not being passed on.

What I am trying to do is upon scrolling, have the page load data from the server. For that purpose I am using a separate jquery file, a view, the controller and the model.

JQUERY FILE. You will see it is pretty straightforward:

    $(document).ready(function() {

    var pageLoaded = 1; //this basically says that we are on page 1 of the results

      $(window).scroll(function()
       {

        if($(window).scrollTop() == $(document).height() - $(window).height())
        {
         pageLoaded = pageLoaded+1; //when this condition has met, increase the pageLoaded so that I can tell php to load in data for page 2/3/4/5, etc, etc

/*// below I send the data to the controller named Home its function loadData gets a variable named id_load with value = to pageLoaded*/

         $.get('home/loadData', {'id_load':pageLoaded}, 
            function(data)
            {
                if (data != "")
                {
                 $('#submissions').append(data);
                }
             }
         );
         //alert(pageLoaded);
        }
       }
      );


    });

CONTROLLER:

<?php if (! defined('BASEPATH')) exit ('No direct script access allowed');


class Home extends CI_Controller 
{
    function __construct()
    {

        parent::__construct();

        $this->load->model('load_model');
        $this->output->enable_profiler(TRUE);

    }


    public function loadData ()
    {
        $pageNumber = $this->input->get('id_load'); 
        $this->load_model->loadPage($pageNumber);

    }


    public function index()  
    {
      $data = array('title' =>'homepage', 'main_content' =>'home_v');
      $this->load->view('template', $data); 

    }



} 

and the MODEL should display the content as it gets new values while u scroll:

<?php


class Load_model extends CI_Model {



    public function loadPage ($pagenumber )

    {

        $sql = $this->db->where('id_load', $pagenumber)->get('data');

        $cadena = "";

        foreach($sql->result_array() as $reg)
        {
        $cadena =   $reg['content'];    

        }

        echo $cadena;


    }

}

回答1:

try this..

in your header <head> put a javascript varaiable with base_url() at the top..

here

html

<head>
  <script>
     var base_url=<?php echo base_url()?>;
  </script>
 ....
</head>

jquery

var path = base_url + 'index.php';


回答2:

i think the problem here is you didn't define or include the URL. the best practice to do this is, define or include base URL in the header section. put following line to the header

var base_url=;

so then use this base_url in anyware you want.



标签: jquery scroll