CodeIgniter Forums
Auto refresh data through Model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Auto refresh data through Model (/showthread.php?tid=74399)



Auto refresh data through Model - AngelTrader - 09-18-2019

HI Everyone

i'm totaly new to MVC programming and CodeIgniter, so please forgive me for basic question. I couldn't figure it out myself.

I have a controller called "dashboard.php"
I have a model called "members_model.php"
I have a view called "dashboard_view.php"

In my controller I call :
    $this->load->model('members_model');
    $resultdata["membersTotal"] = $this->members_model->Get_Total();

The Get_Total() function returns me a number.
Inside my dashboard_view I have a <div id="membertotal"> in which I echo out $membersTotal and this works fine.

However, now I want to refresh that data in that <div> every 5 seconds.
I know I need to do an AJAX call , but how do I call that model dashboard_model.php to retrieve the new value ?

Hopefully it's somewhat clear, thanks guys!


RE: Auto refresh data through Model - dave friend - 09-18-2019

You will probably want to create a new function in Dashboard. Let's call it refresh_count().

The URL you will tell your AJAX to visit will be https://example.com/dashboard/refresh_count

refresh_count() will also use the same model and make the call

PHP Code:
$this->members_model->Get_Total(); 

What you do with the return from that depends on how you set up your AJAX.
There are many possibilities and almost none of them are wrong.


RE: Auto refresh data through Model - AngelTrader - 09-18-2019

Thank you Dave, that's exactly what I tried to do now , but no matter what I write in the URL it always returns 404 not found.
I think something is wrong with my htaccess or routing ... not sure.

htaccess :
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]


RE: Auto refresh data through Model - AngelTrader - 09-18-2019

Solved, I had to add index.php in between :-)