Welcome Guest, Not a member yet? Register   Sign In
Display Loading Image While Page Loads
#1

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()
 
   {
//code........................
echo $this->load->view('report/exam/ExamReport'$this->datatrue);

 how can i do it.....
Reply
#2

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 )
Reply
#3

@kayinja.denis,

Any reason why it takes so long to go through the calculations?
Reply
#4

(This post was last modified: 09-17-2018, 10:47 PM by kayinja.denis.)

(09-17-2018, 07:39 AM)php_rocs Wrote: @kayinja.denis,

Any reason why it takes so long to go through the calculations?

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){
$studentID $student->student;
foreach(
$subjects as $subject){
$subjectID=$subject->subjectID;

$mark=$this->db->query("select * from mark
          where mark.classesID = '
$classID' && mark.studentID = '$studentID
          && mark.subjectID = '
$subjectID' ")->row()->mark;
}


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.
Reply
#5

(This post was last modified: 09-18-2018, 01:36 AM by Pertti.)

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 = [];
$q $this->db
    
->select('studentID, subjectID, mark')
    ->
where('classesID'$classID)
    ->
get('mark');
if (
$q->num_rows()) {
    foreach (
$q->result() as $row) {
        if (!isset(
$marks[$row->studentID])) {
            
$marks[$row->studentID] = [];
        }
       
        
$marks[$row->studentID][$row->subjectID] = $row->mark;
    }
}

foreach(
$students as $student) {
    
$studentID $student->student;
    foreach(
$subjects as $subject) {
        
$subjectID=$subject->subjectID;
        
$mark = isset($marks[$studentID][$subjectID]) ? $marks[$studentID][$subjectID] : false;
    }


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.
Reply
#6

@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.
Reply
#7

(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 Shy
Reply
#8

(This post was last modified: 09-18-2018, 06:13 AM by php_rocs.)

@Pertti,

Actually, I meant database view (which should be very fast). If it is slow it may be an issue of database indexes.
Reply
#9

(09-18-2018, 06:13 AM)php_rocs Wrote: @Pertti,

Actually, I meant database view (which should be very fast).  If it is slow it may be an issue of database indexes.

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.
Reply
#10

@Pertti,

Yes, it is very fast. I use it quite often. My trick is to build simple views that I can used in complex views. This cuts down on queries that look like a DBA with a DR's degree made it and trouble shooting is easier too.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB