Display Loading Image While Page Loads |
Hello,
I've a challenge, I've a view which display student's report card but it takes long to go through all calculations and returning the results. so i'd like to display Loading Image While Page Loads "ExamReport", PHP Code: public function getExamReport()
Check this out should help you out.
Show loading image while Page is loading using jQuery What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
@kayinja.denis,
Any reason why it takes so long to go through the calculations?
(09-17-2018, 07:39 AM)php_rocs Wrote: @kayinja.denis, yes, I've two loops one for students and the other is for subject, in order to get the students' marks i've to loop into mark table to get marks for each student each subject PHP Code: foreach($students as $student){ so if I've more than 10 students per class doing more than 5 subjects, it'll take long to iterate through that mark query, so that's why I need that loading display but the one i've got is not doing what i want.
This is what is generally called N+1 problem, where you create 1 query for every N items you need. This is usually bad approach, as each DB query, no matter how small, adds couple of milliseconds for checking the DB connection, sending query, waiting DB server response, etc.
You should be able to convert this into single query fairly quickly tho. PHP Code: $marks = []; So, basically, you get all the marks for one class in single query, then create reference array in PHP using array indexes as meaningful data, ie student ID and subject ID. Then, final step, you loop through your data, and see if there's mark value available in using meaningful indexes.
@kayinja.denis,
...OR...you could create a view that does the work for you and all you would have to do is call the view. (09-18-2018, 05:39 AM)php_rocs Wrote: ...OR...you could create a view that does the work for you and all you would have to do is call the view. Depends. If controller/model is slow to handle data, pushing any work to view won't work. Generally speaking you only have 2 options here, 1) improve your code so that single request to controller gets result in acceptable time (there are few ways to achieve this) or 2) load generic view with loader, and get AJAX to load slow-loading request behind the scenes with at least some visual aid to visitor that site is still working on the request. Browser needs to load all the HTML to function properly, so even if you manage somehow display part of HTML, the whole document itself keeps loading, I'd say it's very unreliable way to do it, I had to learn that the hard way. Unless by view, you meant AJAX/JS, in which case... I agree ![]() (09-18-2018, 06:13 AM)php_rocs Wrote: @Pertti, Ah yeah, that's a different, but slightly complicated ballgame all together. Our previous Technical Director said how he built a site to only use single query for every page, he apparently scripted single stored procedure that worked all page content out and returned it, in single DB request, and it was apparently very fast. Haven't seen it in action myself, so can't vouch for it, but on paper, it makes sense, making multiple queries has overhead v making a single query so... Guess really depends on exact data etc. |
Welcome Guest, Not a member yet? Register Sign In |