table sorting

table sorting

by Андрей Тюфтин -
Number of replies: 14

the problem is this. Not working to sort the table. the table is displayed by using flexible_table. Here's a snippet enabled sorts:


$table = new flexible_table('public-showentry-list');

$table->define_columns($tablecolumns);

$table->define_headers($tableheaders);

$table->define_baseurl($baseurl);

$table->set_attribute('class', 'admintable generaltable');

//$table->sortable(true, 'flag', SORT_DESC);

$table->sortable(true, 'lastname', SORT_DESC);

        $table->set_attribute('cellspacing', '0');

        $table->set_attribute('id', 'public_entries');

        $table->set_attribute('class', 'generaltable generaltable');

        $table->set_control_variables(array(

                    TABLE_VAR_SORT    => 'ssort',

                    TABLE_VAR_IFIRST  => 'sifirst',

                    TABLE_VAR_ILAST   => 'silast',

                    TABLE_VAR_PAGE    => 'spage'

                    ));

$table->setup();


$table->initialbars(true);

if ($table->get_sql_sort()) {

    $sort = 'ORDER BY '. $table->get_sql_sort();

} else {

    $sort = '';

}

list($where, $params) = $table->get_sql_where();

if ($where) {

    $where = 'WHERE '. $where;

}


generates an error:

Debug info: 
Error code: invalidid 
$a contents:
Stack trace:
  • line 463 of \lib\setuplib.php: moodle_exception thrown
  • line 60 of \mod\public\view.php: call to print_error()


line60     print_error('invalidid', 'public');

What could be wrong? the table itself is excreted in view.php
Average of ratings: -
In reply to Андрей Тюфтин

Re: table sorting

by Андрей Тюфтин -

friends, please help! what assumptions could be?

In reply to Андрей Тюфтин

Re: table sorting

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

As the code you have quoted doesn't seem to include the line that is generating the error, it is hard to really give you any advice.

I assume that mod/public/view.php is expecting some sort of 'id' value passed to it and your code for the table does not generate a suitable 'id' value. You've called 'define_baseurl', but your code example doesn't tell anyone what value you're passing into that function - that would be my starting point for investigating the problem.

Average of ratings: Useful (1)
In reply to Davo Smith

Re: table sorting

by Андрей Тюфтин -

Hello Davo. Yes, I'm beginning now to understand that your problem is related to 'define_baseurl'. Here is my code 

$baseurl = new moodle_url('mod/public/view.php'); 

I know the example of two documents \mod\feedback\show_entries.php and www\tag\manage.php (attached to the record). In show_entries.php got these two lines 

$baseurl = new moodle_url('/mod/feedback/show_entries.php');
$baseurl->params(array('id'=>$id, 'do_show'=>$do_show, 'showall'=>$showall)); 

reading all of this I did not understand how this works? what keeps $params? 

as is in my case?

In reply to Андрей Тюфтин

Re: table sorting

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I don't quite follow your question about what keeps $params?

When you create the flexible_table, you pass it the 'baseurl' (including all the essential params to get back to the current page, in this case 'id' is one of those values).

When it generates each of the 'sort' links, flexible_table uses this 'baseurl' and adds on its own params to handle the change in sort.

When the user clicks on the link, they arrive back at the same page, but with the 'sort' value changed. If you miss out the 'id' value from the 'baseurl', then this will not be included in the URL, so the view.php page will complain that it doesn't know which instance of the 'public' activity it is supposed to be displaying.

Average of ratings: Useful (1)
In reply to Davo Smith

Re: table sorting

by Андрей Тюфтин -

I think I understand what you are, but how do I do that? in what form do I specify and where is this 'id'?

In reply to Андрей Тюфтин

Re: table sorting

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Assuming your table is being displayed by the PHP file /mod/public/view.php:

$baseurl = new moodle_url('/mod/public/view.php', array('id' => $id));

Somewhere at the top of the file it will say something like:

$id = optional_param('id', null, PARAM_INT);

It will then do something with this $id value.

It is this $id value you want to pass into the $baseurl, so that the table can get back to this page when done.


Average of ratings: Useful (1)
In reply to Davo Smith

Re: table sorting

by Андрей Тюфтин -

thanks, now there is no error! but sorting doesn't work( only shows arrows columns in which direction is sorted

In reply to Андрей Тюфтин

Re: table sorting

by Андрей Тюфтин -

to me it becomes clear. it is necessary to integrate with the sql query. Yes? can someone bring a simple example?

In reply to Андрей Тюфтин

Re: table sorting

by Андрей Тюфтин -

here is the entire table. with SQL bad sign..what's wrong?



if (!$entries = $DB->get_records('public_entries', array(), 'concept ASC')) { print_error('moduledoesnotexist', 'error'); } $baseurl = new moodle_url('../public/view.php', array ('id'=>$id)); //$baseurl->params(array('id'=>$id, 'showall'=>$showall)); $tablecolumns = array('concept', 'authors', 'phouse', 'year', 'stranic', 'typep', 'definition',''); $tableheaders = array($strconcept, $strauthors, $strphouse, $stryear, $strstranic, $strtypep, $strdefinition, ''); $table = new flexible_table('public-showentry-list'); $table->define_columns($tablecolumns); $table->define_headers($tableheaders); $table->define_baseurl($baseurl); $table->set_attribute('class', 'admintable generaltable'); $table->sortable(true, 'lastname', SORT_DESC); $table->set_attribute('cellspacing', '0'); $table->set_attribute('id', 'public_entries'); $table->set_attribute('class', 'generaltable generaltable'); $table->set_control_variables(array( TABLE_VAR_SORT => 'ssort', TABLE_VAR_IFIRST => 'sifirst', TABLE_VAR_ILAST => 'silast', TABLE_VAR_PAGE => 'spage' )); $table->no_sorting('definition'); $table->setup(); if ($table->get_sql_sort()) { $sort = 'ORDER BY '. $table->get_sql_sort(); } else { $sort = ''; } list($where, $params) = $table->get_sql_where(); if ($where) { $where = 'WHERE '. $where; } $query = " SELECT p.concept, p.authors, p.phouse, p.year, p.stranic, p.typep, FROM {public_entries} p $where ORDER BY p.concept, p.authors, p.phouse, p.year, p.stranic, p.typep, $sort"; /* $totalcount = $DB->count_records_sql(" SELECT COUNT(DISTINCT(p.id)) FROM {public_entries} p LEFT JOIN {user} u ON u.id = p.userid $where", $params); $table->initialbars(true); $table->pagesize($perpage, $totalcount); */

if ($entries = $DB->get_records_sql($query, $params, $table->get_page_start(), $table->get_page_size())) { foreach ($entries as $entry) { $vstrentri = $entry->concept; $vauthors = $entry->authors; $vphouse = $entry->phouse; $vyear = $entry->year; $vstranic = $entry->stranic; $vtypep = $entry->typep; $vdefinition = $entry->definition; $icons = public_print_entry_icons($course, $cm, $public, $entry,  

$mode, $hook,'html'); $editentries = $icons; $table->add_data(array( $vstrentri, $vauthors, $vphouse, $vyear, $vstranic, $vtypep, $vdefinition, $editentries )); } } $table->print_html();



displays this error:


Debug info: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM mdl_public_entries p

ORDER BY p.concept, p.authors, p' at line 3

SELECT p.concept, p.authors, p.phouse, p.year, p.stranic, p.typep,

FROM mdl_public_entries p

ORDER BY p.concept, p.authors, p.phouse, p.year, p.stranic, p.typep,
ORDER BY concept DESC, phouse ASC
[array (
)] 
Error code: dmlreadexception
Stack trace:
  • line 443 of \lib\dml\moodle_database.php: dml_read_exception thrown
  • line 996 of \lib\dml\mysqli_native_moodle_database.php: call to moodle_database->query_end()
  • line 455 of \mod\public\view.php: call to mysqli_native_moodle_database->get_records_sql()


In reply to Андрей Тюфтин

Re: table sorting

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

The error message tells you exactly where the problem lies - it is at the end of the 'SELECT' line, immediately before the 'FROM' line.

In case that isn't enough to sort you out: the list of fieldnames in a SELECT statement should not end with a comma.

Also, you shouldn't have 2 ORDER BY statements (and certainly they shouldn't end with an extra comma, either).

In reply to Андрей Тюфтин

Re: table sorting

by Андрей Тюфтин -

Thank you very much, Davo! Sorting works! But now I have another problem( in the last column are icons for editing records, function according to their conclusion is described in the library. They work, but Moodle throws a lot of errors, type :

Notice: Undefined property: stdClass::$sourcepublicid in Z:\home\modlj.ru\www\mod\public\lib.php on line 1293
[Денвер: показать возможную причину ошибки]
Notice: Undefined property: stdClass::$approved in Z:\home\modlj.ru\www\mod\public\lib.php on line 1301

Notice: Undefined property: stdClass::$userid in Z:\home\modlj.ru\www\mod\public\lib.php on line 1316

Notice: Undefined property: stdClass::$timecreated in Z:\home\modlj.ru\www\mod\public\lib.php on line 1338

Notice: Undefined property: stdClass::$id in Z:\home\modlj.ru\www\mod\public\lib.php on line 1341

Notice: Undefined property: stdClass::$id in Z:\home\modlj.ru\www\mod\public\lib.php on line 1345


Notice: Undefined property: stdClass::$sourcepublicid in Z:\home\modlj.ru\www\mod\public\lib.php on line 1293

Notice: Undefined property: stdClass::$approved in Z:\home\modlj.ru\www\mod\public\lib.php on line 1301


this is not all of them much more.. I suspect that this is due to conditions
if ($entries = $DB->get_records_sql($query, $params, $table->get_page_start(),  $table->get_page_size()))

how do I get rid of this problem? sorry that take a lot of time, but I really need your help..

attached your module, just in case..maybe it will help. You can connect it, it works)


In reply to Андрей Тюфтин

Re: table sorting

by Андрей Тюфтин -

more questions came..) because I do module, changing the Glossary module (in the end, I guess it will remain)) create table course neither search nor sorting by alphabet, authors and date..sql queries all already described. What do I need to change to these sort earned? applicable to the table

In reply to Андрей Тюфтин

Re: table sorting

by dawn alderson -

Davo-you star! What sterling support.

am still scratching about the glossary - as an influence here.

Dawn


In reply to dawn alderson

Re: table sorting

by Андрей Тюфтин -

I suggested that the Glossary is similar to my idea, so I took it as a basis..but, probably, in the future, will make from 0)