-1

エラー配列が空の場合は true を返し、配列が空でない場合は配列を返す関数があるとします。次に、別の関数で、その関数が true を返すかどうかを確認します。最初の関数からエラー配列を取得してユーザーに表示するにはどうすればよいですか?

public function backup($filename, $tables = '')
{
    $err = array();
    // get all of the tables
    if (empty($tables)) {
        $tables = array();
        $result = Nemesis::query('SHOW TABLES');
        while ($row = $result->fetch_row()) {
            $tables[] = $row[0];
        }
    } elseif (is_array($tables)) {
        $tables = explode(',', $tables);
    } else {
        $err[] = 'If you are specifying tables to be backed up, you need to provide the script with an array.';
    }
    $result = Nemesis::select("*", $table);
    $result_q = Nemesis::query("SHOW CREATE TABLE {$table}");
    if (!$result && !$result_q) {
        $err[] = 'Backup queries failed.';
    }
    // Cycle through each provided table
    foreach ($tables as $table) {
        $num_fields = $result->field_count;
        // First part of the output - remove the table
        $return .= 'DROP TABLE ' . $table . ';<|||||||>';
        // Second part of the output - create table
        $result_q_row = $result_q->fetch_row();
        $return .= "\n\n" . $result_q_row[1] . ";<|||||||>\n\n";
        // Third part of the output - insert values into new table
        for ($i = 0; $i < $num_fields; $i++) {
            while ($row = $result->fetch_row()) {
                $return .= 'INSERT INTO ' . $table . ' VALUES(';
                for ($j = 0; $j < $num_fields; $j++) {
                    $row[$j] = addslashes($row[$j]);
                    $row[$j] = preg_replace("#\n#", "\\n", $row[$j]);
                    if (isset($row[$j])) {
                        $return .= '"' . $row[$j] . '"';
                    } else {
                        $return .= '""';
                    }
                    if ($j < ($num_fields - 1)) {
                        $return .= ',';
                    }
                }
                $return .= ");<|||||||>\n";
            }
        }
        $return .= "\n\n\n";
    }
    $dirs = array($this->dir_backup, $this->dir_files, $this->dir_sql);
    foreach ($dirs as $dir) {
        if (!is_dir($dir)) {
            if (!mkdir($dir)) {
                $err[] = "Could not create {$dir}";
            }
        }
    }
    if (empty($err)) {
        $handle = fopen($this->dir_sql . $filename . '.sql', 'w+');
        fwrite($handle, $return);
        fclose($handle);
        //$arr = array(FRONTEND_IMAGE_UPLOAD_PATH, BACKEND_IMAGE_UPLOAD_PATH . 'users' . DIRECTORY_SEPARATOR);
        $zip = new ZipFolder($this->dir_files . $filename . '.zip', $this->folders_to_backup, $this->zip_ignore);
        return TRUE;
    } else {
        return implode('<br>', $err);
    }
}
public function backup_dbf () 
{
    $filename = 'dbbackup_' . date("d.m.Y_H_i_s");
    if ($this->backup($filename) === TRUE) {
        $msg = new Messages();
        $msg->add('s', "Backup {$filename} successfully created.");
    } else { 
        // somehow get err array
        $msg = new Messages();
        $msg->add('e', "Backup failed: ");
    }
}
4

3 に答える 3

1

これを試してみるとどうなりますか:

public function backup_dbf () {
    $filename = 'dbbackup_' . date("d.m.Y_H_i_s");
    if (($error = $this->backup($filename)) === true) {
        $msg = new Messages();
        $msg->add('s', "Backup {$filename} successfully created.");
    } else { 
        $msg = new Messages();
        $msg->add('e', "Backup failed " . $error);
    }
}
于 2013-07-18T22:54:06.803 に答える
0

参照によってエラー配列を返すことができます。

何かのようなもの

function yourFunction($array, &$errors = array()) {

    /* add errors to $errors */

    return true;
}

次に、関数を呼び出します

$valid = yourFunction($myArray, $errors);

$errorsにはエラー配列が含まれ、$validは true または false (ブール値) になります。

于 2013-07-18T22:46:59.207 に答える
0

backup_dbf ()関数を次のように更新できます


public function backup_dbf () 
{
    $filename = 'dbbackup_' . date("d.m.Y_H_i_s");
    $output = $this->backup($filename);
    if (is_bool($output) && $output === TRUE) {
        $msg = new Messages();
        $msg->add('s', "Backup {$filename} successfully created.");
    } else { 
        // this means that you returned the err array from backup function
        echo print_r($output,true);
    }
}

于 2013-07-18T22:57:27.217 に答える