J'ai le tableau suivant.Pouvez-vous utiliser JOIN dans une fonction récursive PHP/MySQL?
CREATE TABLE IF NOT EXISTS `omc_schedule` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`trainer_id` int(11) NOT NULL,
`course` varchar(255) NOT NULL,
`capacity` int(11) NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`parentid` int(10) NOT NULL,
`order` int(11) NOT NULL,
`booked` int(5) NOT NULL,
`type` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ...
Je peux utiliser la fonction suivante pour générer un arbre de tableau.
function generateTree(&$tree, $parentid = 0) {
$this->db->select('*');
$this->db->where ('parentid',$parentid);
$this->db->where ('active','1');
$this->db->order_by('order asc, parentid asc');
$res = $this->db->get('omc_schedule');
if ($res->num_rows() > 0) {
foreach ($res->result_array() as $r) {
// push found result onto existing tree
$tree[$r['id']] = $r;
// create placeholder for children
$tree[$r['id']]['children'] = array();
// find any children of currently found child
$this->generateTree($tree[$r['id']]['children'],$r['id']);
}
}
$res->free_result();
return $tree;
}
Maintenant, je veux rejoindre une table d'entraîneur pour obtenir le nom de l'entraîneur.
CREATE TABLE IF NOT EXISTS `omc_trainer` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`trainer_name` varchar(50) NOT NULL,
`trainer_image` varchar(100) NOT NULL,
`video_url` text NOT NULL,
`desc` text NOT NULL,
PRIMARY KEY (`id`)
)
Maintenant, j'essayé de rejoindre de différentes manières, mais je ne suis pas en mesure d'obtenir le nom de formateur dans chaque tableau.
Est-ce que je peux utiliser JOIN pour obtenir le nom d'un formateur dans une fonction récursive?
La fonction ci-dessus est CodeIgniter, mais ignorez-la.
Merci d'avance.