huck Posted March 18, 2014 Share Posted March 18, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/287054-2d-arrays-and-passing-between-pages/ Share on other sites More sharing options...
Solution Ch0cu3r Posted March 18, 2014 Solution Share Posted March 18, 2014 (edited) 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 March 18, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/287054-2d-arrays-and-passing-between-pages/#findComment-1473000 Share on other sites More sharing options...
huck Posted March 19, 2014 Author Share Posted March 19, 2014 Hi Ch0cu3r Thank you for the help Quote Link to comment https://forums.phpfreaks.com/topic/287054-2d-arrays-and-passing-between-pages/#findComment-1473138 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.