douna1895 2012-04-28 16:40
浏览 19
已采纳

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

  • 写回答

2条回答 默认 最新

  • dongying195959 2012-04-29 10:50
    关注
        <?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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了