leolim
Joined: 26 Sep 2007 Posts: 1
|
Posted: 2007-09-26 01:32 Post subject: The PHP Class help |
|
|
I have found a source from platon.sk that help save my time a lot. now what i want to use the header function to call another page. what the source did is they use the class function which like below:
this is my page1.php
Code: |
<html>
<head>
<title>My Data</title>
</head>
<body>
<?php
// MySQL host name, user name, password, database, and table
$opts['hn'] = 'localhost';
$opts['un'] = 'root';
$opts['pw'] = '';
$opts['db'] = 'school';
$opts['tb'] = 'employee_data';
// Name of field which is the unique key
$opts['key'] = 'idnum';
// Type of key field (int/real/string/date etc.)
$opts['key_type'] = 'string';
// Sorting field(s)
$opts['sort_field'] = array('idnum');
// Number of records to display on the screen
// Value of -1 lists all records in a table
$opts['inc'] = 15;
// Options you wish to give the users
// A - add, C - change, P - copy, V - view, D - delete,
// F - filter, I - initial sort suppressed Q-Convert
$opts['options'] = 'AQ';
// Number of lines to display on multiple selection filters
$opts['multiple'] = '4';
// Navigation style: B - buttons (default), T - text links, G - graphic links
// Buttons position: U - up, D - down (default)
$opts['navigation'] = 'DB';
// Display special page elements
$opts['display'] = array(
'form' => true,
'query' => true,
'sort' => true,
'time' => true,
'tabs' => true
);
// Set default prefixes for variables
$opts['js']['prefix'] = 'PME_js_';
$opts['dhtml']['prefix'] = 'PME_dhtml_';
$opts['cgi']['prefix']['operation'] = 'PME_op_';
$opts['cgi']['prefix']['sys'] = 'PME_sys_';
$opts['cgi']['prefix']['data'] = 'PME_data_';
/* Get the user's default language and use it if possible or you can
specify particular one you want to use. Refer to official documentation
for list of available languages. */
$opts['language'] = 'EN-US';
$opts['fdd']['idnum'] = array(
'name' => 'ID NUM',
'select' => 'T',
'maxlen' => 16,
'sort' => true
);
$opts['fdd']['Name'] = array(
'name' => 'NAME',
'select' => 'T',
'maxlen' => 10,
'default' => '0000-00-00',
'sort' => true
);
$opts['fdd']['Age'] = array(
'name' => 'AGE',
'select' => 'T',
'maxlen' => 16,
'sort' => true
);
// Now important call to phpMyEdit
require_once 'phpMyEdit.class.php';
new phpMyEdit($opts);
?>
</body>
</html> |
and in the phpMyEdit.class.php
Code: |
<?php
class phpMyEdit
{
var $operation; // operation to do: Add, Change, Delete
var $page_types = array(
'L' => 'list',
'F' => 'filter',
'A' => 'add',
'Q' => 'convert'
);
var $default_buttons = array(
'L' => array('<<','<','add','view','change','copy','delete','convert','>','>>','goto','goto_combo'),
'F' => array('<<','<','add','view','change','copy','delete','convert','>','>>','goto','goto_combo'),
'A' => array('save','more','cancel'),
'Q' => array('save','more','cancel')
);
function nav_buttons() { return stristr($this->navigation, 'B'); }
function nav_up() { return (stristr($this->navigation, 'U') && !($this->buttons[$this->page_type]['up'] === false)); }
function nav_down() { return (stristr($this->navigation, 'D') && !($this->buttons[$this->page_type]['down'] === false)); }
/*
* functions for indicating whether operations are enabled
*/
function add_enabled() { return stristr($this->options, 'A'); }
function convert_enabled(){ return stristr($this->options, 'Q'); }
function filter_enabled() { return stristr($this->options, 'F'); }
function tabs_enabled() { return $this->display['tabs'] && count($this->tabs) > 0; }
function hidden($k) { return stristr($this->fdd[$k]['input'],'H'); }
function password($k) { return stristr($this->fdd[$k]['input'],'W'); }
function readonly($k) { return stristr($this->fdd[$k]['input'],'R') || $this->virtual($k); }
function add_operation() { return $this->operation == $this->labels['Add'] && $this->add_enabled(); }
function convert_operation() { return $this->operation == $this->labels['Convert'] && $this->convert_enabled(); }
function filter_operation() { return $this->fl && $this->filter_enabled() && $this->list_operation(); }
function list_operation() { /* covers also filtering page */ return ! $this->convert_operation()
&& ! $this->add_operation(); }
function add_canceled() { return $this->canceladd == $this->labels['Cancel']; }
function displayed($k) /* {{{ */
{
return
($this->add_operation() && stristr($options, 'A')) ||
($this->convert_operation() && stristr($options, 'Q')) ||
($this->filter_operation() && stristr($options, 'F')) ||
($this->list_operation() && stristr($options, 'L'));
} /* }}} */
function myquery($qry, $line = 0, $debug = 0) /* {{{ */
{
if (isset($this->db)) {
$ret = @mysql_db_query($this->db, $qry, $this->dbh);
}
return $ret;
} /* }}} */
function make_language_labels($language) /* {{{ */
{
if (! file_exists($file)) {
// try the language w/o variant
$file = $this->dir['lang'].'PME.lang.'.substr($language,0,2).'.inc';
}
$ret = @include($file);
if (! is_array($ret)) {
return $ret;
}
return $ret;
} /* }}} */
function set_values($field_num, $prepend = null, $append = null, $strict = false) /* {{{ */
{
return (array) $prepend + (array) $this->fdd[$field_num]['values2']
+ (isset($this->fdd[$field_num]['values']['table']) || $strict
? $this->set_values_from_table($field_num, $strict)
: array())
+ (array) $append;
} /* }}} */
function display()
{
header("Location:convert.php");
}
function get_SQL_query($parts) /* {{{ */
{
foreach ($parts as $k => $v) {
$parts[$k] = trim($parts[$k]);
}
switch ($parts['type']) {
case 'select':
$ret = 'SELECT ';
if ($parts['DISTINCT'])
$ret .= 'DISTINCT ';
$ret .= $parts['select'];
$ret .= ' FROM '.$parts['from'];
if ($parts['where'] != '')
$ret .= ' WHERE '.$parts['where'];
if ($parts['groupby'] != '')
$ret .= ' GROUP BY '.$parts['groupby'];
if ($parts['having'] != '')
$ret .= ' HAVING '.$parts['having'];
if ($parts['orderby'] != '')
$ret .= ' ORDER BY '.$parts['orderby'];
if ($parts['limit'] != '')
$ret .= ' LIMIT '.$parts['limit'];
if ($parts['procedure'] != '')
$ret .= ' PROCEDURE '.$parts['procedure'];
break;
default:
die('unknown query type');
break;
}
return $ret;
} /* }}} */
function get_SQL_join_clause() /* {{{ */
{
$main_table = 'PMEtable0';
$join_clause = $this->tb." AS $main_table";
return $join_clause;
} /* }}} */
function get_SQL_where_from_query_opts($qp = null, $text = 0) /* {{{ */
{
if ($qp == null) {
$qp = $this->query_opts;
}
$where = array();
return ''; /* empty string */
} /* }}} */
function form_begin() /* {{{ */
{
$page_name = htmlspecialchars($this->page_name);
if ($this->add_operation()) {
$first_required = true;
for ($k = 0; $k < $this->num_fds; $k++) {
if ($this->displayed[$k] && ! $this->readonly($k) && ! $this->hidden($k)
&& ($this->fdd[$k]['js']['required'] || isset($this->fdd[$k]['js']['regexp']))) {
if ($first_required) {
$first_required = false;
echo '<script type="text/javascript"><!--',"\n";
echo '
function '.$this->js['prefix'].'trim(str)
{
while (str.substring(0, 1) == " "
|| str.substring(0, 1) == "\\n"
|| str.substring(0, 1) == "\\r")
{
str = str.substring(1, str.length);
}
while (str.substring(str.length - 1, str.length) == " "
|| str.substring(str.length - 1, str.length) == "\\n"
|| str.substring(str.length - 1, str.length) == "\\r")
{
str = str.substring(0, str.length - 1);
}
return str;
}
function '.$this->js['prefix'].'form_control(theForm)
{',"\n";
}
}
}
if (! $first_required) {
echo '
return true;
}',"\n\n";
echo '// --></script>', "\n";
}
}
if ($this->display['form']) {
echo '<form class="',$this->getCSSclass('form'),'" method="POST"';
echo ' action="',$page_name,'" name="'.$this->cgi['prefix']['sys'].'form">',"\n";
}
return true;
} /* }}} */
function display_add_record() /* {{{ */
{
echo "<h1> DISPLAY_ADD RECORD</h1>";
} /* }}} */
function getCSSclass($name, $position = null, $divider = null, $postfix = null) /* {{{ */
{
} /* }}} */
/**
* Returns field cell HTML attributes
*/
function htmlSubmit($name, $label, $css_class_name, $js_validation = true, $disabled = false, $js = NULL) /* {{{ */
{
// Note that <input disabled> isn't valid HTML, but most browsers support it
$markdisabled = $disabled ? ' disabled' : '';
$ret = '<input'.$markdisabled.' type="submit" class="'.$css_class_name
.'" name="'.$this->cgi['prefix']['sys'].ltrim($markdisabled).$name
.'" value="'.(isset($this->labels[$label]) ? $this->labels[$label] : $label);
if ($js_validation) {
$ret .= '" onClick="return '.$this->js['prefix'].'form_control(this.form);';
}
$ret .='"';
if(isset($js)) $ret .= ' '.$js;
$ret .= '>';
return $ret;
} /* }}} */
function get_sys_cgi_var($name, $default_value = null) /* {{{ */
{
if (isset($this)) {
return $this->get_cgi_var($this->cgi['prefix']['sys'].$name, $default_value);
}
return phpMyEdit::get_cgi_var(phpMyEdit::get_default_cgi_prefix('sys').$name, $default_value);
} /* }}} */
function get_cgi_var($name, $default_value = null) /* {{{ */
{
if (! isset($var)) {
global $HTTP_POST_VARS;
$var = @$HTTP_POST_VARS[$name];
}
return $var;
} /* }}} */
function display_list_table_buttons($position) /* {{{ */
{
if (($but_str = $this->display_buttons($position)) === null)
return;
if($position == 'down') echo '<hr class="'.$this->getCSSclass('hr', 'down').'">'."\n";
echo '<table class="',$this->getCSSclass('navigation', $position),'">',"\n";
echo '<tr class="',$this->getCSSclass('navigation', $position),'">',"\n";
echo '<td class="',$this->getCSSclass('buttons', $position),'">',"\n";
echo $but_str,'</td>',"\n";
} /* }}} */
function display_record_buttons($position) /* {{{ */
{
if (($but_str = $this->display_buttons($position)) === null)
return;
if ($position == 'down') {
if ($this->tabs_enabled()) $this->display_tab_labels('down');
echo '<hr class="',$this->getCSSclass('hr', 'down'),'">',"\n";
}
echo '<table class="',$this->getCSSclass('navigation', $position),'">',"\n";
echo '<tr class="',$this->getCSSclass('navigation', $position),'">',"\n";
echo '<td class="',$this->getCSSclass('buttons', $position),'">',"\n";
echo $but_str,'</td>',"\n";
// Message is now written here
} /* }}} */
function display_buttons($position) /* {{{ */
{
$nav_fnc = 'nav_'.$position;
if(! $this->$nav_fnc())
return;
$buttons = (is_array($this->buttons[$this->page_type][$position]))
? $this->buttons[$this->page_type][$position]
: $buttons = $this->default_buttons[$this->page_type];
foreach ($buttons as $name) {
$ret .= $this->display_button($name, $position)."\n";
}
return $ret;
} /* }}} */
function display_button($name, $position = 'up') /* {{{ */
{
if (is_array($name)) {
if (isset($name['code'])) return $name['code'];
return $this->htmlSubmit($name['name'], $name['value'], $name['css'], $name['disabled'], $name['js']);
}
$disabled = 1; // show disabled by default
if ($name[0] == '+') { $name = substr($name, 1); $disabled = 0; } // always show disabled as enabled
if ($name[0] == '-') { $name = substr($name, 1); $disabled = -1; } // don't show disabled
if ($name == 'cancel') {
return $this->htmlSubmit('cancel'.$this->page_types[$this->page_type], 'Cancel',
$this->getCSSclass('cancel', $position), false);
}
if (in_array($name, array('add','convert'))) {
$enabled_fnc = $name.'_enabled';
$enabled = $this->$enabled_fnc();
if ($name != 'add' && ! $this->total_recs && strstr('LF', $this->page_type))
$enabled = false;
return $this->htmlSubmit('operation', ucfirst($name),
$this->getCSSclass($name, $position), false, $enabled ? 0 : $disabled);
}
} /* }}} */
function number_of_recs() /* {{{ */
{
$count_parts = array(
'type' => 'select',
'select' => 'count(*)',
'from' => $this->get_SQL_join_clause(),
'where' => $this->get_SQL_where_from_query_opts());
$res = $this->myquery($this->get_SQL_query($count_parts), __LINE__);
$row = @mysql_fetch_array($res, MYSQL_NUM);
$this->total_recs = $row[0];
} /* }}} */
function list_table() /* {{{ */
{
$this->number_of_recs();
$this->form_begin();
$this->display_list_table_buttons('down');
} /* }}} */
function display_record() /* {{{ */
{
$this->form_begin();
if ($this->cgi['persist'] != '') {
echo $this->get_origvars_html($this->cgi['persist']);
}
if ($this->add_operation()) {
$this->display_add_record();
}else if ($this->convert_operation()) {
$this->display();
}
$this->display_record_buttons('down');
} /* }}} */
function exec_triggers($op, $step, $oldvals, &$changed, &$newvals) /* {{{ */
{
if (! isset($this->triggers[$op][$step])) {
return true;
}
$ret = true;
$trig = $this->triggers[$op][$step];
if (is_array($trig)) {
ksort($trig);
for ($t = reset($trig); $t !== false && $ret != false; $t = next($trig)) {
$ret = include($t);
}
} else {
$ret = include($trig);
}
return $ret;
} /* }}} */
function exec_triggers_simple($op, $step) /* {{{ */
{
$oldvals = $newvals = $changed = array();
return $this->exec_triggers($op, $step, $oldvals, $changed, $newvals);
} /* }}} */
function recreate_fdd($default_page_type = 'L') /* {{{ */
{
// TODO: one level deeper browsing
$this->page_type = $default_page_type;
$this->filter_operation() && $this->page_type = 'F';
$this->convert_operation() && $this->page_type = 'Q';
if ($this->add_operation()
|| $this->saveadd == $this->labels['Save']
|| $this->moreadd == $this->labels['More']) {
$this->page_type = 'A';
}
} /* }}} */
function connect() /* {{{ */
{
if (isset($this->dbh)) {
return true;
}
if (!isset($this->db)) {
$this->error('no database defined');
return false;
}
if (!isset ($this->tb)) {
$this->error('no table defined');
return false;
}
$this->dbh = @ini_get('allow_persistent')
? @mysql_pconnect($this->hn, $this->un, $this->pw)
: @mysql_connect($this->hn, $this->un, $this->pw);
if (!$this->dbh) {
$this->error('could not connect to MySQL');
return false;
}
return true;
} /* }}} */
function execute() /* {{{ */
{
// Database connection
if ($this->connect() == false) {
return false;
}
if ($this->add_operation()) {
$this->display_record();
}elseif ($this->convert_operation()) {
$this->display_record();
}
else {
$this->list_table();
}
}
function phpMyEdit($opts) /* {{{ */
{
// Set desirable error reporting level
$error_reporting = error_reporting(E_ALL & ~E_NOTICE);
// Database handle variables
if (isset($opts['dbh'])) {
$this->close_dbh = false;
$this->dbh = $opts['dbh'];
$this->dbp = '';
} else {
$this->close_dbh = true;
$this->dbh = null;
$this->dbp = $opts['db'].'.';
$this->hn = $opts['hn'];
$this->un = $opts['un'];
$this->pw = $opts['pw'];
$this->db = $opts['db'];
}
$this->tb = $opts['tb'];
$this->options = $opts['options'];
$this->display['form'] = isset($opts['display']['form'])
? $opts['display']['form'] : true;
// Creating directory variables
$this->dir['root'] = dirname(realpath(__FILE__))
. (strlen(dirname(realpath(__FILE__))) > 0 ? '/' : '');
$this->dir['lang'] = $this->dir['root'].'lang/';
// Navigation
$this->navigation = @$opts['navigation'];
if (! $this->nav_buttons() && ! $this->nav_text_links() && ! $this->nav_graphic_links()) {
$this->navigation .= 'B'; // buttons are default
}
$this->buttons = $opts['buttons'];
// Language labels (must go after navigation)
$this->labels = $this->make_language_labels(isset($opts['language'])
? $opts['language'] : $this->get_server_var('HTTP_ACCEPT_LANGUAGE'));
// Form variables all around
$this->fl = intval($this->get_sys_cgi_var('fl'));
$this->fm = intval($this->get_sys_cgi_var('fm'));
$this->operation = $this->get_sys_cgi_var('operation');
$oper_prefix_len = strlen($this->cgi['prefix']['operation']);
if (! strncmp($this->cgi['prefix']['operation'], $this->operation, $oper_prefix_len)) {
$this->operation = $this->labels[substr($this->operation, $oper_prefix_len)];
}
// Specific $fdd modifications depending on performed action
$this->recreate_fdd();
// Call to action
!isset($opts['execute']) && $opts['execute'] = 1;
$opts['execute'] && $this->execute();
} /* }}} */
}
?> |
now when the page started it will show the two button which is the add and convert button. add button which will call the display_add_record() with no problem. but when i click the convert button which triggered the display() function that have the line " header("Location:convert.php"); " and display trhe warning message below:
Warning: Cannot modify header information - headers already sent by (output started at c:\program files\easyphp1-8\www\ets_new\ets_new.php:7) in c:\program files\easyphp1-8\www\ets_new\phpMyEdit.class.php on line 83
i know that the header must be put before the html tag but now it call the class function. how to solve my problem? i really need this function to work as it helps me a lot.
could anyone solve my problem cause once i triggered the convert button i want the header to pass to the convert.php page as the page will generate my report.
Anyone please help me out with this so i can call the function display() to triggered the header when i click on the convert button.
thanks |
|