2017-06-19 132 views
-1

我是trieng从数据库列表项目,但结果出来两个。每个记录显示2个结果。目前我只有一个记录在数据库中,但是当我在屏幕上显示它时,它被重新修复。我不知道为什么会发生这种情况,我尝试过一个计数器,看看循环有多少次,结果总是一次。但结果显示两次。在php和mysql中重复记录

的代码如下:

<section class="container"> 
    <div class="row"> 
      <?php 
      if($bidCollection->selectBidsByStatusAndAppId(BidAccount::OPEN_NEW, $id)){   
       foreach($bidCollection->getBids() as $bid){ 
       $banker->find($bid->getCustomerId()); 
       $counter=0; 
      ?> 


         <div class='col-xs-12 bg-more-light-gray bidlist'> 

          <div class="col-xs-12 col-sm-1 col-md-1"> 

           <?php 

            if(!$uploader->findProfilePicture($banker->data()->_customer_id)){ 
             echo "<img src='image/holder.png ' width='50' height='50' class='img-responsive' />"; 
              }else{ 
             echo "<img src='upload/proPicture/".$uploader->data()->pictureUrl."' width='50' height='50' class='img-responsive'/>";       
            } 
           ?>    

          </div> 









          <div class="col-xs-12 col-sm-8 col-md-8"> 
           <?php 
            echo '<h2>Agent name :'.$validate->cleanInput($banker->data()->officer_name).'</h2>'; 
            echo '<h3>Institute name : '.$validate->cleanInput($banker->data()->bank_name).' '.++$counter.'</h3>'; 
            if($bid->getApplicationOwnerId() === $customer->data()->_customer_id){ 
               echo "<p>CheckBook: "; 
                if($bid->getRequestCheckBook()){echo "Yes";}else{echo "No";} 
               echo "</p>"; 
               echo"<p> Minimum Deposit: AED ".$bid->getMinDeposit()."</p>"; 
               echo"<p> Direct Debit Card: "; 
                 if($bid->getRequestCreditCard()){echo "Yes";}else{echo "No";} 
               echo "</p>"; 
               echo"<p> Other Fees/Arrangement Fees: AED ".$bid->getFees(). "</p>"; 
               echo"<p> Account Will be ready in: ".$bid->getSetupTime() ."</p>"; 
            } 
           ?>   
          </div> 
          <div class="col-xs-12 col-sm-2 col-md-2"> 
           <?php     
           $datetime = new DateTime($bid->getDatePosted()); 
           $date = $datetime->format('Y-m-d'); 
           $time = $datetime->format('H:i:s'); 
            echo '<h4> Date: '.$date.'<br/>Time: '.$time.'</h4>'; 
            echo"<form method='POST' action='viewBanker.php'>"; 
            echo "<input type='hidden' name='banker' value='".$bid->getCustomerId()."'>"; 
            echo "<input type='submit' class ='btn btn-default' name='submit' value='View Profile'>"; 
            echo "</form>"; 
            echo'<br/>'; 

             if($bid->getApplicationOwnerId() === $customer->data()->_customer_id){      

               echo"<form method='post' action='acceptAccountBid.php'>";     
                echo "<input type='hidden' value='".$bid->getCustomerId()."' name ='bankerId'/>"; 
                echo "<input type='hidden' value='".$id."' name ='appid'/>"; 
                echo "<input type='hidden' value='".$bid->getApplicationOwnerId()."' name='ownerId'>"; 
                echo "<input class='btn btn-default' type='submit' value='Accept Offer' name='submit'>"; 

               echo"</form>"; 

             } 

          ?>       
          </div> 
         </div> 

      <?php 

       } 
      } 
      ?> 

,返回的值的方法如下:

public function selectBidsByStatusAndAppId($status, $appid) 
    { 
     $sql = "SELECT * FROM accountBid WHERE application_id = :appId AND status = :st"; 
     try { 

      $sth = $this->_db->getConnection()->prepare($sql); 
      $sth->bindValue(':appId', $appid); 
      $sth->bindValue(':st', $status); 
      $sth->execute(); 

     } catch (Exception $e) { 
      $this->setAlert('danger', 'Information Presentation Error: ' . $e->getMessage()); 
     } 

     foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $data) { 
      $this->addBid($data); 
     } 

     return true; 
    } 



public function addBid($data = null) 
    { 
     $bid = new BidAccount($data); 
     $this->bids[] = $bid; 

    } 


public function getBids() 
    { 
     return $this->bids; 
    } 
+0

'''$ bidCollection-> getBids()'''返回什么?在'foreach'''之前,加入'''echo“

getBids():

".print_r($bidCollection->getBids(),true)."

”;'''让我们知道你得到了什么。 –

+0

你可以将''''bidCollection-> getBids()'''移到foreach外面,并将它的值放入一个变量中,这样它只被调用一次。 –

+0

getBids()返回对象的数组。它所做的只是返回Bid对象的集合,然后使用foreach提取每个对象并显示其内容。 – Salim

回答

0

首先,我要感谢我的队友谁试图帮助我早。问题是我有另外的功能,我忘了张贴在这里bidExist()

public function bidExist($appId , $id){ 


     $sql = "SELECT * FROM accountBid WHERE application_id = :appId AND _customer_id = :id"; 
     try { 

      $sth = $this->_db->getConnection()->prepare($sql); 
      $sth->bindValue(':appId', $appId); 
      $sth->bindValue(':id', $id); 
      $sth->execute(); 

     } catch (Exception $e) { 
      $this->setAlert('danger', 'Information Presentation Error: ' . $e->getMessage()); 
     } 

     foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $data) { 
      $this->addBid($data); 
     } 

     return $sth->rowCount(); 


    } 

以上通过收集循环检查,看看如果该对象在此之前存在,如果是,那么你不能添加的功能不再是出价,但是,我忘记删除此方法的addBid()函数,并导致对象被添加两次到集合。这让我整晚都搞不清楚。但是我感谢所有试图支持和协助的人。