语法错误,意外的' foreach' (T_FOREACH)

时间:2014-12-22 09:32:05

标签: php

我的foreach语法错误。我想从我的后台选择一个文本文件,该文件从网站上写下所有的eamil。我只想从我的数据库中获取电子邮件,并将它们写入我的硬盘中的文本文件中。

<?php
    if (!defined('_PS_VERSION_'))
        exit;

    class SuperModule extends Module
    {
        public function __construct()
        {
            $this->name = 'supermodule';
            $this->tab = 'administration';
            $this->version = 1.0;
            $this->author = 'Lelu Matthias';
            $this->need_instance = 0;
            $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_); 
            $this->bootstrap = true;
            parent::__construct(); 
            $this->displayName = $this->l('My super module');
            $this->description = $this->l('This module is super !'); 
            $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
            if (!Configuration::get('MYMODULE_NAME'))
                $this->warning = $this->l('No name provided.'); 
        }
        public function install()
        {
            if (Shop::isFeatureActive())
                Shop::setContext(Shop::CONTEXT_ALL);
            return parent::install() &&
                $this->registerHook('displayNav') &&
                $this->registerHook('header') &&
                Configuration::updateValue('MYMODULE_NAME', 'super module') &&
                    Configuration::updateValue('MOD_SUPERMODULE_OPENINGHOURS', 'Ouvert de 9h a 19h') && Configuration::updateValue('MOD_SUPERMODULE_NEWSLETTER', '0');
        }
        public function uninstall()
        {
            return parent::uninstall();
        }
        public function getContent()
        {
            $output = null;
            if (Tools::isSubmit('submit_openinghours'))
            {
                $supermodule_openinghours = Tools::getValue('MOD_SUPERMODULE_OPENINGHOURS');
                if (!$supermodule_openinghours  || empty($supermodule_openinghours) || !
                    Validate::isGenericName($supermodule_openinghours))
                    $output .= $this->displayError( $this->l('Invalid Configuration value') );
                else
                {
                    Configuration::updateValue('MOD_SUPERMODULE_OPENINGHOURS', 
                                               $supermodule_openinghours);
                    $output .= $this->displayConfirmation($this->l('Settings updated'));
                }
            }
            if (Tools::isSubmit('submit_exportnewsletter'))

            {   
                $id_option = Tools::getValue('MOD_SUPERMODULE_NEWSLETTER');
             $output .=$id_option;
                $txt="";
                if($id_option ==1)
                {
                    (foreach $newsletter as $row)
                    {
                    $text.=$row['email'];}
                    $table='newsletter';

                }
                else 
                {
                    (foreach $customer as $row)
                    {
                    $text.=$row['email'];
                    }
                    $table='customer';
                }

                $date = gmdate('dmY');
                $file = fopen(dirname(__FILE__).'/export-newsletter-'.$table.'-'.
                              $date.'.txt', 'w');
                fputs($file, $txt);
                fclose($file);

            }
            return $output.$this->displayForm1().$this->displayForm2();
        }
        public function displayForm1()
        {
            $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');

            $fields_form[0]['form'] = $this->formulaire1();

            $helper = new HelperForm();

            // Module, token and currentIndex
            $helper->module = $this;
            $helper->name_controller = $this->name;
            $helper->token = Tools::getAdminTokenLite('AdminModules');
            $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;

            // Language
            $helper->default_form_language = $default_lang;
            $helper->allow_employee_form_lang = $default_lang;

            // Title and toolbar
            $helper->title = $this->displayName;
            $helper->show_toolbar = true;
            $helper->toolbar_scroll = true;
            $helper->submit_action = 'submit'.$this->name;
            $helper->toolbar_btn = array(
                'save' =>
                array(
                    'desc' => $this->l('Save'),
            'href' => AdminController::$currentIndex.'&configure='.$this->
                    name.'&save'.$this->name.
                    '&token='.Tools::getAdminTokenLite('AdminModules'),
                ),
                'back' => array(
                    'href' => AdminController::
                    $currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
                    'desc' => $this->l('Back to list')
                )
            );

            // Load current value
            $helper->fields_value['MOD_SUPERMODULE_OPENINGHOURS'] = 
                Configuration::get('MOD_SUPERMODULE_OPENINGHOURS');
            return $helper->generateForm($fields_form);
        }
        protected function formulaire1()
        {
            return array(
                'legend' => array(
                    'title' => $this->l('Opening hours'),
                ),
                'input' => array(
                    array(
                        'type' => 'text',
                        'label' => $this->l('Opening hours'),
                        'name' => 'MOD_SUPERMODULE_OPENINGHOURS',
                        'size' => 20,
                        'required' => true
                    ),
                ),
                'submit' => array(
                    'name' => 'submit_openinghours',
                    'title' => $this->l('Save')
                )
            );
        }
        protected function formulaire2()
        {
            $options = array(
                array(
                    'id_option' => 0, 
                    'name' => $this->l('Customer table')
                ),
                array(
                    'id_option' => 1,
                    'name' => $this->l('Newsletter table')
                ),
            );
            return array(
                'legend' => array(
                    'title' => $this->l('Newsletter export'),
                ),
                'input' => array(
                    array(
                        'type' => 'select',
                        'label' => $this->l('Choice a table'),
                        'name' => 'MOD_SUPERMODULE_NEWSLETTER',
                        'desc' => $this->l('Please choice a table.'),
                        'options' => array(
                            'query' => $options, 
                            'id' => 'id_option', 
                            'name' => 'name'
                            ),
                        ),
                    ),
                    'submit' => array(
                            'name' => 'submit_exportnewsletter',
                            'title' => $this->l('Save')
                    )
            );
    }
        public function displayForm2()
        {
            $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');

            $fields_form[0]['form'] = $this->formulaire2();

            $helper = new HelperForm();

            // Module, token and currentIndex
            $helper->module = $this;
            $helper->name_controller = $this->name;
            $helper->token = Tools::getAdminTokenLite('AdminModules');
            $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;

            // Language
            $helper->default_form_language = $default_lang;
            $helper->allow_employee_form_lang = $default_lang;

            // Title and toolbar
            $helper->title = $this->displayName;
            $helper->show_toolbar = true;  
            $helper->toolbar_scroll = true;  
            $helper->submit_action = 'submit'.$this->name;
            $helper->toolbar_btn = array(
                'save' =>
                array(
                    'desc' => $this->l('Save'),
    'href'=>AdminController::$currentIndex.'&configure='.$this->name.
                    '&save'.$this->name.
                    '&token='.Tools::getAdminTokenLite('AdminModules'),
                ),
                'back' => array(
                    'href' => AdminController::
                    $currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
                    'desc' => $this->l('Back to list')
                )
            );
            // Load current value
            $helper->fields_value['MOD_SUPERMODULE_NEWSLETTER'] = 
                Configuration::get('MOD_SUPERMODULE_NEWSLETTER');
            return $helper->generateForm($fields_form);
        }
        public function hookDisplayNav($params)
        {
            $ma_variable = Configuration::get('MOD_SUPERMODULE_OPENINGHOURS');
            $this->context->smarty->assign(array(
                'ma_variable' => $ma_variable
            ));
            return $this->display(__FILE__, 'supermodulenav.tpl');
        }
        public function hookDisplayHeader()
        {
            $this->context->controller->addCSS($this->_path.'css/supermodule.css', 'all');
        }
        public function GetMailNewsLetter()
        {
            $sql='SELECT email FROM '._DB_PREFIX_.'newsletter';
            $newsletter=Db::getInstance()->executeS($sql);
            return $newsletter;
        }
        public function GetMailCustomer()
        {
            $sql='SELECT email FROM '._DB_PREFIX_.'customer';
            $customer=Db::getInstance()->executeS($sql);
            return $customer;
        }
    }

5 个答案:

答案 0 :(得分:1)

试试这个

foreach ($newsletter as $row)

检查Manual here

答案 1 :(得分:1)

嘿只会将您的(foreach $newsletter as $row)更改为foreach($newsletter as $row)

类似这样的事情

<?php
    if (!defined('_PS_VERSION_'))
        exit;

    class SuperModule extends Module
    {
        public function __construct()
        {
            $this->name = 'supermodule';
            $this->tab = 'administration';
            $this->version = 1.0;
            $this->author = 'Lelu Matthias';
            $this->need_instance = 0;
            $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_); 
            $this->bootstrap = true;
            parent::__construct(); 
            $this->displayName = $this->l('My super module');
            $this->description = $this->l('This module is super !'); 
            $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
            if (!Configuration::get('MYMODULE_NAME'))
                $this->warning = $this->l('No name provided.'); 
        }
        public function install()
        {
            if (Shop::isFeatureActive())
                Shop::setContext(Shop::CONTEXT_ALL);
            return parent::install() &&
                $this->registerHook('displayNav') &&
                $this->registerHook('header') &&
                Configuration::updateValue('MYMODULE_NAME', 'super module') &&
                    Configuration::updateValue('MOD_SUPERMODULE_OPENINGHOURS', 'Ouvert de 9h a 19h') && Configuration::updateValue('MOD_SUPERMODULE_NEWSLETTER', '0');
        }
        public function uninstall()
        {
            return parent::uninstall();
        }
        public function getContent()
        {
            $output = null;
            if (Tools::isSubmit('submit_openinghours'))
            {
                $supermodule_openinghours = Tools::getValue('MOD_SUPERMODULE_OPENINGHOURS');
                if (!$supermodule_openinghours  || empty($supermodule_openinghours) || !
                    Validate::isGenericName($supermodule_openinghours))
                    $output .= $this->displayError( $this->l('Invalid Configuration value') );
                else
                {
                    Configuration::updateValue('MOD_SUPERMODULE_OPENINGHOURS', 
                                               $supermodule_openinghours);
                    $output .= $this->displayConfirmation($this->l('Settings updated'));
                }
            }
            if (Tools::isSubmit('submit_exportnewsletter'))

            {   
                $id_option = Tools::getValue('MOD_SUPERMODULE_NEWSLETTER');
             $output .=$id_option;
                $txt="";
                if($id_option ==1)
                {
                    foreach ($newsletter as $row)
                    {
                    $text.=$row['email'];
                    }
                    $table='newsletter';



                }
                else 
                {
                    foreach ($customer as $row)
                    {
                    $text.=$row['email'];
                    }
                    $table='customer';
                }

                $date = gmdate('dmY');
                $file = fopen(dirname(__FILE__).'/export-newsletter-'.$table.'-'.
                              $date.'.txt', 'w');
                fputs($file, $txt);
                fclose($file);

            }
            return $output.$this->displayForm1().$this->displayForm2();
        }
        public function displayForm1()
        {
            $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');

            $fields_form[0]['form'] = $this->formulaire1();

            $helper = new HelperForm();

            // Module, token and currentIndex
            $helper->module = $this;
            $helper->name_controller = $this->name;
            $helper->token = Tools::getAdminTokenLite('AdminModules');
            $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;

            // Language
            $helper->default_form_language = $default_lang;
            $helper->allow_employee_form_lang = $default_lang;

            // Title and toolbar
            $helper->title = $this->displayName;
            $helper->show_toolbar = true;
            $helper->toolbar_scroll = true;
            $helper->submit_action = 'submit'.$this->name;
            $helper->toolbar_btn = array(
                'save' =>
                array(
                    'desc' => $this->l('Save'),
            'href' => AdminController::$currentIndex.'&configure='.$this->
                    name.'&save'.$this->name.
                    '&token='.Tools::getAdminTokenLite('AdminModules'),
                ),
                'back' => array(
                    'href' => AdminController::
                    $currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
                    'desc' => $this->l('Back to list')
                )
            );

            // Load current value
            $helper->fields_value['MOD_SUPERMODULE_OPENINGHOURS'] = 
                Configuration::get('MOD_SUPERMODULE_OPENINGHOURS');
            return $helper->generateForm($fields_form);
        }
        protected function formulaire1()
        {
            return array(
                'legend' => array(
                    'title' => $this->l('Opening hours'),
                ),
                'input' => array(
                    array(
                        'type' => 'text',
                        'label' => $this->l('Opening hours'),
                        'name' => 'MOD_SUPERMODULE_OPENINGHOURS',
                        'size' => 20,
                        'required' => true
                    ),
                ),
                'submit' => array(
                    'name' => 'submit_openinghours',
                    'title' => $this->l('Save')
                )
            );
        }
        protected function formulaire2()
        {
            $options = array(
                array(
                    'id_option' => 0, 
                    'name' => $this->l('Customer table')
                ),
                array(
                    'id_option' => 1,
                    'name' => $this->l('Newsletter table')
                ),
            );
            return array(
                'legend' => array(
                    'title' => $this->l('Newsletter export'),
                ),
                'input' => array(
                    array(
                        'type' => 'select',
                        'label' => $this->l('Choice a table'),
                        'name' => 'MOD_SUPERMODULE_NEWSLETTER',
                        'desc' => $this->l('Please choice a table.'),
                        'options' => array(
                            'query' => $options, 
                            'id' => 'id_option', 
                            'name' => 'name'
                            ),
                        ),
                    ),
                    'submit' => array(
                            'name' => 'submit_exportnewsletter',
                            'title' => $this->l('Save')
                    )
            );
    }
        public function displayForm2()
        {
            $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');

            $fields_form[0]['form'] = $this->formulaire2();

            $helper = new HelperForm();

            // Module, token and currentIndex
            $helper->module = $this;
            $helper->name_controller = $this->name;
            $helper->token = Tools::getAdminTokenLite('AdminModules');
            $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;

            // Language
            $helper->default_form_language = $default_lang;
            $helper->allow_employee_form_lang = $default_lang;

            // Title and toolbar
            $helper->title = $this->displayName;
            $helper->show_toolbar = true;  
            $helper->toolbar_scroll = true;  
            $helper->submit_action = 'submit'.$this->name;
            $helper->toolbar_btn = array(
                'save' =>
                array(
                    'desc' => $this->l('Save'),
    'href'=>AdminController::$currentIndex.'&configure='.$this->name.
                    '&save'.$this->name.
                    '&token='.Tools::getAdminTokenLite('AdminModules'),
                ),
                'back' => array(
                    'href' => AdminController::
                    $currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
                    'desc' => $this->l('Back to list')
                )
            );
            // Load current value
            $helper->fields_value['MOD_SUPERMODULE_NEWSLETTER'] = 
                Configuration::get('MOD_SUPERMODULE_NEWSLETTER');
            return $helper->generateForm($fields_form);
        }
        public function hookDisplayNav($params)
        {
            $ma_variable = Configuration::get('MOD_SUPERMODULE_OPENINGHOURS');
            $this->context->smarty->assign(array(
                'ma_variable' => $ma_variable
            ));
            return $this->display(__FILE__, 'supermodulenav.tpl');
        }
        public function hookDisplayHeader()
        {
            $this->context->controller->addCSS($this->_path.'css/supermodule.css', 'all');
        }
        public function GetMailNewsLetter()
        {
            $sql='SELECT email FROM '._DB_PREFIX_.'newsletter';
            $newsletter=Db::getInstance()->executeS($sql);
            return $newsletter;
        }
        public function GetMailCustomer()
        {
            $sql='SELECT email FROM '._DB_PREFIX_.'customer';
            $customer=Db::getInstance()->executeS($sql);
            return $customer;
        }
    }

我认为它可以帮助你

答案 2 :(得分:1)

  

您使用了错误的 foreach 语法。请按照下面给出的代码进行操作。另外,请检查thisthis

(foreach $newsletter as $row)   // it is not a correct syntax
foreach ($newsletter as $row)   // this is a correct syntax

(foreach $customer as $row)   // it is not a correct syntax
foreach ($customer as $row)   // this is a correct syntax

答案 3 :(得分:0)

语法错误:

(foreach $newsletter as $row)

更正语法

foreach($newsletter as $row)

答案 4 :(得分:0)

foreach的正确语法是

foreach($newsletter as $row)
{

}

(foreach $newsletter as $row)