Advertisement
Guest User

Untitled

a guest
Jun 7th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. <?php
  2. /*
  3. * Mysql database class - only one connection alowed
  4. */
  5. class Database {
  6. private $_connection;
  7. private static $_instance; //The single instance
  8. private $_host = "localhost";
  9. private $_username = "yourdatabaseusername";
  10. private $_password = "yourdatabasepass";
  11. private $_database = "databasename";
  12. /*
  13. Get an instance of the Database
  14. @return Instance
  15. */
  16. public static function getInstance() {
  17. if(!self::$_instance) { // If no instance then make one
  18. self::$_instance = new self();
  19. }
  20. return self::$_instance;
  21. }
  22. // Constructor
  23. private function __construct() {
  24. $this->_connection = new mysqli($this->_host, $this->_username,
  25. $this->_password, $this->_database);
  26.  
  27. // Error handling
  28. if(mysqli_connect_error()) {
  29. trigger_error("Failed to conencto to MySQL: " . mysql_connect_error(),
  30. E_USER_ERROR);
  31. }
  32. }
  33. // Magic method clone is empty to prevent duplication of connection
  34. private function __clone() { }
  35. // Get mysqli connection
  36. public function getConnection() {
  37. return $this->_connection;
  38. }
  39. }
  40. ?>
  41.  
  42. <?php
  43. include('database_connection.php');
  44. $db = Database::getInstance();
  45. $mysqli = $db->getConnection();
  46. $sql_query = "SELECT * FROM `eav_attribute_option` WHERE `attribute_id` =137";// 137 my size attribute id
  47. $result = $mysqli->query($sql_query);
  48.  
  49. if ($result->num_rows > 0) {
  50. // output data of each row
  51. while($row = $result->fetch_assoc()) {
  52. // echo "id: " . $row["option_id"]. " - Name: " . $row["attribute_id"]. "<br>";
  53. $sizes_value_id[]=$row["option_id"];//adding array all option_id
  54. }
  55. } else {
  56. echo "0 results";
  57. }
  58. //add a query all value informations
  59. foreach($sizes_value_id as $size){
  60. $sql_query = "SELECT * FROM `eav_attribute_option_value` WHERE `option_id`='.$size.'";
  61. $result = $mysqli->query($sql_query);
  62. if ($result->num_rows > 0) {
  63. // output data of each row
  64. while($row = $result->fetch_assoc()) {
  65. echo "id: " . $row["option_id"]. " - Name: " . $row["value"]. "<br>";
  66. $sizes_value[]=$row["value"];//adding all values to an array
  67. }
  68. } else {
  69. echo "0 results";
  70. }
  71. }
  72. // for understand what we have
  73. echo "entity_id=".$sizes_value_id[0]."Value=".$sizes_value[0];
  74. // you can use same logics for other steps
  75.  
  76. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement