Guest User

Untitled

a guest
Feb 25th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. CAT ID PARENT_ID
  2. ------+----------
  3. 1 0
  4. 2 0
  5. 3 1
  6. 4 1
  7. 5 3
  8. 6 2
  9.  
  10. Cat 1
  11. Subcat1
  12. SubCat2
  13. SubSubcat1
  14. Cat 2
  15. Subcat1
  16.  
  17. <?php
  18. require_once 'config.php';
  19.  
  20. ?>
  21. <?php header('Content-Type: text/html; charset=UTF-8'); ?>
  22. <?php
  23.  
  24.  
  25.  
  26. $username = "qzd708";
  27. $password = "aR689193";
  28. $hostname = "qzd708.laclonadora.com";
  29. $databasename = "qzd708";
  30. $parent_id=0;
  31. $salida=" ";
  32. $mysqli = new mysqli($hostname , $username, $password, $databasename);
  33. $ruta="";
  34.  
  35.  
  36. //connect to mysql and select db
  37. $conn = mysqli_connect($hostname , $username, $password, $databasename);
  38.  
  39. if( !empty($conn->connect_errno)) {die("Error " . mysqli_error($conn));}
  40.  
  41. echo "<select>";
  42. echo "<option value='' disabled selected>--Seleccione Categoría--</option>";
  43.  
  44. //call the recursive function to print category listing
  45. category_tree(0);
  46.  
  47. echo "</select>";
  48. //
  49. //echo gettheparent(168);
  50. //Recursive php function
  51.  
  52. function category_tree($catid){
  53.  
  54. global $conn;
  55. $sql = "select * from oc_category where parent_id ='".$catid."'";
  56. $result = $conn->query($sql);
  57.  
  58. while($row = mysqli_fetch_object($result)):
  59. $i = 0;
  60. //$ruta=gettheparent(168);
  61. if ($i == 0) {
  62.  
  63. echo "<option value='". $row->category_id;echo "'>". gettheparent ($row->category_id) . getthename($row->category_id);
  64. category_tree($row->category_id);
  65. echo "</option>";
  66.  
  67. $i++;
  68.  
  69. }
  70. if ($i > 0) {
  71. //echo '</ul>';
  72. //$ruta=gettheparent($catid);
  73.  
  74.  
  75.  
  76. }
  77.  
  78. endwhile;
  79.  
  80. }
  81.  
  82. function category_tree_li($catid){
  83. global $conn;
  84.  
  85. $sql = "select * from oc_category where parent_id ='".$catid."'";
  86. $result = $conn->query($sql);
  87.  
  88. while($row = mysqli_fetch_object($result)):
  89. $i = 0;
  90. if ($i == 0) {
  91. echo '<ul>';
  92. echo '<li>' . $row->category_id;
  93. category_tree_li($row->category_id);
  94. echo '</li>';
  95. $i++;
  96. }
  97. if ($i > 0) {
  98. echo '</ul>';
  99.  
  100. }
  101.  
  102. endwhile;
  103.  
  104. }
  105.  
  106. //category_tree_li(0);
  107. //close the connection
  108. mysqli_close($conn);
  109.  
  110. function getthename ($category_id){
  111. if ($category_id !=0) {
  112. $username = "qzd708";
  113. $password = "aR689193";
  114. $hostname = "qzd708.laclonadora.com";
  115. $databasename = "qzd708";
  116. $mysqli = new mysqli($hostname , $username, $password, $databasename);
  117. if (mysqli_connect_errno()) {
  118. printf("Connect failed: %sn", mysqli_connect_error());
  119. exit();
  120. }
  121. $query = "SELECT name FROM oc_category_description WHERE category_id = '".$category_id."'";
  122. $result = $mysqli->query($query);
  123. while($row = $result->fetch_array())
  124. {
  125. $rows[] = $row;
  126. }
  127. foreach($rows as $row)
  128. {
  129. $salida.=" - ";
  130. $salida.= utf8_encode($row["name"]);
  131. return $salida;
  132. }
  133. $result->close();
  134. $mysqli->close();
  135. }
  136.  
  137. }
Add Comment
Please, Sign In to add comment