Guest User

Untitled

a guest
Mar 31st, 2011
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.60 KB | None | 0 0
  1. <?php
  2.   //EDIT YOUR MySQL Connection Info:
  3.    $DB_Server = "localhost";        //your MySQL Server  
  4.    $DB_Username = "dbuser";                 //your MySQL User Name  
  5.    $DB_Password = "pass";                //your MySQL Password  
  6.    $DB_DBName = "db";                //your MySQL Database Name
  7.         //Prešteje število vrstic za pravilen izpis!
  8.         $link = mysql_connect("$DB_Server", "$DB_Username", "$DB_Password");
  9.             mysql_select_db("$DB_DBName", $link);
  10.             $result = mysql_query("SELECT * FROM 00_osnovni_podatki", $link);
  11.             $num_rows = mysql_num_rows($result);
  12.    
  13.      
  14.   // $DB_TBLName = "00_osnovni_podatki t1 INNER JOIN 01_opisni_podatki INNER JOIN 02_kontaktni_podatki INNER JOIN 03_reklamacije INNER JOIN 04_slike LIMIT 0,$num_rows";                //your MySQL Table Name    
  15.    $DB_TBLName = "00_osnovni_podatki t1
  16.                                     INNER JOIN
  17.                                 01_opisni_podatki t2 ON t1.id = t2.id
  18.                                     INNER JOIN
  19.                                 02_kontaktni_podatki t3 ON t1.id = t3.id
  20.                                     INNER JOIN 03_reklamacije
  21.                                 t4 ON t1.id = t4.id
  22.                                     INNER JOIN
  23.                                 04_slike t5 ON t5.id = t1.id
  24.                                     INNER JOIN
  25.                                 06_uporabniki t6 ON t6.user_id = t1.user_id
  26.                                             LIMIT 0,$num_rows";
  27.    
  28.    //$DB_TBLName,  $DB_DBName, may also be commented out & passed to the browser
  29.    //as parameters in a query string, so that this code may be easily reused for
  30.    //any MySQL table or any MySQL database on your server
  31.    //DEFINE SQL QUERY:
  32.    //you can use just about ANY kind of select statement you want -  
  33.    //edit this to suit your needs!
  34.    $sql = "Select t1.naziv,redna_cena,popust,odstotek_popusta,prihranek,glavna_prednost,naslov_izdelka,uvodni_text,naziv_ponudnika,polni_naslov,slika_izdelka,logotip,slika0,slika1,slika2,slika3,desna,exclusive,preverjen_prodajalec,opomba from $DB_TBLName";
  35.    //Optional: print out title to top of Excel or Word file with Timestamp
  36.    //for when file was generated:
  37.    //set $Use_Titel = 1 to generate title, 0 not to use title
  38.    $Use_Title = 0;
  39.    //define date for title: EDIT this to create the time-format you need
  40.    $now_date = date('m-d-Y H:i');
  41.    //define title for .doc or .xls file: EDIT this if you want
  42.    $title = "Dump For Table $DB_TBLName from Database $DB_DBName on $now_date";
  43.    /*
  44.    Leave the connection info below as it is:
  45.    just edit the above.
  46.    (Editing of code past this point recommended only for advanced users.)
  47.    */
  48.    //create MySQL connection
  49.    $Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
  50.        or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
  51.    //select database
  52.    $Db = @mysql_select_db($DB_DBName, $Connect)
  53.        or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());
  54.    //execute query
  55.    $result = @mysql_query($sql,$Connect)
  56.        or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
  57.    //if this parameter is included ($w=1), file returned will be in word format ('.doc')
  58.    //if parameter is not included, file returned will be in excel format ('.xls')
  59.    if (isset($w) && ($w==1))
  60.    {
  61.        $file_type = "msword";
  62.        $file_ending = "doc";
  63.    }else {
  64.        $file_type = "vnd.ms-excel";
  65.        $file_ending = "xls";
  66.  
  67.    }
  68.    //header info for browser: determines file type ('.doc' or '.xls')
  69.    header("Content-Type: application/$file_type; charset=utf-8");
  70.    header("Content-Disposition: attachment; filename=izvoz_materiala.$file_ending");
  71.    header("Pragma: no-cache");
  72.    header("Expires: 0");
  73.  
  74.    /*    Start of Formatting for Word or Excel    */
  75.    if (isset($w) && ($w==1)) //check for $w again
  76.    {
  77.        /*    FORMATTING FOR WORD DOCUMENTS ('.doc')   */
  78.        //create title with timestamp:
  79.        if ($Use_Title == 1)
  80.        {
  81.            echo("$titlenn");
  82.        }
  83.        //define separator (defines columns in excel & tabs in word)
  84.        $sep = "\n"; //new line character
  85.        while($row = mysql_fetch_row($result))
  86.        {
  87.            //set_time_limit(60); // HaRa
  88.            $schema_insert = "";
  89.            for($j=0; $j<mysql_num_fields($result);$j++)
  90.            {
  91.            //define field names
  92.            $field_name = mysql_fieldname($result,$j);
  93.            //will show name of fields
  94.            $schema_insert .= "$field_name:\t";
  95.                if(!isset($row[$j])) {
  96.                    $schema_insert .= "NULL".$sep;
  97.                    }
  98.                elseif ($row[$j] != "") {
  99.                    $schema_insert .= "$row[$j]".$sep;
  100.                    }
  101.                else {
  102.                    $schema_insert .= "".$sep;
  103.                    }
  104.            }
  105.            $schema_insert = str_replace($sep."$", "", $schema_insert);
  106.            $schema_insert .= "\t";
  107.            print(trim($schema_insert));
  108.            //end of each mysql row
  109.            //creates line to separate data from each MySQL table row
  110.            print "n----------------------------------------------------n";
  111.        }
  112.    }else{
  113.        /*    FORMATTING FOR EXCEL DOCUMENTS ('.xls')   */
  114.        //create title with timestamp:
  115.        if ($Use_Title == 1)
  116.        {
  117.            echo("$titlen");
  118.        }
  119.        //define separator (defines columns in excel & tabs in word)
  120.        $sep = "\t"; //tabbed character
  121.        //start of printing column names as names of MySQL fields
  122.        for ($i = 0; $i < mysql_num_fields($result); $i++)
  123.        {
  124.            echo mysql_field_name($result,$i) . "\t";
  125.        }
  126.        print("\n");
  127.        //end of printing column names
  128.        //start while loop to get data
  129.        while($row = mysql_fetch_row($result))
  130.        {
  131.            //set_time_limit(60); // HaRa
  132.            $schema_insert = "";
  133.            for($j=0; $j<mysql_num_fields($result);$j++)
  134.            {
  135.                if(!isset($row[$j]))
  136.                    $schema_insert .= "NULL".$sep;
  137.                elseif ($row[$j] != "")
  138.                    $schema_insert .= "$row[$j]".$sep;
  139.                else
  140.                    $schema_insert .= "".$sep;
  141.            }
  142.            $schema_insert = str_replace($sep."$", "", $schema_insert);
  143.            //following fix suggested by Josue (thanks, Josue!)
  144.            //this corrects output in excel when table fields contain n or r
  145.            //these two characters are now replaced with a space
  146.            $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
  147.            $schema_insert .= "\t";
  148.            print(trim($schema_insert));
  149.            print "\n";
  150.        }
  151.    }
  152.    ?>
Advertisement
Add Comment
Please, Sign In to add comment