On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • This is an auto-generated support/comment thread for MakeTable.

    Use this forum to post any comments about this addition or any questions you have regarding its use.

    Brief Description:
    A helper class for creating tables from PHP arrays; includes pagination support, sorting functions, and much more.
    • I’ve been tring to ad a button to each row that deletes the row, but I can’t understand how to get the buttons to display. could you show a code example on how to use the form function since there is nothing in the example thats included with the file.


      Thank You
        Hexagone3internet -  marketing - advertisinghexacontact.comEmail marketing service
      • Hopefully someone can help me, I don’t know enough to make this work from the examples?

        I have a table called modx_courses. The table has the following rows which I want to get data from:

        Code
        Start
        End
        Location
        Client
        Courses


        Problem is the snippet returns the following with no records:

        Code Start End Course Location
        25 per page
        Go to page:

        [edit: I get this whether there are records or not]

        I am guessing there is any issue with this area of the code, however when I try to match it with the roes above I get various errors.

         features
        if ($ds= $this->db->query($query)) {
        	// if the query was successful, build our table array from the rows
        	while ($row= $this->db->getRow($ds)) {
        		$industries[]= array(
        			'Code'=> stripslashes($row['Code']),
        			'Start'=> stripslashes($row['Start']),
        			'Start'=> stripslashes($row['Start']),
                                'End'=> stripslashes($row['End']),
                                'Course'=> stripslashes($row['Course']),
                                'Location'=> stripslashes($row['Location']),
        		);
        	}
        }
        
        // make sure our query of industries has returned rows
        if (is_array($industries)) {
        	// create the table header definition with each header providing a link to sort by that field
        	$industryTableHeader= array(
        		'Code'=> $objTable->prepareOrderByLink('Code', 'Code'),
        		'Start'=> $objTable->prepareOrderByLink('Start', 'Start'),
        		'End'=> $objTable->prepareOrderByLink('End', 'End'),
        		'Course'=> $objTable->prepareOrderByLink('Course', 'Course'),
        		'Location'=> $objTable->prepareOrderByLink('Location', 'Location'),
        	);
          [img]http://www.emanz.ac.nz/assets/images/logo/emanz-icon_16x16.gif[/img] Emergency Management Academy of New Zealand [br] http://www.emanz.ac.nz[br][br]MODx Sandbox Login: sandbox Password: castle [br]
          Admin Sandbox Login: sandbox Password: castle
        • Looks like your code is not complete.
          When I read it correctly it is doing the following:
          1. collect all found values in the industries array
          2. build a tableheader (that’s probably what the snippet returns)

          but I see no code where the industries-array is extracted to table-rows! huh
          • Thanks for the reply.

            That was only a section of the code from the MakeTable Snippet. The entire code.

            /**
             * Example snippet using the MakeTable class.
             */
            
            // Check if the class already exists (for multiple usage per page)
            if (!class_exists('MakeTable')) {
            	// load the MakeTable class
            	include $modx->config['base_path'] . "assets/snippets/maketable/maketable.class.php";
            }
            
            // Check for number of records per page preferences and define global setting
            // TODO: need to change this so you can use pagination with multiple instances per page
            //  Currently, you can only use pagination and sorting with one instance per page
            //  but you can present multiple instances without these options
            if (is_numeric($_GET['pageSize'])) {
            	setcookie("pageSize", $_GET['pageSize'], time() + 3600000);
            	$maxPageSize= $_GET['pageSize'];
            } else {
            	if (is_numeric($_COOKIE['pageSize'])) {
            		$maxPageSize= $_COOKIE['pageSize'];
            	} else {
            		$maxPageSize= 25;
            	}
            }
            define('MAX_DISPLAY_RECORDS_NUM', $maxPageSize);
            
            // Instantiate a new instance of the MakeTable class
            $objTable= new MakeTable();
            
            $industries= false;
             
            // query the total number of possible records for the table
            $query= "SELECT COUNT(id) FROM modx_courses";
            $numRecords= $this->db->getValue($query);
            
            // execute the main table query with MakeTable sorting and paging features
            if ($ds= $this->db->query($query)) {
            	// if the query was successful, build our table array from the rows
            	while ($row= $this->db->getRow($ds)) {
            		$industries[]= array(
            			'Code'=> stripslashes($row['Code']),
            			'Start'=> stripslashes($row['Start']),
            			'Start'=> stripslashes($row['Start']),
                                    'End'=> stripslashes($row['End']),
                                    'Course'=> stripslashes($row['Course']),
                                    'Location'=> stripslashes($row['Location']),
            		);
            	}
            }
            
            // make sure our query of industries has returned rows
            if (is_array($industries)) {
            	// create the table header definition with each header providing a link to sort by that field
            	$industryTableHeader= array(
            		'Code'=> $objTable->prepareOrderByLink('Code', 'Code'),
            		'Start'=> $objTable->prepareOrderByLink('Start', 'Start'),
            		'End'=> $objTable->prepareOrderByLink('End', 'End'),
            		'Course'=> $objTable->prepareOrderByLink('Course', 'Course'),
            		'Location'=> $objTable->prepareOrderByLink('Location', 'Location'),
            	);
            	
            	// set the field to be appended as a query string parameter to cell/link actions
            	$objTable->setActionFieldName('id');
            	
            	// set table styling options
            	$objTable->setTableClass('industryTable');
            	$objTable->setRowHeaderClass('headerRow');
            	$objTable->setRowRegularClass('regRow');
            	$objTable->setRowAlternateClass('altRow');
            	
            	// generate the paging navigation controls
            	$objTable->createPagingNavigation($numRecords);
            	
            	// generate the table output and assign to a variable
            	$industryTable = $objTable->create($industries, $industryTableHeader);
            } else {
            	// send a message that no results were found
            	$industryTable = '<p>No industry records were found.</p>';
            }
            
            // output the table content to a placeholder
            // NOTE: you could just as easily return $industryTable if you prefer
            $modx->setPlaceholder('industryTable', $industryTable);
            
              [img]http://www.emanz.ac.nz/assets/images/logo/emanz-icon_16x16.gif[/img] Emergency Management Academy of New Zealand [br] http://www.emanz.ac.nz[br][br]MODx Sandbox Login: sandbox Password: castle [br]
              Admin Sandbox Login: sandbox Password: castle
            • Any hint as to the errors you are getting?
              • I dont get any errors I ONLY get the headers as per below, I don’t see any records. If the table names is wrong I get an error, so I am 99% sure the table is being found, but it fails to say "no records found" if the table is empty and it fails to display records if they are in the table.

                Code   Start    End   Course   Location
                25 per page
                Go to page:
                


                [edit: I get this whether there are records or not]

                If you go to the sandbox as per my signature below, its under MakeTable Courses Table
                  [img]http://www.emanz.ac.nz/assets/images/logo/emanz-icon_16x16.gif[/img] Emergency Management Academy of New Zealand [br] http://www.emanz.ac.nz[br][br]MODx Sandbox Login: sandbox Password: castle [br]
                  Admin Sandbox Login: sandbox Password: castle
                • I’m lost, that output is from the sample database headers, not the code you’re showing. Is this page cached or something?

                  And if it’s not that simple, perhaps the database you have does not have the field name capitalized? Field names returned as associative arrays in PHP are case-sensitive.
                  • Sorry my bad, the example I gave was the orginal output prior to chaning the names (I have corrected that post). The output on the site is Code Start End Course Location .

                    I’m not wanting to display Client on the website either so have left it out of the code, the field names are in capitals as per below


                    Server: localhost Database: emanz_modx Table: modx_courses

                    Field Type Null Default
                    Code varchar(6) No
                    Start varchar(20) No
                    End varchar(20) No
                    Location varchar(30) No
                    Client varchar(100) No
                    Course varchar(100) No
                    id int(6) No
                      [img]http://www.emanz.ac.nz/assets/images/logo/emanz-icon_16x16.gif[/img] Emergency Management Academy of New Zealand [br] http://www.emanz.ac.nz[br][br]MODx Sandbox Login: sandbox Password: castle [br]
                      Admin Sandbox Login: sandbox Password: castle
                    • What is contained in the $industries array you are populating with the results of your query before it gets passed into the $objTable->create($industries, $industryHeaders) function?