CakePHP 2.0搜索数据库错误

Hi Im trying to create a function that searches my Uploads model and displays the information that is in the that table.

 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]

Before I am even allowed to search I get this error,

This is my 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>

and this is the function in the uploads controller:

   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));
     }
        }

Any help would be greatly appreciated!

Thanks in advance

    <?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; ?>

This ensures that if an empty set is found then the no search match found is thrown into the webpage

Why are you running the uploads/search twice from the same view?

Try removing this from your view:

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

It calls the same action you are rendering with search.ctp.

UPDATE:

If you are getting the Undefined Variable notice (that you will not see when in production and debug is set to 0 by the way), all you need to do is set the uploads variable to null in the controller (not the view). Before you run the search do: $this->set('uploads', array()); in the controller so the variable will be defined in the view. You do not want to clutter the view with logic.