我怎么能在其他地方回响“;所搜索的业务不存在”;


How can I echo in else that "The searched business does not exist"?

我的最后一个问题是在得到成功答案后,如何访问模型的一个视图中的数据。我实现了它。这是我的问题

现在它工作得很好,但当用户搜索数据库中不存在的business时,系统如何回应

业务不存在

我试图在foreach循环中插入else,但它多次回显"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>

如果您想在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>