Skip to content


Recordset azkarrak

Lanean hurrengo kode hau topatu dut. Konfidentzialtasun ituna sinatu harren argi dago kodea ez dela enpresako, eta hemen jartzen badut, beste ezer baino lanean behar dudanean azkar topatzeko modua besterik ez da, eta noski, nik edo beste edonork behar badu erabili ahal izateko ;-) .

Honek ni lehenago idatzia nuen orrialdeztapen kodea alderatzera behartzen nau nire hobe beharrez yujuuu :-D

/*
THIS CLASS WAS DEVELOPED BY WEB FORTE LTD.

YOU CAN USE IT AS YOU LIKE. WE DON'T CARE.

HTTP://WWW.ALL-IT.RO/ THIS SITE USE IT AND WORKS FINE
*/

class recordset{
    var $_connection="";
    var $_sql_string=""; //we send the sql string without limits and without "; "

    var $_url = ""; //it's mandatory to send url with the rest of QUERY_STRING parameters; if there aren't other parameters you send this with ?. Ex: $myobj->_url = "mypage.php?"
    var $_p_name = "page"; //if you don't send this, the default is page
   
    var $_records_page = 25;
    var $_page = 1;
    var $_showeachside = 4;
    var $_idioma = 'es';
   
    function get_total_pages(){
        $sql_count=$this->_sql_string;
        $query_count=mysql_query($sql_count);
        $cantidad = mysql_num_rows($query_count);
        $row_count=mysql_fetch_array($query_count);
        $total_pages=ceil($cantidad/$this->_records_page);
        return $total_pages;
    }
   
    function get_no_pages(){
        // you use this for getting the total number of pages based on $_records_page and row count from mysql result
        $fromPos = strpos( strtolower($this->_sql_string), "from");
        $sql = "select count(*) ".substr( $this->_sql_string, $fromPos);
       
        $R = mysql_query( $sql, $this->_connection);
        $T = mysql_fetch_array( $R);

        $records_no = $T[0];

        $pages_no = floor($records_no / $this->_records_page);
        $pages_no = ($records_no % $this->_records_page==0?$pages_no:$pages_no+1);
       
        return $pages_no;
    }
   
    function get_str_pages(){
        // Gives you the string for navigation on pages
        // 1 2 3 ... etc ...
        switch ($this->_idioma){
            case 'es':
                $anterior='anterior';
                $siguiente='siguiente';
            break;
            case 'eu':
                $anterior='aurrekoa';
                $siguiente='hurrengoa';
            break;
            case 'en':
                $anterior='previous';
                $siguiente='next';
            break;
            case 'fr':
                $anterior='précédente';
                $siguiente='suivante';
            break;     
        }
        $pagina_anterior = $this->_page - 1;

        if($this->get_no_pages()>1){
        // INICIO CODIGO DE PAGINACION
        $STR = "<ol class='pag'>";
            // ANTERIOR
            if((($this->_page*$this->_records_page) - $this->_records_page) > 0){      
                    $STR .= "<li class='first'><a href=\"".$this->_url."&amp;".$this->_p_name."=$pagina_anterior\">".$anterior."</a></li>";
                    if($this->_page > $this->_showeachside){
                        $STR .= "<li class='first02'><a href=\"".$this->_url."&amp;".$this->_p_name."=1\">1</a><span>...</span></li>";      // ir a primera página            
                    }
                }else{
                    $STR .= "<li class='first'><span>".$anterior."</span></li>";               
                } // end if
           
            // LISTADO DE NÚMEROS
            $eitherside = ($this->_showeachside * $this->_records_page);
    //      $pg=1;
            for($i=1; $i< =($this->get_no_pages()); $i++){
                $class=($i==$this->_page)?"'sel'":"'paginacion'";

                if((($i*$this->_records_page) > (($this->_page*$this->_records_page) - $eitherside)) && (($i*$this->_records_page) < (($this->_page*$this->_records_page) + $eitherside))){
                    $STR .= "<li><a class=".$class."  href=\"".$this->_url."&amp;".$this->_p_name."=$i\">$i</a></li>";
                    if ($i < $this->get_no_pages()){
                        $siguiente_pagina = $this->_page+1;
                    }
                } // end if
        //      $pg++;
            } // end for   

             // SIGUIENTE

                if(($this->_page * $this->_records_page)< ($this->_records_page*($this->get_no_pages()))){
                    if($this->_page < = $this->get_total_pages()-$this->_showeachside){
                        $STR .= "<li class='last02'><span>...</span><a href=\"".$this->_url."&amp;".$this->_p_name."=".$this->get_no_pages()."\">".$this->get_total_pages()."</a></li>"; // ir a última página   
                    }
                    $STR .= "<li class='last'><a href=\"".$this->_url."&amp;".$this->_p_name."=$siguiente_pagina\">".$siguiente."</a></li>";
                }else{
                    $STR .= "<li class='last'><span>".$siguiente."</span></li>";               
                } // end if
            $STR .= "</ol>";
        // FINAL DEL CODIGO DE PAGINACION
        }
        return $STR;
    }
   
    function get_arr_rst($Paged=true){
        /*
        This is the method which gives you an array with your sql result
        Is a key based array so you can acces it by the name of field alias $R[12]["id"] will return the value of ID for the 12 record in yoour result
        */

        $Fields = array();
       
        $low = "0";
        $S = $this->_sql_string;
        if($Paged){
            $low = $this->_records_page*($this->_page-1);
            $S .= " limit $low, ".$this->_records_page;

        }

        $Raw = mysql_query( $S, $this->_connection);
       
        while($F=mysql_fetch_field($Raw)){
            array_push($Fields, $F->name);
        }
       
        $RECORDS = array();
       
        while($row=mysql_fetch_array($Raw)){
            for($i=0; $i<count ($Fields); $i++){
                $REC[$Fields[$i]] = $row[$Fields[$i]];
            }
            array_push($RECORDS, $REC);
        }
       
        return $RECORDS;
    }
}

Ala ere bilatzaileren baten begiratuz begirada azkar batean 3 edo 4 classe topatu kodituzue lan hau egiteko GNU lizentzia edo antzekoarekin.

Posted in Php.

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.