Guest User

Untitled

a guest
Jan 17th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. <?php
  2. /**
  3. * String store, using sqlite & PDO to store and search your strings.
  4. */
  5. class string_store{
  6. private $db;
  7.  
  8. function __construct($dsn,$setup_query){
  9. $this->dsn = $dsn;
  10. $this->setup = $setup_query;
  11. $this->chkSetup();
  12. }
  13. /**
  14. * Connect
  15. */
  16. public function connect(){
  17. try{
  18. if (!$this->db instanceof PDO){
  19. $this->db = new PDO($this->dsn);
  20. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  21. $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  22. $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
  23. }
  24. }catch (Exception $e){
  25. die($e->getMessage());
  26. }
  27. }
  28. /**
  29. * Search store for matching words
  30. *
  31. * @param string $string
  32. * @return array
  33. */
  34. public function search($string){
  35. $this->connect();
  36. $words = explode(' ',$string);
  37. $sql = "SELECT * FROM strings WHERE ";
  38. $w = array();
  39. foreach($words as $word){
  40. $w[] = '%'.$word.'%';
  41. $sql .= "string LIKE ? OR ";
  42. }
  43. $sql = rtrim($sql,'OR ');
  44.  
  45. $statement = $this->db->prepare($sql);
  46. $statement->execute($w);
  47. return $statement->fetchAll(PDO::FETCH_ASSOC);
  48. }
  49.  
  50. /**
  51. * Insert strings into the store
  52. *
  53. * @param array $values
  54. */
  55. public function insert($values){
  56. $this->connect();
  57. $fieldnames = array_keys($values[0]);
  58. $fields = '('.implode(' ,',$fieldnames).')';
  59. $bounds = '(:'.implode(', :',$fieldnames).')';
  60. $sql = "INSERT INTO strings {$fields} VALUES {$bounds}";
  61. $statement = $this->db->prepare($sql);
  62. foreach($values as $vals){
  63. $statement->execute($vals);
  64. }
  65. }
  66.  
  67. /**
  68. * Setup and Create table for store
  69. */
  70. function chkSetup(){
  71. $dso = explode(':',$this->dsn);
  72. if(file_exists($dso[1])){
  73. return;
  74. }else{
  75. $this->connect();
  76. //Setup Table
  77. if(is_array($this->setup)){
  78. foreach($this->setup as $table){
  79. $this->db->query($table);
  80. }
  81. }else{
  82. $this->db->query($this->setup);
  83. }
  84. exit(header("refresh:0;url=./"));
  85. }
  86. }
  87. }//End Class
  88. ?>
  89.  
  90. <?php
  91. include('./string_store.php');
  92. $string_store_table = array("CREATE TABLE strings ( id INTEGER PRIMARY KEY,
  93. string TEXT);");
  94.  
  95. $string_store = new string_store("sqlite:./strings.db", $string_store_table);
  96.  
  97. /* Insert your strings into the store
  98. $listofsearhches = array(array('string'=>'Java programmer is a good boy'),
  99. array('string'=>'he plays ball at times'),
  100. array('string'=>'java and dogs hate each other'),
  101. array('string'=>'dogs are not java programmers'),
  102. );
  103. $string_store->insert('strings',$listofsearhches);
  104. */
  105.  
  106. $search = "Java programmer"; //GET INPUT FROM USER
  107.  
  108. $result = $string_store->search($search);
  109.  
  110. print_r($result);
  111. /* Result
  112. Array
  113. (
  114. [0] => Array
  115. (
  116. [id] => 1
  117. [string] => Java programmer is a good boy
  118. )
  119.  
  120. [1] => Array
  121. (
  122. [id] => 3
  123. [string] => java and dogs hate each other
  124. )
  125.  
  126. [2] => Array
  127. (
  128. [id] => 4
  129. [string] => dogs are not java programmers
  130. )
  131.  
  132. )
  133. */
  134. ?>
Add Comment
Please, Sign In to add comment