Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: get_foreign.lib.php,v 2.10 2006/01/17 17:02:30 cybot_tm Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4  
5  
6 /**
7 * Gets foreign keys in preparation for a drop-down selector
8 * Thanks to <markus@noga.de>
9 */
10  
11  
12 // lem9: we always show the foreign field in the drop-down; if a display
13 // field is defined, we show it besides the foreign field
14 $foreign_link = false;
15 if ($foreigners && isset($foreigners[$field])) {
16 $foreigner = $foreigners[$field];
17 $foreign_db = $foreigner['foreign_db'];
18 $foreign_table = $foreigner['foreign_table'];
19 $foreign_field = $foreigner['foreign_field'];
20  
21 // Count number of rows in the foreign table. Currently we do
22 // not use a drop-down if more than 200 rows in the foreign table,
23 // for speed reasons and because we need a better interface for this.
24 //
25 // We could also do the SELECT anyway, with a LIMIT, and ensure that
26 // the current value of the field is one of the choices.
27  
28 $the_total = PMA_countRecords($foreign_db, $foreign_table, TRUE);
29  
30 if ((isset($override_total) && $override_total == true) || $the_total < $cfg['ForeignKeyMaxLimit']) {
31 // foreign_display can be FALSE if no display field defined:
32 $foreign_display = PMA_getDisplayField($foreign_db, $foreign_table);
33  
34 $f_query_main = 'SELECT ' . PMA_backquote($foreign_field)
35 . (($foreign_display == FALSE) ? '' : ', ' . PMA_backquote($foreign_display));
36 $f_query_from = ' FROM ' . PMA_backquote($foreign_db) . '.' . PMA_backquote($foreign_table);
37 $f_query_filter = empty($foreign_filter) ? '' : ' WHERE ' . PMA_backquote($foreign_field)
38 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
39 . (($foreign_display == FALSE) ? '' : ' OR ' . PMA_backquote($foreign_display)
40 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
41 );
42 $f_query_order = ($foreign_display == FALSE) ? '' :' ORDER BY ' . PMA_backquote($foreign_table) . '.' . PMA_backquote($foreign_display);
43 $f_query_limit = isset($foreign_limit) ? $foreign_limit : '';
44  
45 if (!empty($foreign_filter)) {
46 $res = PMA_DBI_query('SELECT COUNT(*)' . $f_query_from . $f_query_filter);
47 if ($res) {
48 $the_total = PMA_DBI_fetch_value($res);
49 @PMA_DBI_free_result($res);
50 } else {
51 $the_total = 0;
52 }
53 }
54  
55 $disp = PMA_DBI_query($f_query_main . $f_query_from . $f_query_filter . $f_query_order . $f_query_limit);
56 if ($disp) {
57 // garvin: If a resultset has been created, pre-cache it in the $disp_row array
58 // This helps us from not needing to use mysql_data_seek by accessing a pre-cached
59 // PHP array. Usually those resultsets are not that big, so a performance hit should
60 // not be expected.
61 $disp_row = array();
62 while ($single_disp_row = @PMA_DBI_fetch_assoc($disp)) {
63 $disp_row[] = $single_disp_row;
64 }
65 @PMA_DBI_free_result($disp);
66 }
67 } else {
68 unset($disp_row);
69 $foreign_link = true;
70 }
71 } // end if $foreigners
72  
73 ?>