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:
CREATE TABLE IF NOT EXISTS `e2c83c3_gallery_albums` (
`id` mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL DEFAULT '',
`description` varchar(255) DEFAULT NULL,
`cat` mediumint(8) UNSIGNED NOT NULL DEFAULT '1',
`datestamp` int(10) UNSIGNED NOT NULL DEFAULT '0',
`order` smallint(5) UNSIGNED NOT NULL,
`access` tinyint(3) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
INSERT INTO `e2c83c3_gallery_albums` (`id`, `name`, `description`, `cat`, `datestamp`, `order`, `access`) VALUES
(1, 'album1', 'album1', 1, 1323334067, 1, 1),
(2, 'album2', 'album2', 1, 1323334078, 2, 1),
(3, 'album3', 'album3', 2, 1323334093, 3, 1),
(4, 'album4', 'album4', 2, 1323334103, 4, 1);
CREATE TABLE IF NOT EXISTS `e2c83c3_gallery_cats` (
`id` mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL DEFAULT '',
`description` varchar(255) DEFAULT NULL,
`datestamp` int(10) UNSIGNED NOT NULL DEFAULT '0',
`order` smallint(5) UNSIGNED NOT NULL,
`access` tinyint(3) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
INSERT INTO `e2c83c3_gallery_cats` (`id`, `name`, `description`, `datestamp`, `order`, `access`) VALUES
(1, 'kat1', 'test', 1323334008, 1, 1),
(2, 'kat2', 'test', 1323334014, 2, 1);
Od strony PHP:
$query = $_pdo->getData('
SELECT ga.`id`, ga.`name`, gc.`name` AS cat_title , gc.`id` AS cat_id FROM [gallery_albums] ga
LEFT JOIN [gallery_cats] gc ON ga.`cat`= gc.`id`
ORDER BY ga.`id`');
if ($_pdo->getRowsCount($query))
{
foreach($query as $row)
{
'name' => $row['cat_title'],
'cities' => array($row['name']) );
}
}
Od strony OPT:
<select name="album" id="album"> {foreach=$albums; cat_title; cat_value}
{foreach=@cat_value.cities; album_value; album_title}
<option value="{@album_value}"{if $photos.album == @album_value} selected="selected"{/if}>{@album_title}
</option> {/foreach}
{/foreach}
Wiem, że błąd leży w PHP, bo idealna tablica do OPT powinna wyglądać tak:
'name' => 'kat1',
1 => 'album1',
2 => 'album2'
)
),
'name' => 'kat2',
1 => 'album3',
2 => 'album4'
)
)
));
Lecz nie potrafię ugryźć tego tak aby taką stworzyć.
Pozdrawiam Rafał