FORMfields, the premiere web framework
Login
Support Home | Knowledge Base | Documentation | Forums | Contact Us
FORMfields v3 Tutorials
FORMfields Tutorials
FORMfields Lib Installation
FORMgen Installation
Manually Creating a Form
Load/Unload From a DB
Emailing a Form
Forms on Your Webpage
Creating Tables
Pagifying Data
Using CSS to Format Forms 1
Using CSS to Format Forms 2
Other Tutorials
Pagifying Data with the SearchResultsField
Last Updated: September 21st, 2007
Overview
In this tutorial we will demonstrate how to use the SearchResultsField to automatically pagify the display of large amounts of database data. We highly recommended that you complete the Creating Tables tutorial before continuing this tutorial.
Creating a Basic SearchResultsField

Preview SearchResults Example 1

Creating a SearchResultsField is simple:
<?
    
// Instantiate a container for the SearchResultsField
    
$ffForm = new FfForm();

    
// This is our query. It can be any valid SQL query
    
$sql "
        SELECT
            CONCAT('<img src=\"',image,'\" />') AS 'Thumbnail',
            CONCAT('<a target=\"_blank\" href=\"',url,'\">',url,
                '</a><br /><br />',description) As 'Description',
            CONCAT('<span style=\"white-space:nowrap;\">',DATE_FORMAT(inserted_on,
                '%Y-%m-%d %h:%i %p'),'</span>') AS 'Inserted On'
        FROM ffexample_websites
    "
;

    
// Create the SearchResultsField and add it to our FfForm
    
$ffForm->addField(new SearchResultsField("websites""Websites"$sql5));
    
    
// Enable column sorting
    
$ffForm->formFields["websites"]->tableSet->enableSort true;
    
    
// Capture and process any button presses
    
$ffForm->getParameters();
?>
Note: Each SearchResultField contains a TableSet. Therefore, you can use any TableSet method to manipulate a SearchResultsField. For example:
<?
    
// Hide the "Inserted On" column.
    
$ffForm->formFields["websites"]->tableSet->hideColumns["Inserted On"] = true;
?>
Display the SearchResultsField:

    <form action="<? $_SERVER["REQUEST_URI"?>" method="post">
        <?
            
// Display the HTML for the SearchResultsField
            
echo $ffForm->getFieldTag("websites");
        
?>
    </form>
    
Here are links to download Example 1: searchResultsEx1.php, searchResultsEx1.sql, searchResultsEx1ExecuteSql.php. To install this example, upload all the files to a directory on your website and use your web browser to visit searchResultsEx1ExecuteSql.php and create the database table. You can then visit the example at searchResultsEx1.php.
Adding Checkboxes to a SearchResultsField
Sometimes we want to add a Checkbox column to each row so that the user can select multiple rows and then perform an operation on all the selected rows. We will now demonstrate how this can be done using a SearchResultsField.

Preview SearchResults Example 2

<?
    
// Instantiate a container for the SearchResultsField
    
$resultsForm = new FfForm();

    
// This is our query. It can be any valid SQL query
    
$sql "
        SELECT
            id,
            CONCAT('<img src=\"',image,'\" alt=\"Thumbnail\" />') AS 'Thumbnail',
            CONCAT('<a target=\"_blank\" href=\"',url,'\">',url,
                '</a><br /><br />',description) As 'Description',
            CONCAT('<span style=\"white-space:nowrap;\">',
                DATE_FORMAT(inserted_on,'%Y-%m-%d %h:%i %p'),'</span>') AS 'Inserted On'
        FROM ffce_websites
    "
;

    
// Create the SearchResultsField and add it to our FfForm
    
$resultsForm->addField(new SearchResultsField("websites""Websites"$sql5));

    
// Capture and process any button presses
    
$resultsForm->getParameters();

    
// Add the checkbox column and use the 1st column for the value associated with the row
    
$resultsForm->formFields["websites"]->addCheckboxColumn();

    
// We used the id column so that the checkboxes could reference the row, but we don't
    // want to display the ids to the user, so we hide the id column.
    
$resultsForm->formFields["websites"]->tableSet->hideColumns["id"] = true;

    
// Retrieve the selected ids as a PHP array
    
$ids $resultsForm->formFields["websites"]->getCheckboxValues();
    if (
sizeof($ids) > 0) {
        echo 
"Selected IDs: ";
        
print_r($ids); // Print the ids
    
}

    
// Display the HTML for the SearchResultsField
    
echo $resultsForm->getFieldTag("websites");

    
// We display a "Print Selected" button, which just refreshes the page when clicked.
    
$ffForm = new FfForm();
    
$ffForm->addField(new SubmitField("ff_submit""Print Selected"));
    echo 
$ffForm->getTableTag();
?>
The above example only prints the selected ids, instead we might want to do something like delete all the selected rows and redirect to the homepage. To do this we could instead use:
<?
    
// Retrieve the selected ids as a PHP array
    
$ids $resultsForm->formFields["websites"]->getCheckboxValues();
    if (
sizeof($ids) > 0) {
        
$ffDb = &FfDb::s();
        foreach (
$ids as $id) {
            
$ffDb->query("DELETE FROM ffce_websites WHERE id='" $id "'");
        }
        
header("Location: /"); // Redirect to the homepage
    
}
?>
Here are links to download Example 2: searchResultsEx2.php, searchResultsEx1.sql, searchResultsEx1ExecuteSql.php. To install this example, upload all the files to a directory on your website and use your web browser to visit searchResultsEx1ExecuteSql.php and create the database table (you may have already done this for Example 1). You can then visit the example at searchResultsEx2.php.
Support | Earn Money
Copyright © 2005-2018 Brain Book Software LLC.
Built with FORMfields, the premiere web framework.