Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: optgroup zasdy tworzenie kategori i podkategori
Forum PHP.pl > Forum > PHP
slawekxx
Witam,

znalazłem skrypt w necie , ale nie rozumień tu taj powiązań pomiędzy kategorią a podkategorią

tabela i przykładowe dane
  1. CREATE TABLE `testcat` (
  2. `id` mediumint(8) unsigned NOT NULL auto_increment,
  3. `title` varchar(200) NOT NULL,
  4. `left_id` int(10) NOT NULL,
  5. `right_id` int(10) NOT NULL,
  6. `level` mediumint(8) unsigned NOT NULL,
  7. UNIQUE KEY `id` (`id`)
  8. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;
  9.  
  10. --
  11. -- Dumping data for table `testcat`
  12. --
  13.  
  14. INSERT INTO `testcat` VALUES (1, 'Digital Art', 9, 20, 1);
  15. INSERT INTO `testcat` VALUES (2, '3D Art', 10, 11, 2);
  16. INSERT INTO `testcat` VALUES (3, 'Drawings', 12, 13, 2);
  17. INSERT INTO `testcat` VALUES (4, 'Miscellaneous', 14, 15, 2);
  18. INSERT INTO `testcat` VALUES (5, 'Photo Manipulation', 16, 17, 2);
  19. INSERT INTO `testcat` VALUES (6, 'Wallpapers', 18, 19, 2);
  20. INSERT INTO `testcat` VALUES (7, 'Internet Use', 20, 25, 2);
  21. INSERT INTO `testcat` VALUES (8, 'Signatures', 21, 22, 3);
  22. INSERT INTO `testcat` VALUES (9, 'Avatars', 23, 24, 3);
  23. INSERT INTO `testcat` VALUES (10, 'Photography', 1, 8, 1);
  24. INSERT INTO `testcat` VALUES (11, 'Landscape', 6, 7, 2);
  25. INSERT INTO `testcat` VALUES (12, 'Portrait', 4, 5, 2);
  26. INSERT INTO `testcat` VALUES (13, 'Architecture', 2, 3, 2);


i skrypt

  1. <?php
  2. $sql = 'SELECT id, left_id, right_id, level, title FROM testcat ORDER BY left_id asc';
  3. $result = mysql_query($sql) or die(mysql_error());
  4. $level = 1;
  5. $classes = array('cat' => array(1 => 'maincat', 'subcat'), 'element' => array(1 => 'sub', 'subcatsub'));
  6. $first = false;
  7.  
  8. print '<select class="select" name="id">' . "\n";
  9. while($row = mysql_fetch_assoc($result))
  10. {
  11. while($level > $row['level'])
  12. {
  13. $level--;
  14. print str_repeat("\t", $level) . '</optgroup>' . "\n";
  15. }
  16. if(has_children($row['left_id'], $row['right_id']))
  17. {
  18. print str_repeat("\t", $row['level']) . '<optgroup class="' . $classes['cat'][$row['level']] . '" label="' . $row['title'] . '">' . "\n";
  19. $level++;
  20. $first = true;
  21. continue;
  22. }
  23. print str_repeat("\t", $row['level']) . '<option class="' . $classes['element'][$row['level']] . (($first) ? ' first' : '') . '" value="' . $row['id'] . '">' . $row['title'] . '</option>' . "\n";
  24. $first = false;
  25. }
  26. while($level > 1)
  27. {
  28. $level--;
  29. print str_repeat("\t", $level) . '</optgroup>' . "\n";
  30. }
  31.  
  32. print '</select>' . "<br />\n";
  33.  
  34. function has_children($left_id, $right_id)
  35. { // MPTT is good!
  36. return (bool) ( ($right_id - $left_id - 1) / 2 );
  37. } <?php
  38. $sql = 'SELECT id, left_id, right_id, level, title FROM testcat ORDER BY left_id asc';
  39. $result = mysql_query($sql) or die(mysql_error());
  40. $level = 1;
  41. $classes = array('cat' => array(1 => 'maincat', 'subcat'), 'element' => array(1 => 'sub', 'subcatsub'));
  42. $first = false;
  43.  
  44. print '<select class="select" name="id">' . "\n";
  45. while($row = mysql_fetch_assoc($result))
  46. {
  47. while($level > $row['level'])
  48. {
  49. $level--;
  50. print str_repeat("\t", $level) . '</optgroup>' . "\n";
  51. }
  52. if(has_children($row['left_id'], $row['right_id']))
  53. {
  54. print str_repeat("\t", $row['level']) . '<optgroup class="' . $classes['cat'][$row['level']] . '" label="' . $row['title'] . '">' . "\n";
  55. $level++;
  56. $first = true;
  57. continue;
  58. }
  59. print str_repeat("\t", $row['level']) . '<option class="' . $classes['element'][$row['level']] . (($first) ? ' first' : '') . '" value="' . $row['id'] . '">' . $row['title'] . '</option>' . "\n";
  60. $first = false;
  61. }
  62. while($level > 1)
  63. {
  64. $level--;
  65. print str_repeat("\t", $level) . '</optgroup>' . "\n";
  66. }
  67.  
  68. print '</select>' . "<br />\n";
  69.  
  70. function has_children($left_id, $right_id)
  71. { // MPTT is good!
  72. return (bool) ( ($right_id - $left_id - 1) / 2 );
  73. }
Pyton_000
http://en.wikipedia.org/wiki/Nested_set_model
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.