airi
airi

Reputation: 585

PHP function for looping variable

I have this coding :

foreach ($users_id_array[REGION_NORTH_REFID][813] as $key) {

        $query_course = "SELECT ut_lp_marks.obj_id, object_data.title, read_event.spent_seconds, " .
                "read_event.read_count, ut_lp_marks.status, ut_lp_marks.percentage, ut_lp_marks.u_comment FROM ut_lp_marks ".
                "LEFT JOIN object_data ON (object_data.obj_id = ut_lp_marks.obj_id) ".
                "LEFT JOIN read_event ON (read_event.obj_id = object_data.obj_id AND read_event.usr_id = ut_lp_marks.usr_id) ".
                "WHERE ut_lp_marks.usr_id=$key AND object_data.type = 'crs'";

             $set_course = mysql_query($query_course);

                    while($rec_course = mysql_fetch_assoc($set_course))
                    {

                           if ($rec_course['status'] == 0) {

                                $total_regna++;
                            }
                            if ($rec_course['status'] == 1) {
                                $total_reginprogress++; 
                            }
                            if ($rec_course['status'] == 2) {
                                $total_regpassed++;
                            }
                            if ($rec_course['status'] == 3) {
                                $total_regfailed++;
                            }       

                           // $total_attempt = $total_attempt + $rec_course['read_count'];                                                            
                           // $total_spent = $total_spent + $rec_course['spent_seconds']; 


                    }
                    $no_test++;
            }   

the variable that i used is for each :

813, 945, 835, 777 

My problems is my coding right now is only for 1 variable.
How can i used this same code for different variable,do i need to make array or a function?
How can i access .. different $total_regna++; $total_reginprogress++; $total_regpassed++; $total_regfailed++; if i am using 1 code for 4 variable?..

Upvotes: 0

Views: 71

Answers (2)

Luca Rocchi
Luca Rocchi

Reputation: 6464

$ids=array(813, 945, 835, 777);
for ($i=0;$i<4;$i++){
   foreach ($users_id_array[REGION_NORTH_REFID][$ids[$i]] as $key) {
...

Upvotes: 0

Naveed Ramzan
Naveed Ramzan

Reputation: 3593

You can make an array of these values such as

$values = array('813','945','835','777');

and instead of using for each, you can implode this array values in query.

$query_course = "SELECT ut_lp_marks.obj_id, object_data.title, read_event.spent_seconds, " .
            "read_event.read_count, ut_lp_marks.status, ut_lp_marks.percentage, ut_lp_marks.u_comment FROM ut_lp_marks ".
            "LEFT JOIN object_data ON (object_data.obj_id = ut_lp_marks.obj_id) ".
            "LEFT JOIN read_event ON (read_event.obj_id = object_data.obj_id AND read_event.usr_id = ut_lp_marks.usr_id) ".
            "WHERE ut_lp_marks.usr_id in (".implode(',',$values).") AND object_data.type = 'crs'";

Now while loop will be same.

I think it will be good in your case.

Upvotes: 2

Related Questions