Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP][PDO][OPT] Zgrupowanie pól wyboru
Rafik89
post
Post #1





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 4.12.2009

Ostrzeżenie: (0%)
-----


Witam. Chciałbym stworzyć zgrupowane pola wyboru pobierając dane z bazy z dwóch powiązanych tabel.

Lecz nie wiem jak stworzyć tablicę która odpowiadała by systemowi szablonów OPT 1.1.5.

Od strony bazy danych.

Kategorie:
  1. CREATE TABLE IF NOT EXISTS `e2c83c3_gallery_albums` (
  2. `id` mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT,
  3. `name` varchar(100) NOT NULL DEFAULT '',
  4. `description` varchar(255) DEFAULT NULL,
  5. `cat` mediumint(8) UNSIGNED NOT NULL DEFAULT '1',
  6. `datestamp` int(10) UNSIGNED NOT NULL DEFAULT '0',
  7. `order` smallint(5) UNSIGNED NOT NULL,
  8. `access` tinyint(3) UNSIGNED NOT NULL DEFAULT '0',
  9. PRIMARY KEY (`id`)
  10. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
  11.  
  12.  
  13. INSERT INTO `e2c83c3_gallery_albums` (`id`, `name`, `description`, `cat`, `datestamp`, `order`, `access`) VALUES
  14. (1, 'album1', 'album1', 1, 1323334067, 1, 1),
  15. (2, 'album2', 'album2', 1, 1323334078, 2, 1),
  16. (3, 'album3', 'album3', 2, 1323334093, 3, 1),
  17. (4, 'album4', 'album4', 2, 1323334103, 4, 1);
  18.  
  19. CREATE TABLE IF NOT EXISTS `e2c83c3_gallery_cats` (
  20. `id` mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT,
  21. `name` varchar(100) NOT NULL DEFAULT '',
  22. `description` varchar(255) DEFAULT NULL,
  23. `datestamp` int(10) UNSIGNED NOT NULL DEFAULT '0',
  24. `order` smallint(5) UNSIGNED NOT NULL,
  25. `access` tinyint(3) UNSIGNED NOT NULL DEFAULT '0',
  26. PRIMARY KEY (`id`)
  27. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
  28.  
  29.  
  30. INSERT INTO `e2c83c3_gallery_cats` (`id`, `name`, `description`, `datestamp`, `order`, `access`) VALUES
  31. (1, 'kat1', 'test', 1323334008, 1, 1),
  32. (2, 'kat2', 'test', 1323334014, 2, 1);


Od strony PHP:

  1. $query = $_pdo->getData('
  2. SELECT ga.`id`, ga.`name`, gc.`name` AS cat_title , gc.`id` AS cat_id FROM [gallery_albums] ga
  3. LEFT JOIN [gallery_cats] gc ON ga.`cat`= gc.`id`
  4. ORDER BY ga.`id`');
  5. if ($_pdo->getRowsCount($query))
  6. {
  7. foreach($query as $row)
  8. {
  9. $albums[] = array(
  10. 'name' => $row['cat_title'],
  11. 'cities' => array($row['name'])
  12. );
  13. }
  14. }


Od strony OPT:

  1. <select name="album" id="album">
  2. {foreach=$albums; cat_title; cat_value}
  3. <optgroup label="{@cat_value.name}">
  4. {foreach=@cat_value.cities; album_value; album_title}
  5. <option value="{@album_value}"{if $photos.album == @album_value} selected="selected"{/if}>{@album_title}</option>
  6. {/foreach}
  7. {/foreach}
  8. </select>


Wiem, że błąd leży w PHP, bo idealna tablica do OPT powinna wyglądać tak:

  1. 1 => array(
  2. 'name' => 'kat1',
  3. 'cities' => array(
  4. 1 => 'album1',
  5. 2 => 'album2'
  6. )
  7. ),
  8. 2 => array(
  9. 'name' => 'kat2',
  10. 'cities' => array(
  11. 1 => 'album3',
  12. 2 => 'album4'
  13. )
  14. )
  15. ));


Lecz nie potrafię ugryźć tego tak aby taką stworzyć.

Pozdrawiam Rafał
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 24.08.2025 - 23:15