0

您好我正在尝试创建一个函数来搜索我的 Uploads 模型并显示该表中的信息。

 Notice (8): Undefined variable: uploads [APP/View/Uploads/search.ctp, line 28]
 Warning (2): Invalid argument supplied for foreach() [APP/View/Uploads/search.ctp, line 28]

在我什至被允许搜索之前,我得到了这个错误,

这是我的 search.ctp

        <?php $uploads = $this->requestAction('uploads/search'); 
       ?>

     <div id="search">
  <?php echo $this->Form->create('Upload',array('action'=>'search'));?>
   <fieldset>
    <legend><?php __('Upload Search');?></legend>
    <?php
    echo $this->Form->input('searchupload', array('label' => false, 'class'=>'searchinput')); 

    $options = array(
                     'label' => '',
                     'value' => 'Search',
                     'class' => 'searchbutton'
                            );
    echo $this->Form->end($options);


?>
</fieldset>



    </div>
    <div id="contentbox">
<table>
    <?php foreach($uploads as $upload) : ?>  
        <tr>
            <td><?php echo $upload['Upload']['name'] ?></td>
            <td><?php echo $upload['Upload']['eventname'] ?></td>
        </tr>
    <?php endforeach; ?>
</table>
    </div>

这是上传控制器中的功能:

   function search() {

        if (!empty($this->data)) {
    $searchstr = $this->data['Upload']['search'];
    $this->set('searchstring', $this->data['Upload']['search']);
    $conditions = array(
        'conditions' => array(
        'or' => array(
            "Upload.name LIKE" => "%$searchstr%",
            "Upload.eventname LIKE" => "%$searchstr%"
        )
        )
    );
   $this->set('uploads', $this->Upload->find('all', $conditions));
     }
        }

任何帮助将不胜感激!

提前致谢

4

2 回答 2

0
    <?php if(!empty($uploads)) : ?>
    <?php foreach($uploads as $upload) : ?>  
        <tr>
            <td><?php echo $upload['Upload']['name'] ?></td>
            <td><?php echo $upload['Upload']['eventname'] ?></td>
        </tr>
    <?php endforeach; ?>
    <?php else : ?>

 <div>
   No search matches found
   </div>

     <?php endif; ?>

这确保如果找到一个空集,则将未找到的搜索匹配项扔到网页中

于 2012-04-29T10:50:04.330 回答
0

为什么要从同一个视图运行两次上传/搜索?

尝试从您的视图中删除它:

<?php $uploads = $this->requestAction('uploads/search'); ?>

它调用您使用 search.ctp 呈现的相同操作。

更新:

如果您收到未定义变量通知(顺便说一下,在生产和调试设置为 0 时您不会看到),您需要做的就是在控制器(而不是视图)中将上传变量设置为 null。在运行搜索之前,请执行以下操作:$this->set('uploads', array());在控制器中,以便在视图中定义变量。你不想让逻辑混乱视图。

于 2012-04-28T20:30:55.313 回答