添加foreach时其他文件中的语法错误

时间:2017-02-02 17:06:30

标签: php laravel-5 foreach

这是我得到的错误:

  

语法错误,意外结束文件,期待函数(T_FUNCTION)'在D:\ User \ Documents ... \ Controllers \ DeviceController.php:68

在此代码中出现:

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Data\Campaign;
use App\Data\Device;
use App\Helpers\Database;
use App\Helpers\DateHelpers;
use App\Helpers\ProfileHelpers;
use App\Helpers\Utils;
use Illuminate\Http\Request;

class DeviceController extends ApiController
{
    public function show($deviceId)
    {
        return Device::find($deviceId);
    }

    public function update(Request $request, $deviceId)
    {
        $isAlertActive = filter_var($request->input('isAlertActive'), FILTER_VALIDATE_BOOLEAN);
        $isAlertAfter5Pushes = filter_var($request->input('isAlertAfter5Pushes'), FILTER_VALIDATE_BOOLEAN);
        $isAlertAfter10Pushes = filter_var($request->input('isAlertAfter10Pushes'), FILTER_VALIDATE_BOOLEAN);
        $hasAtLeastOneSettingActive = $isAlertActive || $isAlertAfter5Pushes || $isAlertAfter10Pushes;

        $settings = [
            'is_alert' => $isAlertActive,
            'is_alert_after_5_pushes' => $isAlertAfter5Pushes,
            'is_alert_after_10_pushes' => $isAlertAfter10Pushes
        ];

        if ($isAlertActive) {
            $settings['alert_seuil'] = intval($request->input('alertThreshold'));
            $settings['alert_votemini'] = intval($request->input('minVoteCountBeforeAlert'));
        }

        if ($hasAtLeastOneSettingActive)
            $settings['next_alert_in_hours'] = intval($request->input('nextAlertInHours'));

        Device::where('id_device', $deviceId)
              ->update($settings);
    }

/////////////////////////////////////////////////////////////////////////////

    public function putInLocation($sectionId, $locationName)
    {
        $section = Section::find($sectionId);
        if ($section && $section->area->id_client == $this->clientId) {
            Location::createLocation($sectionId, $locationName);
            return self::jsonSuccess();
        }
        else
            return self::jsonBadRequest();
    }

    public function delete($locationId)
    {
        $location = Location::find($locationId);
        if ($location && $location->section->area->id_client == $this->clientId) {
            $location->delete();
            return self::jsonSuccess();
        }
        else
            return self::jsonBadRequest();
    }
}
?>

我没有看到任何语法错误,我多次检查括号,但也许我错过了什么?

对我来说奇怪的是,在我在另一个文件中添加foreach之前没有语法错误(D:\ User \ Documents ... \ Views \ Partials \ areasettings.php)。这是foreach:

<div class="col-lg-3 col-lg-offset-0 v">
    <div class="input-group">
        <span class="input-group-addon"><span class="glyphicon glyphicon-barcode"></span></span>
        <select class="form-control" id="deviceSelect"> 
            <option selected disabled> <?php echo trans('areasettings.add_id_placeholder') ?> </option>
            <option> TEST </option>
            <?php 
            $devices = $db->fetchAll("select `id_device_mac` from `dashboard_demo.nao_device`");
            foreach ($devices as $device) {
                $idDevice = $device->id_device;
                $idDeviceMac = $device->id_device_mac;
                //echo "<option value=\"$idDevice\">$idDeviceMac</option>\n";
                <option value="<?= $device['id_device_mac'] ?>"><?= $device['id_device_mac'] ?></option>
            }
            ?>
        </select>
        <!--<input type="text" class="form-control" placeholder="<?php echo trans('areasettings.add_id_placeholder') ?>" autocomplete="off"
            ng-model="newSectionName">-->
    </div>
</div>

先谢谢你的帮助!

编辑:用这一行改变了fetchAll(laravel方式):

$devices = DB::table('dashboard_demo.nao_device')->select('id_device_mac')->get()

但我仍然得到同样的错误。 我在日志中搜索得更深,并且发现设备显然是未定义的。

1 个答案:

答案 0 :(得分:0)

如果您使用的是vanilla PDO,则此行看起来不对。

$devices = $db->fetchAll("select `id_device_mac` from `dashboard_demo.nao_device`");

PDO :: fetchAll不执行查询,它只处理已执行查询的结果集。

这样的事可能

$sql = 'select `id_device_mac` from `dashboard_demo.nao_device`';
$result = $db->query($sql);
if ( ! $result ) {
    print_r($db->errorInfo())
    exit;
}
while ($device = $result->fetch(PDO::FETCH_OBJ)) {

    $idDevice = $device->id_device;
    $idDeviceMac = $device->id_device_mac;
    echo "<option value='$idDevice'>$idDeviceMac</option>\n";

}

这种方式应该显示查询中是否发生任何错误。

还改变了while循环的内容。你在那里使用数组表示法,但从上面的$device变量获取数据时是一个对象表示法?