1
zite83
Object handler combind multipal tables data
  • 2009/9/12 23:25

  • zite83

  • Just popping in

  • Posts: 41

  • Since: 2004/10/5


I need some help, I read the topic on how can i create and use XOOPS object handler in my module? and played around with that, and just love it! No more SQL statements! The one thing I can't figure out is how to join two tables data or if there is another route I can take.

Here's an example of what I want to do


table one "departments"

id - name

1 - hardware
2 - sporting-goods
3 - auto
4 - produce
5 - deli

table two "employees"

id - dept_id - name

1 - 2 - Joe
2 - 2 - Bob
3 - 2 - Susan
4 - 4 - Greg
5 - 4 - Jim
6 - 3 - Frank
7 - 1 - Jill
8 - 1 - Brian
9 - 5 - Kate
10 - 5 - Matt


Now my template will show each department and under each department would be its employees.

<{foreach item=department from=$departments}>

<{
department.name}>

<{foreach 
item=employee from=$employees}>

<{
$employee.name}>

<{/foreach}>

<{/foreach}>


I don't know if that is even the route to go, but any help would be great.

2
trabis
Re: Object handler combind multipal tables data
  • 2009/9/13 0:52

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


Easy way is to get a list of departments and get a list of employees, then you loop the list of departments and append the corresponding employees
idea:
foreach ($departments as $id => $department) {
   
$departments[$id]['employees'] = array();
   foreach(
$employees as $employee) {
      if (
$employee['dept_id'] == $id) {
         
$departments[$id]['employees'][] = $employee;
      }
   }
}


Then you use smarty:
<{foreach item=department from=$departments}>

<{
department.name}>

<{foreach 
item=employee from=$department.employees}>

<{
$employee.name}>

<{/foreach}>

<{/foreach}>



3
ghia
Re: Object handler combind multipal tables data
  • 2009/9/13 0:57

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


You are rushing a bit the things with your template, because there is no link from employees to departments and with your two lists the employee list will be the same and repeated for each department.
You can have only one list in the template, that means that you call your template for each department:
<{departmentname}>

<{foreach 
item=employee from=$employees}>

<{
$employee.name}>

<{/foreach}>

In PHP you call your handler to get all departments eg sorted by name.
Then for each department you call
- the handler for employees and limit by the departmentnummer and order by name.
- assign the departmentname to the template and also the employees and show it.

Other possibility is to make one list of all the employees with the departmentname added (combining the getall arrays of the two handlers) and ordered (array sort) by departmentname and employeename.
Your template could then be something like this:
<{assign var=dpt_name value=''}>
<{foreach 
item=employee from=$employees}>
<{if 
$employee.departmentname != $dpt_name}>
<{
$employee.departmentname}>
<{
assign var=dpt_name value=$employee.departmentname}>
<{/if}>
<{
$employee.name}>

<{/foreach}>

4
zite83
Re: Object handler combind multipal tables data
  • 2009/9/13 4:13

  • zite83

  • Just popping in

  • Posts: 41

  • Since: 2004/10/5


My brain hurts some what, but I got it working! Thanks so much!

Like I stated the example I gave was just an example, what I'm really making is a Raid Progression module for our guild website. I've seen many addons on other CMS' like it, but since XOOPS dosn't have one, I would just make it myself.


//Append each boss to instance array where the raid_lid matches the raid_id
foreach ($instances as $id => $instance_id){
    foreach (
$bosses as $boss_id){
        if (
$instance_id['raid_id'] == $boss_id['raid_lid']){
                
$instances[$id]['bosses'][] = $boss_id;
        }
    }
}


Now I just need to do abit of styling of course the screen shot is just a test.

Resized Image


Yes I'm World of Warcraft nerd and I will be making a few modules for XOOPS based around World of Warcraft Guilds.

5
sailjapan
Re: Object handler combind multipal tables data

Looking forward to seeing them!

Login

Who's Online

245 user(s) are online (161 user(s) are browsing Support Forums)


Members: 0


Guests: 245


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: May 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits