0

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?..

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

hi, thanks.. but i do not want to combine all variable in one because each variable will generate different data in while.. i want to use 1 code for different variable and different result.
In that case, you can add if condition in while block which will check ID and then you can parse your result as you wish. In this way, one if condition will be added but it will be much more efficient than using For each.
0
$ids=array(813, 945, 835, 777);
for ($i=0;$i<4;$i++){
   foreach ($users_id_array[REGION_NORTH_REFID][$ids[$i]] as $key) {
...

4 Comments

i think of this ways too.. but how can i access the data in while?..$total_regna++; $total_reginprogress++; $total_regpassed++; $total_regfailed++;
i agree with Naveed ... while loop will be the same
You can move these into the array after while loop and use at last.
i got this error btw.. Warning: Invalid argument supplied for foreach()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.