Custom Select Box for Large Data Lists (PHP, AJAX)

Posted by admin at December 30, 2016

inc/read.php

This script will handle the generating of all records from our tables using the SELECT query. the main function will receive parameters and use a switch case construct to determine the appropriate SQL statement to return that generates the values we need.


<?php
/**
* CRUD Functions.
*
* @package    read.php
* @subpackage 
* @author     BlinkWiki
* @version    1.0
* Copyright 2016 BlinkWiki
* 
*/
function get_read_sql($tbl)
{
    switch($tbl)
    {
        case 'tbl_map':
            // for normalised tables that have secondary keys
            $sql = 'SELECT'
                    .' *'
                .' FROM'
                    .' '.$tbl
                .' WHERE 1'
                    // relate table with secondary table
                    .' AND '.$tbl.'.state_id = tbl_states.state_id'
                ;
            break;
        case 'tbl_lang':
            // languages
        case 'tbl_airports':
            // airports
        case 'tbl_aoc':
            // airlines
        case 'tbl_states':
            // countries
        default:
            // simply read all the records from the single table sent
            $sql = 'SELECT * FROM '.$tbl.' WHERE 1';

    }

    // return the query
    return $sql;
}
?>

For the purpose of this tutorial, we will simply be taking only columns from the selected table and will not be relating normalised columns to secondary reference tables, as seen in the SQL queries in the first case (tbl_map). This case in the switch construct is just illustrate how the relationships to other tables can be handled in mapped tables. Also because the SQL statements for normalised columns and tables will differ from table to table, we need to put all SQL statements in a separate file (the read.php). CRUD-ing normalised tables

   0 likes

Pages: 1 2 3 4 5 6


Suggested Read