SoFunction
Updated on 2025-04-13

Specially written for beginners in combination with smarty class page 2/3



// Send SQL statement function
    public function send_query( $sql ) 
    { 
        switch ( strtoupper( $this->config->database ) ) 
        { 
            case 'MYSQL' : 
                return $this->send_mysql_query( $sql ); 
                break; 
            case 'ACCESS' : 
                return $this->send_odbc_query( $sql ); 
                break; 
            default : 
$this->sys_err( 'Database type is wrong. This class currently only supports two databases: MYSQL and ACCESS', 'die');
                break; 
        } 
    } 

//Send SQL statements to MYSQL function
    private function send_mysql_query( $sql ) 
    { 
        @$rs = mysql_query( $sql, $this->conn ); 
        if ( $rs == false ) 
        { 
            $mysql_err = mysql_error(); 
$this->sys_err( "SQL statement:{{$sql}} failed to execute because:{{$mysql_err}}", 'die');
        } 
        return $rs; 
    } 

//Send SQL statements to ACCESS function
    private function send_odbc_query( $sql ) 
    { 
        @$rs = odbc_exec( $this->conn, $sql ); 
        if ( $rs == false ) 
        { 
            $odbc_err = odbc_errormsg( $this->conn ); 
$this->sys_err( "SQL statement:{{$sql}} failed to execute because:{{$odbc_err}}", 'die');
        } 
        return $rs; 
    } 

// Get the query return function
    public function select_query( $sql, $retuen_res = false ) 
    { 
        $res = $this->send_query( $sql ); 
        if ( $retuen_res == true ) 
        { 
            return $res; 
        } 
        switch ( strtoupper( $this->config->database ) ) 
        { 
            case 'MYSQL' : 
                return $this->select_mysql_query( $res ); 
                break; 
            case 'ACCESS' : 
                return $this->select_access_query( $res ); 
                break; 
            default : 
$this->sys_err( 'Database type is wrong. This class currently only supports two databases: MYSQL and ACCESS', 'die');
                break; 
        } 
    } 

// Get the MYSQL query return function
    private function select_mysql_query( $res ) 
    { 
        $arr = array(); 
        while ( false != ( $rs = mysql_fetch_assoc( $res ) ) ) 
        { 
            $arr[] = $rs; 
        } 
        mysql_free_result( $res ); 
        return ( count( $arr ) > 0 ? $arr : false ); 
    } 

// Get the ACCCESS query return function
    private function select_access_query( $res ) 
    { 
        $arr = array(); 
        while ( false != ( $rs = odbc_fetch_array( $res ) ) ) 
        { 
            $arr[] = $rs; 
        } 
        odbc_free_result( $res ); 
        return ( count( $arr ) > 0 ? $arr : false ); 
    } 

// Get the system error function
public function sys_err( $err_msg, $method, $err_Notice = 'Sorry, a system error occurred on this site, please try again later.' )
    { 
        $this->err_record( 'sys', $err_msg ); 
        switch ( $method ) 
        { 
            case 'keep': 
                return; 
                break; 
            default: 
                $this->err_notice( $err_notice ); 
                break; 
        } 
    } 

// Get user error function
    public function user_err( $err_notice, $method, $re_href = '', $err_msg = '' ) 
    { 
        if ( !empty( $err_msg ) ) 
        { 
            $this->err_record( 'user', $err_msg ); 
        } 
        switch ( $method ) 
        { 
            case 'keep': 
                return; 
                break; 
            default: 
                $this->err_notice( $err_notice, $re_href ); 
                break; 
        } 
    } 

//Record error function
    private function err_record( $type, $err_msg ) 
    { 
        $err_url = $this->the_dir . 'lib/error/'; 
        $err_url .= ( $type == 'sys' ? '' : '' ); 
        $record_msg =  date( 'Y-m-d H:i:s' ) . '|' . $err_msg . "\n"; 
        $this->file_put( $err_url, $record_msg, 'ab' ); 
    } 

// File writing function
    public function file_put( $url, $content, $method ) 
    { 
        $dir = str_replace( basename( $url ), '', $url ); 
        if ( !file_exists( $dir ) ) 
        { 
$this->sys_err( "{{$dir}} folder does not exist", 'die');
        } 
        @$f = fopen( $url, $method ); 
        @flock( $f, LOCK_NM ); 
        @fwrite( $f, $content, strlen( $content ) ); 
        @flock( $f, LOCK_UN ); 
        @fclose( $f ); 
    } 

// Prompt error function
    protected function err_notice( $err_notice, $re_href = '' ) 
    { 
        $err_page = $this->the_dir . $this->err_page; 
        if ( !file_exists( $err_page ) ) 
        { 
$this->sys_err( 'Error prompt page missing', 'keep' );
die( 'We're sorry, a system error occurred on this site, please wait and try again.');
        } 
        $err_html = file_get_contents( $err_page ); 
        $err_html = str_replace( '<%$err_notice%>', $err_notice, $err_html); 
        $err_html = str_replace( '<%$this_url%>', $this->the_dir, $err_html); 
        $err_html = str_replace( '<%$re_href%>', $re_href, $err_html); 
        echo $err_html; 
        exit; 
    } 

// Functions used to set the template file path
    function set_dir( $file_dir = 'smarty_file' ) 
    { 
        if ( !preg_match( '{/$}', $file_dir ) ) 
        { 
            $file_dir .= '/'; 
        } 
        if ( !file_exists( $this->the_dir . $file_dir . $this->template_dir ) ) 
        { 
$this->sys_err( 'smarty template path missing', 'die' );
        } 
        if ( !file_exists( $this->the_dir . $file_dir . $this->compile_dir ) ) 
        { 
$this->sys_err( 'smarty compile path missing', 'die' );
        } 
        if ( !file_exists( $this->the_dir . $file_dir . $this->cache_dir ) ) 
        { 
$this->sys_err( 'smarty cache path missing', 'die');
        } 
        $this->template_dir = $this->the_dir . $file_dir . $this->template_dir; 
        $this->compile_dir = $this->the_dir . $file_dir . $this->compile_dir; 
        $this->cache_dir = $this->the_dir . $file_dir . $this->cache_dir; 
    } 

// Generate static page functions
    public function create_html( $tpl, $file_url = '', $html_name = '' ) 
    { 
        $html_tpl = $this->fetch( $tpl ); 
//The file name of the static file generated
        if ( empty( $html_name ) ) 
        { 
            $file_name = strtolower( basename( $_SERVER['PHP_SELF'] ) ); 
            $file_name = str_replace( '.php', ".{$this->html_cache}", $file_name ); 
        } 
        else 
        { 
            $file_name = $html_name; 
        } 
        if ( !empty( $file_url ) && !preg_match( '!\/$!', $file_url ) ) 
        { 
            $file_url .= '/'; 
        } 
        $file_url = !empty( $file_url ) ? $this->the_dir . $file_url : "./{$file_url}"; 
        $file_url .= $file_name; 
        $this->file_put( $file_url, $html_tpl, 'wb'); 
        header("location:{$file_url}"); 
        exit(); 
    } 

// Go to the static page
    public function goto_html( $left_time = null, $file_url = '', $html_name = '' ) 
    { 
        $left_time = ( $left_time == null ? $this->html_cache_lifetime : intval( $left_time ) ); 
//Get the file name of the static file
        if ( empty( $html_name ) ) 
        { 
            $file_name = strtolower( basename( $_SERVER['PHP_SELF'] ) ); 
            $file_name = str_replace( '.php', ".{$this->html_cache}", $file_name ); 
        } 
        else 
        { 
            $file_name = $html_name; 
        } 
        if ( !empty( $file_url ) && !preg_match( '!\/$!', $file_url ) ) 
        { 
            $file_url .= '/'; 
        } 
        $file_url = !empty( $file_url ) ? $this->the_dir . $file_url : "./{$file_url}"; 
        $file_url .= $file_name; 
        if ( !file_exists( $file_url) ) 
        { 
            return; 
        } 
        if ( $left_time == -1 ) 
        { 
            header("location:{$file_url}"); 
            exit; 
        } 
        else 
        { 
            @$fmtime = filemtime( $file_url ); 
            $fmtime = intval( $fmtime ); 
            if ( time() - $fmtime <= $left_time ) 
            { 
                header("location:{$file_url}"); 
                exit; 
            } 
        } 
    } 

?>