Platon Technologies
neprihlásený Prihlásiť Registrácia
SlovakEnglish
open source software development oslavujeme 10 rokov vývoja otvoreného softvéru! Nedeľa, 29. november 2026
O nás
Magazín
Otvorený softvér
CVS
Služby
Index  »  Projekty  »  phpMyEdit  »  Fórum  »  Advanced Table Join

phpMyEdit Configuration     Advanced Table Join
Odoslať novú tému   Odpovedať na tému    
 
spenconetworks     Založený: 03.06.2009   Príspevky: 6  
Príspevok Zaslal: 2009-06-03 05:45
Návrat hore  Odpovedať s citátom     

Hi guys, I'm working on an events registration system, and I need to get phpMyEdit to show some fields from another table. As it stands, I've searched the web and these forums, and read the docs, but I can't get it to work right. Basically, I have two tables: EVENT_LOG which contains a timestamp and a user_id, and MEMBERS which contains the same user_id and also name, email etc. What I want to do is have phpMyEdit show the first and last names of the user_id it shows in EVENT_LOG. I can do this effectively with a SQL query, but I like using phpMyEdit because it's so clean. Any help is greatly appreciated. Also, I'm a n00b at PHP and phpMyEdit. I've dome some database work but probably not enough for you guys to shake a stick at.

 
doug     Založený: 10.02.2003   Príspevky: 1013   Bydlisko: Denver, Colorado (USA)
Príspevok Zaslal: 2009-06-03 13:47
Návrat hore  Odpovedať s citátom     

Sounds like a simple Table Lookup.

http://opensource.platon.sk/projects/doc.php/phpMyEdit/html/configuration.input-restrictions.html#AEN958

Post some code (it's unlikely anyone is going to write your code for you, but they might spot problems with code you post).

On second thought, if a `user_id` column exists in both tables, it probably takes the "Column joining" code at the above link.

 
spenconetworks     Založený: 03.06.2009   Príspevky: 6  
Príspevok Zaslal: 2009-06-03 15:13
Návrat hore  Odpovedať s citátom     

Thanks for the quick reply. I guess what I'm having trouble with is how phpMyEdit makes use of all these $opts. Basically, I have a standard phpMyEdit script generated for my table, EVENT_LOG. Since it's a log, it's read only. What I picked up from the docs is that some input restrictions are good for when you want to use a table to define what data can be added, so since users won't be adding data to this table, it's the Advanced Table Lookup that actually does a real join. So I guess I need to figure out how to do a real join in phpMyEdit.

My code includes:

Kód:
$opts['fdd']['psu_id'] = array(
  'name'     => 'PSU ID',
  'select'   => 'T',
  'maxlen'   => 9,
  'sort'     => true
);
$opts['fdd']['event_id'] = array(
  'name'     => 'Event ID',
  'select'   => 'T',
  'maxlen'   => 5,
  'sort'     => true
);


so do I want to define another column, first_name, even though it only exists in another table?

Kód:
$opts['fdd']['col_name']['values']['join']
= '$main_table.$main_column = $join_table.$join_column AND '
. '$main_table.another_col  = $join_table.another_col'


Do I place this after I define the first_name column? So I not define the first_name column? And although I know how to join with SQL, the main_table, main_column, join_table, join_column are kinda confusing.

My SQL query that I would use to build a simple table in HTML:

Kód:
SELECT DISTINCT swipe_timestamp, MEMBERS.psu_id, first_name, last_name, event_id FROM EVENT_LOG LEFT JOIN MEMBERS ON MEMBERS.psu_id = EVENT_LOG.psu_id;


Thanks again guys.

 
doug     Založený: 10.02.2003   Príspevky: 1013   Bydlisko: Denver, Colorado (USA)
Príspevok Zaslal: 2009-06-03 15:39
Návrat hore  Odpovedať s citátom     

I am guilty of introducing typo's into my own work from time to time, or getting elements confused. Perhaps the following example will help you figure it out.

The following example was copied from a working script. Two lookups are done in this example.

Kód:
$main_table = 'domain'; // alias of the main table

$join_table = 'contacts'; // alias of the values table

$join_column = 'contact_id'; // join column in the values table

$join_description = 'company'; // description column in the values table

$main_column = 'domain_id'; // join column in the main table

$opts['fdd']['registrar'] = array(
  'default'    => '',
  'input'      => '',
  'maxlen'     => 30,
  'name'       => 'Registrar',
  'options'    => 'ACPVDFL',
  'required'   => false,
  'select'     => '',
  'size|ACP'   => 30,
  'sqlw'       => 'TRIM("$val_as")',
  'sort'       => true,
  'values'     => array(
    'join'     => '$main_table.$main_column = $join_table.$join_column',
    'table'       => 'contacts',
    'column'      => 'contact_id',
    'description' => array(
      'columns'    => array($join_description)
    )
    // ,    'filters'  => 'category = "Registrar"'
  )
);
// MySQL column 8: string - varchar(30)

// ********** lookup for a different field follows

// Join table and main table remain unchanged from the above usage

// Different main column, join column and join description are used for 'owner_id' below

$join_column = 'display_name';

$join_description = 'display_name';

$main_column = 'owner_id';

$opts['fdd']['owner_id'] = array(
  'default'    => '0',
  'input'      => '',
  'maxlen'     => 5,
  'name'       => 'Owner Id',
  'options'    => 'ACPVDFL',
  'required'   => false,
  'select'     => '',
  'size|ACP'   => 5,
  'sqlw'       => 'TRIM("$val_as")',
  'sort'       => true,
  'values'     => array(
    'join'     => '$main_table.$main_column = $join_table.$join_column',
    'table'       => 'contacts',
    'column'      => 'contact_id',
    'description' => array(
      'columns'    => array($join_description)
    )
    // ,   'filters'  => 'category = "Client"'
  )
);
// MySQL column 30: int - smallint(5) unsigned


 
doug     Založený: 10.02.2003   Príspevky: 1013   Bydlisko: Denver, Colorado (USA)
Príspevok Zaslal: 2009-06-03 15:55
Návrat hore  Odpovedať s citátom     

If memory serves me correctly, the join is required when doing lookups between two tables having a common column name (psu_id in your project).

If (instead) psu_id is the column name in on one table and foo_psu_id is the column name in the other table, then with phpMyEdit the "Table lookup" or "Column joining" examples found at the aforementioned documentation link will work OK.

I think the moral of the story is that if lookup columns have dis-similar names when using phpMyEdit, there is less brain damage required to execute simple lookups. Someone please correct me if they disagree.

 
spenconetworks     Založený: 03.06.2009   Príspevky: 6  
Príspevok Zaslal: 2009-06-04 04:30
Návrat hore  Odpovedať s citátom     

Aha! Great, thanks Doug, I've got it joining First Name now:

Kód:
$main_table = 'EVENT_LOG'; // alias of the main table
$join_table = 'MEMBERS'; // alias of the values table
$join_column = 'psu_id'; // join column in the values table
$join_description = 'first_name'; // description column in the values table
$main_column = 'psu_id'; // join column in the main table

$opts['fdd']['psu_id'] = array(
  'default'    => '',
  'input'      => '',
  'maxlen'     => 30,
  'name'       => 'First Name',
  'options'    => 'ACPVDFL',
  'required'   => false,
  'select'     => '',
  'size|ACP'   => 30,
  'sqlw'       => 'TRIM("$val_as")',
  'sort'       => true,
  'values'     => array(
    'join'     => '$main_table.$main_column = $join_table.$join_column',
    'table'       => 'MEMBERS',
    'column'      => 'psu_id',
    'description' => array(
      'columns'    => array($join_description)
    )
    // ,    'filters'  => 'category = "Registrar"'
  )
);


This code works... I'm hoping it follows the conventions, let me know if I've used it wrong.

My questions now are, it seems to be replacing psu_id with first_name, rather than displaying it in an additional column, like a SQL query would do, and along those same lines, I'd like it to display last_name too, but as it stands, I can only get psu_id to be replaced with one join column. If I repeat the code, it complains about duplicates...

Again, thanks for your help! phpMyEdit certainly has a learning curve but it's so powerful! Why would you use anything else? ^_^

 
doug     Založený: 10.02.2003   Príspevky: 1013   Bydlisko: Denver, Colorado (USA)
Príspevok Zaslal: 2009-06-04 04:39
Návrat hore  Odpovedať s citátom     

In a manner of speaking, your $join_description should look more that appearing in "Example 4-18. Complex table lookup example" at the above link.

Kód:
$opts['fdd']['col_name']['values']['description']['columns'][0] = 'name_last';
$opts['fdd']['col_name']['values']['description']['divs'][0]    = ', ';
$opts['fdd']['col_name']['values']['description']['columns'][1] = 'name_first';


Post-initialization examples are sometimes easier to read than cramming everything in nested arrays, a matter of personal preference.

 
spenconetworks     Založený: 03.06.2009   Príspevky: 6  
Príspevok Zaslal: 2009-06-04 05:01
Návrat hore  Odpovedať s citátom     

OK, I got that, but it still insists that I join into an existing field. The examples I found elsewhere show how to create a dummy field:

Kód:
$opts['fdd']['first_name'] = array (
   'name'    => 'First Name',
   'sql'     => 'CONCAT(first_name)',
   'options' => 'VDL',
   'escape'  => false,
   'sort'    => true
);


Which does a good job adding a column, but it seems to rely on my previous posts' code to define first_name. How can I get phpMyEdit to put all that code in my previous post in it's own column? Which would be First Name, which doesn't actually exist in the EVENT_LOG table (the main table.)

 
doug     Založený: 10.02.2003   Príspevky: 1013   Bydlisko: Denver, Colorado (USA)
Príspevok Zaslal: 2009-06-04 05:15
Návrat hore  Odpovedať s citátom     

You're earlier 'description' array is bringing in one column ($join_description). I think you wanted to bring in 2 columns and use a div option to separate them with a space when displayed.

Kód:
'columns' => array($join_description)


Hard to explain this while unable to actively work on the form script and create a valid / known good example. (which I wouldn't have time to do anyway).

 
spenconetworks     Založený: 03.06.2009   Príspevky: 6  
Príspevok Zaslal: 2009-06-04 05:41
Návrat hore  Odpovedať s citátom     

OK I think I'm starting to see where you're coming from. I can't get a hold on how to write the code that pulls two fields into the join_description. Where does this go?

$opts['fdd']['col_name']['values']['description']['columns'][0] = 'first_name';
$opts['fdd']['col_name']['values']['description']['divs'][0] = ', ';
$opts['fdd']['col_name']['values']['description']['columns'][1] = 'last_name';

because if I leave "col-name" I get "Unknown column 'PMEtable0.col_name' in 'field list'" and if I change it to "first_name" I get the same but about first_name.

Sorry if this is simple... I'm sure it is.

 
doug     Založený: 10.02.2003   Príspevky: 1013   Bydlisko: Denver, Colorado (USA)
Príspevok Zaslal: 2009-06-04 13:30
Návrat hore  Odpovedať s citátom     

It's not so simple. See if this works:

Kód:
$opts['fdd']['psu_id'] = array(
  'default'    => '',
  'input'      => '',
  'maxlen'     => 30,
  'name'       => 'First Name',
  'options'    => 'ACPVDFL',
  'required'   => false,
  'select'     => '',
  'size|ACP'   => 30,
  'sqlw'       => 'TRIM("$val_as")',
  'sort'       => true,
  'values'     => array(
    'join'     => '$main_table.$main_column = $join_table.$join_column',
    'table'       => 'MEMBERS',
    'column'      => 'psu_id',
    'description' => array(
      'columns'    => array('first_name', 'last_name'),
      'divs'       => ' '
    )
  )
);


Other examples:

Kód:
$opts['fdd']['col_name'] = array(
  'default'  => 'default',
  'maxlen'   => 10,
  'name'     => 'Title',
  'select'   => 'T',
  'sort'     => true,
  'values'   => array(
    'table' => 'other_table',
    'column' => 'col_name2',
    'description' => array(
       'columns'  => array('0' => 'name', '1' => 'father_name', '2' => 'mother_name'),
       'divs'     => array('0' => ' - ', '1' => ' - '),
     ),
     'orderby'=>'name'
   )
);


Kód:
// Records the `contact_id` from the `contacts` table as `creditor` in the main table.
// Displays a drop down list containing `company` names found in the `contacts` table.
// Displays only those records categorized as a Creditor in the `contacts` table.
// Sorts the list alphabetically.

$opts['fdd']['creditor'] = array(
  'default'    => '',
  'input'      => '',
  'maxlen'     => 6,
  'name'       => 'Creditor',
  'options'    => 'ACPVDFL',
  'required'   => true,
  'select'     => 'D',
  'size|ACP'   => 6,
  'sqlw'       => 'TRIM("$val_as")',
  'sort'       => true,
  'values'   => array(
    'table' => 'contacts',
    'column' => 'contact_id',
    'description'=> array(
       'columns' => array('0' => 'company'),
     ),
    'filters'     => 'category = "Creditor" AND category <> ""',
    'orderby'     => 'company'
   )
);
// MySQL column 1: int - smallint(6)


 
spenconetworks     Založený: 03.06.2009   Príspevky: 6  
Príspevok Zaslal: 2009-06-07 03:11
Návrat hore  Odpovedať s citátom     

Thanks Doug.

Unfortunately, this still does not accomplish what I need. I still cannot find a way to create a dummy field and populate it with join data. So far each method replaces the psu_id data with first_name or last_name or something, but I still need to see psu_id in the view. This example will however, come in handy for filtering input so I thank you for showing me this. I will investigate removing this requirement from the project because it seems near impossible, i've played with it for hours and it won't do it. Unfortunately this is a fairly easy thing to construct with a SQL command, but phpMyEdit doesn't like it. Thanks again!

 
Odoslať novú tému   Odpovedať na tému    

Copyright © 2002-2006 Platon Group
Stránka používa redakčný systém Metafox
Na začiatok · Odkazový formulár · Prihláška
Upozorniť na chybu na PLATON.SK webstránke · Podmienky použitia · Ochrana osobných údajov