1

这是我第一次尝试使用 php 类,以前从未这样做过,我很享受这个过程。

代码可能有点乱,如果您有任何建议,我将非常感激!

关于这个问题,我正在尝试,echo "$asd->getData()"结果有点奇怪,我无法解释结果:

数组 ( [0] => 名称 [1] => 电子邮件 [2] => telemovel [3] => loja [4] => assunto [5] => mensagem [6] => localidade [7] => nserie [nome] => holder [email] => eeee [telemovel] => 913582837 [loja] => braga [assunto] => [mensagem] => [localidade] => [nserie] => )

我正在寻找以下结果:

Array([nome] => MyName [email] => emailHotmail [telemovel] => cellphone_here [loja] => store_name [assunto] => subject_here [mensagem] => Message_here [localidade] => Location [nserie] => Serial_number )

为什么会出现数组编号/键?

<?php

class Email{

    private $data;

    function Email(){
            $this->data=array();
    }

        public function setData($d){
            $this->data = $d;
        }

    public function valida(){
            $verifica = true;
        foreach ($this->data as $campo) {
                $this->data[$campo] = $_GET[$campo];
                if(!isset($_GET[$campo]) || empty($_GET[$campo])){
                    $verifica=true;
                }
            }
            return $verifica;
    }

    public function getData(){
            print_r($this->data);
        }


        public function sendEmail($data){
            $to = ($this->data['loja']=="braga") ? "email1@email.com" : "email1@email.com";
            $subject = $this->data['assunto'];//$campos[4];
        $nserie = ($this->data['nserie']) ? '\r\n nº de série:'.$this->data['nserie'] : "";
        $localidade = ($this->data['localidade']) ? '\r\n Localidade:'.$this->data['localidade'] : "";
        $messages = $this->data['mensagem'];//$campos[5];
        $message = $messages.$nserie.$localidade;
            $message = htmlspecialchars_decode($message,ENT_QUOTES);//optional - I use encoding to POST data
            $name = $this->data['nome'];
            $from = $this->data['email'];//$campos[1];
            $headers = "From:".$name." <".$from.">\r\n";
            $headers .= "MIME-Version: 1.0\r\n"; 
            $headers .= "Content-type: text/plain; charset=utf-8\r\n";
            $headers .= "Content-Transfer-Encoding: 8bit";

            return mail($to, "=?utf-8?B?".base64_encode($subject)."?=", $message, $headers);
        }
}
$asd = new Email();
$campos = array('nome','email','telemovel','loja','assunto','mensagem','localidade','nserie');
$asd->setData($campos);
$asd->sendEmail($data);
echo ($asd->valida($data)) ? "sent<br/>" : "not sent<br/>";
echo "<br/>".$asd->getData()."<br/>";


?>
4

3 回答 3

0

您正在打印一个数组。您需要循环并打印出数组中的每个项目。

于 2012-07-07T16:03:17.023 回答
0
$campos = array('nome','email','telemovel','loja','assunto','mensagem','localidade','nserie');

与 /short 相同

$campos = array(0=> 'nome',1=> 'email',....);
于 2012-07-07T16:04:43.167 回答
0

您想在通话中设置 $campos,如下所示:

$campos = array('nome' => '','email' => '','telemovel' => '','loja' => '','assunto' => '','mensagem' => '','localidade' => '','nserie' => '');
$asd->setData($campos);

$campos 数组是一个值列表,您希望将这些值作为空白值的键。

祝你好运!

于 2012-07-07T16:12:57.300 回答