我怎么能在其他方面回应"搜索到的业务不存在"?

时间:2015-11-13 15:03:36

标签: php database yii view model

我的最后一个问题是如何在获得成功答案之后在其中一个视图中访问模型的数据。 This was my question

现在它的工作正常,但是当用户搜索数据库中不存在的business系统回显时,我怎么能回应呢?

  

业务不存在

我尝试在else循环中插入foreach,但它回应了" Business not exist"多次。

我在我的landing.php中发布用户搜索的商家名称,这是我searching视图的布局。

我正在粘贴我的Landing.php,业务控制器和我调用的Searching

视图文件

我的landing.php:

<form action="business/searching" method="POST">                     
    <div class="form-group form-group-lg">
        <h2 class="title">
            Find the best places to eat, drink, shop, or visit in Islamabad. 
        </h2>
        <div class="col-sm-5 col-md-5 col-lg-5 col-md-offset-1">
            <input type="text" class="form-control" name="business" id="lg" placeholder="I'm looking for...."/>
        </div>
        <div class="col-sm-3 col-md-3 col-lg-3">
            <input type="text" class="form-control" id="sm"  placeholder="Islamabad" disabled=""/>
        </div>
        <div class="col-sm-3 col-md-3 col-lg-3">
            <input type="submit" class="btn btn-primary btn-lg"/>
        </div>
    </div>
</form>

在我的业务控制器中

 public function actionSearching()
 {
     $models = Business::model()->findAll();
     $modeladd = Address::model()->findAll();

     $this->layout='//layouts/main';
     $this->render(
         'searching', 
         array(
             'models' => $models,
             'modeladd' => $modeladd,        
         )
     );
 }

搜索视图它有点复杂,但问我可以解释。

    <div class="gap"> </div>
                <div class="container">
                <div class="row">
                <div class="col-md-9">

                <ul class="booking-list">
                <li>  
  <?php foreach($models as $model)  //getting business model from searching action in business controller
      {
      $name=$model->business_name; //storing name
       $id=$model->id;   //storing id in order to use this id in address if as addres has no business_name
        if($name== $_POST["business"])    //matching name
                 {  ?>
    <a class="booking-item" href="<?php Yii::app()->request->baseUrl;?>/business/userbusiness/<?php echo $id?>">
    <div class="row">
    <div class="col-md-5">
     <img src="<?php Yii::app()->request->baseUrl;?>/img/<?php echo $model->image;?>" alt="Not availble" title="Image business" />
       </div>
    <div class="col-md-7">
    <div class="booking-item-rating">
    </div>
      <h5 class="booking-item-title"><?php echo $model->business_name; ?></h5>
                <?php   foreach($modeladd as $mo) //getting address model from searching action in business controller
          {
               $addbizid=$mo->business_id;   //businessid from address table
            if($id== $addbizid)     //wanted to match business name, but couldnot as addres has no business name only business_id, so i get business_id
                                     //  from above after the searched business name is matched and now comparing that id with addres->businessid
            {
   $street=$mo->street_number;   //if match store address in these variable and then simple print them below
     $sector=$mo->sector; 
     $city=$mo->city; ?>
                <p class="booking-item-address"><i class="fa fa-map-marker"></i> <?php echo $street; echo $sector; ;echo $city;?>  <?php  }} ?></p>
               <p class="booking-item-description"> <?php echo $model->business_description;?>
                </p>
                </div>
                </div>
                      <?php }}  ?>
                </a>
                </li>
                </ul>
                  </div>

        <div class="col-md-3">
        <div class="booking-item-dates-change mb30">
        <h3>Not here? Tell us what we're missing.</h3><p>If the business you're looking for isn't here, add it!</p>
        <a href="addbusiness.php" class="btn btn-primary"><i class="fa fa-plus"></i> Add Business Page</a>
        </div>

        </div>

        </div>
        <div class="gap"></div>
        </div>
<div class="gap"></div>
        </div>

1 个答案:

答案 0 :(得分:0)

如果你想在foreach循环中使用if else,你可以使用endif,end foreach循环。

    <?php

/* 
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/  
//echo $_POST["business"]; echo "</br>";
//foreach($modeladd as $mo)
//  {
//   
//     $street=$mo->street_number;
//    
//     $address=$mo->sector; 
//    
//      $city=$mo->city;
//      $business_id=$mo->business_id;
//      echo $street; echo ("<br />");echo $address;  echo ("<br />");echo $city; echo ("<br />");echo $business_id;
//}

?>
<?php $matchFound = false; ?>
<div class="gap"> </div>
<div class="container">
<div class="row">
<div class="col-md-9">
<ul class="booking-list">

<li>  
<?php foreach($models as $model): ?>  
  <?php  //removing foreach outerloop
$name=$model->business_name; //storing name
$id=$model->id;   //storing id in order to use this id in address if as addres has no business_name
?>
<?php if($name== $_POST["business"]):    //matching name
  $matchFound = true;            //removing if match
?>

<a class="booking-item" href="<?php Yii::app()->request->baseUrl;?>/business/userbusiness/<?php echo $id?>">
<div class="row">
<div class="col-md-5">
<img src="<?php Yii::app()->request->baseUrl;?>/img/<?php echo $model->image;?>" alt="Not availble" title="Image business" />
</div>
<div class="col-md-7">
<div class="booking-item-rating">
</div>
<h5 class="booking-item-title"><?php echo $model->business_name; ?></h5>
<?php   foreach($modeladd as $mo) //getting address model from searching action in business controller
{
$addbizid=$mo->business_id;   //businessid from address table
if($id== $addbizid)     //wanted to match business name, but couldnot as addres has no business name only business_id, so i get business_id
//  from above after the searched business name is matched and now comparing that id with addres->businessid
{
$street=$mo->street_number;   //if match store address in these variable and then simple print them below
$sector=$mo->sector; 
$city=$mo->city; ?>
<p class="booking-item-address"><i class="fa fa-map-marker"></i> 
<?php echo $street; echo $sector; ;echo $city;?> 
 <?php 
}
} 
?>
</p>
<p class="booking-item-description"> <?php echo $model->business_description;?>
</p>
</div>
</div>
<?php 
break;
 endif;                   //removing if matc outer loop
 endforeach;              //removing for each outer loop
?>
    <?php if (!$matchFound): ?>
    <?php echo "No data found for the room number you entered."; ?>
<?php endif; ?>
</a>
</li>
</ul>
</div>
<div class="col-md-3">
<div class="booking-item-dates-change mb30">
<h3>Not here? Tell us what we're missing.</h3><p>If the business you're looking for isn't here, add it!</p>
<a href="addbusiness.php" class="btn btn-primary"><i class="fa fa-plus"></i> Add Business Page</a>
</div>
</div>
</div>
<div class="gap"></div>
</div>
<div class="gap"></div>
</div>