Advertisement
Guest User

Untitled

a guest
May 13th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.56 KB | None | 0 0
  1. <?php
  2.  
  3. // ANSWER #1
  4.  
  5. class datareader {
  6.  
  7.     var $string;
  8.     var $type;
  9.     var $query;
  10.     var $result;
  11.    
  12.     function read($string,$query = "") {
  13.         $this->string = $string;
  14.         if (!empty($query)) $this->query = $query;
  15.         $find_type = "/([A-Za-z]+):\/\//";
  16.         preg_match($find_type,$string,$type);
  17.         $this->type = $type[1];
  18.         if ($this->store())
  19.             return true;
  20.         else
  21.             return false;
  22.     }
  23.    
  24.     protected function store() {
  25.         switch($this->type) {
  26.             case "db":
  27.                 $find_type = "/db:\/\/([A-Za-z.0-9]+):([A-Za-z0-9_]+):([A-Za-z0-9]+):([A-Za-z0-9]+):([A-Za-z_0-9]+)/";
  28.                 preg_match($find_type,$this->string,$match);
  29.                 if (mysql_connect($match[1],$match[3],$match[4]))
  30.                     mysql_select_db($match[2]);
  31.                 else
  32.                     die(mysql_error());
  33.                    
  34.                 if (!empty($this->query))
  35.                     $sql = $this->query;
  36.                 else
  37.                     die("No query input");
  38.                
  39.                 $get = mysql_query($this->query);
  40.                
  41.                 if ($this->result = mysql_fetch_array($get))
  42.                     return true;
  43.                 else
  44.                     return false;
  45.                
  46.                 break;
  47.             case "file":
  48.                 $find_type = "/file:\/\//";
  49.                 $replace = preg_replace($find_type,"",$this->string);
  50.                 if (file_exists($replace))
  51.                     $this->result = file($replace);                
  52.                 else
  53.                     die("File ".$replace." not found!");
  54.                
  55.         }
  56.     }
  57.    
  58.     function formatRS() {
  59.         switch($this->type) {
  60.            
  61.             case "db":
  62.                 $return = "<table border=\"1\"><thead><th>#</th><td>Table Name</td></thead>";
  63.                 $c = 0;
  64.                 foreach ($this->result as $row) {
  65.                     $return .= "<tr><td>".$c."</td><td>".$row."</td>";
  66.                     $c++;
  67.                 }
  68.                 $return .= "</table>";
  69.                 break;
  70.             case "file":
  71.                 $return = "<table border=\"1\">";
  72.                 foreach ($this->result as $row) {
  73.                     $tr = explode(",",$row);
  74.                     $return .= "<tr>";
  75.                     foreach ($tr as $td) {
  76.                         $return .= "<td>".$td."</td>";
  77.                     }
  78.                     $return .= "</tr>";
  79.                 }
  80.                 $return .= "</table>";
  81.                 break;
  82.         }
  83.        
  84.         return $return;
  85.    
  86.     }
  87.  
  88. }
  89.  
  90. ?>
  91.  
  92.  
  93. Write a class to read from a database (MySql) table or a flat file and
  94. return the results formatted in a HTML table.
  95.  
  96. The class should work when called using the following code:
  97.       <?php
  98.               $SQL='SHOW TABLES';
  99.               require_once('datareader.class.php');
  100.               $datareader=new datareader();
  101.               $datareader->read('db://localhost:jobtstdb:root::jobtst',$SQL);
  102. //arguments "db://HOST:DBNAME:USER:PASS:TBL",query
  103.  
  104.               echo $datareader->formatRS();
  105.               $datareader->read('file://C:\phpdev\www\csv\test.csv:,');
  106. //arguments "file://FILEPATH:DELIMITER"
  107.               echo $datareader->formatRS();
  108.       ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement