Jump to content

2D Arrays and passing between pages


huck
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi Folks

 

I have been trying to get a sql query to pass between pages in moodle 2.x. Moodle uses object orientation. The problem I have is the processed query return $DB->get_records($sql) will not pass back to index.php without a horrible hack.

 

First: There are two files, index.php and lib.php (has functions in it)

 

index.php calls lib.php with this line:

$users = get_mentor_users($fromdate, $todate);

lib.php executes this function:

function get_mentor_users($fromdate = 0, $todate = 0){

    $sql = "SELECT DISTINCT u.id , u.firstname, u.lastname

    FROM mdl_role_assignments ra, mdl_user u, mdl_course c, mdl_context cxt

    WHERE ra.userid = u.id

    AND ra.contextid = cxt.id

    AND cxt.contextlevel =50

    AND ( roleid =3 )

    AND u.firstname LIKE '%mentor%'";

    

    if($datesql = generate_sql_range($fromdate, $todate, ', mdl_grade_grades.timemodified'))

    {

        $sql .= " AND $datesql";

    }

    

    return $DB->get_records_sql($sql);

    

}

Now for the perplexing part. index.php throws out a blank screen.

 

I rewrote this function to pass the sql string from lib.php to index.php then for index.php to do the sql query locally and then it works fine. Like so:

 

index.php

$sql2 = get_mentor_users($fromdate, $todate);
$users = array();
$users = $DB->get_records_sql($sql2);

But then accessing the array gets kinda ugly. Then in lib.php only the unprocessed sql string is returned back to index.php.

function get_mentor_users($fromdate = 0, $todate = 0){

    $sql = "SELECT DISTINCT u.id , u.firstname, u.lastname

    FROM mdl_role_assignments ra, mdl_user u, mdl_course c, mdl_context cxt

    WHERE ra.userid = u.id

    AND ra.contextid = cxt.id

    AND cxt.contextlevel =50

    AND ( roleid =3 )

    AND u.firstname LIKE '%mentor%'";

    

    if($datesql = generate_sql_range($fromdate, $todate, ', mdl_grade_grades.timemodified'))

    {

        $sql .= " AND $datesql";

    }

    

   return  $sql;   

}

The output of the array below:

Array
(
    [12] => stdClass Object
        (
            [id] => 12
            [firstname] => Mentor Peter
            [lastname] => Swananipoel
            [totalmarked] => 
            [totalcommarked] => 0
            [totalsummarked] => 0
            [markedcompetent] => 0
            [markedincompetent] => 0
        )

    [14] => stdClass Object
        (
            [id] => 14
            [firstname] => Mentor Garfield
            [lastname] => Robertson
            [totalmarked] => 
            [totalcommarked] => 0
            [totalsummarked] => 0
            [markedcompetent] => 0
            [markedincompetent] => 0
        )

    [15] => stdClass Object
        (
            [id] => 15
            [firstname] => Mentor Bert
            [lastname] => Cat
            [totalmarked] => 
            [totalcommarked] => 0
            [totalsummarked] => 0
            [markedcompetent] => 0
            [markedincompetent] => 0
        )

)

How can I pass this array between pages without the shenanigans?

 

Thanks

 

Link to comment
Share on other sites

  • Solution

lib.php executes this function:

 

Now for the perplexing part. index.php throws out a blank screen.

You have a variable scope issue. Functions have their own variable scope, meaning any variables defined outside of that function is not accessible. The same rule apples to variables defined inside of the function.

Have a read of manual on variable scope http://php.net/manual/en/language.variables.scope.php

 

The fix is to pass $DB as an argument to your get_mentor_users function.

function get_mentor_users($DB, $fromdate = 0, $todate = 0){
...
}

// pass $DB as a function argument
$sql2 = get_mentor_users($DB, $fromdate, $todate);
Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.