2017-05-07 153 views
0

iam正在爲網站做一個搜索條件,我面對的問題是,如果在數據庫中我有更多的驅動程序的同名,我的查詢只返回最後一個它記錄發現, 首先IAM從用戶獲取輸入,並在表中調用驅動程序搜索,含有相同的,因爲他進入輸入,代碼記錄:後我的laravel查詢返回的只是數據庫中的最後一條記錄

if(!empty($name)||!empty($policynum)||!empty($platenum)||!empty($platecode)) // ** if one of name or policy# or platenum or platecode is set ** // 
{ 
$query = DB::table('drivers'); 
if(!empty($name)) 
$query -> where('driverName',$name); 
if(!empty($policynum)) 
$query -> where('policyNumber',$policynum); 
if(!empty($platenum)) 
$query -> where('plateNumber',$platenum); 
if(!empty($platecode)) 
$query -> where('plateCode',$platecode); 

$result = $query->get(); 
$result=json_encode($result,true); 
$result=json_decode($result,true); 
}        // --- end of if 4 inputs set --- // 

我得到的司機,我有一個屬性在司機的數據庫中稱爲事故ID,所以現在我必須去事故數據庫表,並尋找事故ID與事故ID相同,代碼:

if(count($result)>0 && empty($expname)) // if result array returns >0 results and exp-name empty // 

{ 
for($x=0;$x<count($result);$x++) 
$accident= accident::where('id',$result[$x]['accidentId']) 
->get(); 
return $accident; 
} 

因此iam使用for循環來遍歷驅動程序表中的所有結果,查看驅動程序表中是否有任何結果在事故表中具有相同的id,並且如果發現我想要獲取所有事故行,並返回它,我把它放在一個表,如下所示:

<tr ng-repeat="accident in accidents" > <!-- --> 
       <td>{{accident.accidentRecord}}</td> 
       <td>{{accident.expertId}}</td> 
       <td>{{accident.address}}</td> 
       <td>{{accident.date}} -- -- {{accident.time}}</td> 
       <td>{{accident.completedDate}}</td> 

和事故的js代碼,如果需要的話:

$scope.search=function(accident) 
{ 
    $http({ 
     method:'GET', 
     url:'getAccident', 
     params: {accident:accident} 
    }) 
     .success(function(data){ 
      $scope.accidents=data; 
      console.log(data); 
     }) 
     .error(function(err){ 
      console.log(err); 
     }) 
}; 

但該表只顯示最後一行在我的數據庫有相同的ID,不是所有的行,都可以有人幫忙嗎?

PHP函數輸出($事故):

[[{ 「ID」:1, 「accidentFloor」: 「」, 「accidentRecord」: 「rexord試驗」, 「地址」:「哈雷特hreik 「,」location「:」no location「,」region「:」「,」colleague「:」「,」policeMan「:」「,」roadDirection「:」「,」roadType「:」「,」roadNumbers「: 「」, 「roadWidth」: 「」, 「日期」: 「2017年4月12日」, 「時間」: 「16時28分29秒」, 「願景」: 「」, 「weatherCondition」: 「」,「expertId 「:1,」accidentGenerationNumber「:」2017-04-1216:28:291「,」completedDate「:」「,」freeDescription「:」「,」created_at「:」2017-04-12 16:28:29「 ,「updated_at」:「2017-04-12 16:28:29」,「deleted_at」:null}],[{「id」:1,「accidentFloor」:「」,「accidentRecord」:「rexord test」, 「address」:「haret hreik」,「location」:「no location」,「region」:「」,「colleague」:「」,policeman「:」「,」roadDirection「:」「,」roadType「: 「」,「R oadNumbers 「:」」, 「roadWidth」: 「」, 「日期」: 「2017年4月12日」, 「時間」: 「16時28分29秒」, 「願景」: 「」, 「weatherCondition」: 「」 ,「expertId」:1,「accidentGenerationNumber」:「2017-04-1216:28:291」,「completedDate」:「」,「freeDescription」:「」,「created_at」:「2017-04-12 16:28 :29「,」updated_at「:」2017-04-12 16:28:29「,」deleted_at「:null}],[{」id「:1,」accidentFloor「:」「,」accidentRecord「:」rexord測試「,」地址「:」haret hreik「,」location「:」no location「,」region「:」「,」colleague「:」「,」policeMan「:」「,」roadDirection「:」「, roadType 「:」」, 「roadNumbers」: 「」, 「roadWidth」: 「」, 「日期」: 「2017年4月12日」, 「時間」: 「16時28分29秒」, 「願景」: 「」 「weatherCondition」: 「」, 「expertId」:1, 「accidentGenerationNumber」: 「2017-04-1216:28:291」, 「completedDate」: 「」, 「freeDescription」: 「」, 「created_at」:「2017 「-04-12 16:28:29」,「updated_at」:「2017-04-12 16:28:29」,「deleted_at」:null}],[{「id」:3,「accidentFloor」:「」 ,「accidentRecord」:「bir al abed」,「address」:「borj」,「location」:「33.8591245,35。5110078" , 「區域」: 「」, 「同事」: 「」, 「警察」: 「」, 「roadDirection」: 「」, 「roadType」: 「\ u0645 \ u0646 \ u0639 \ u0637 \ u0641」,「roadNumbers 「:」 2" , 「roadWidth」: 「」, 「日期」: 「2017年5月1日」, 「時間」: 「13點21分26秒」, 「願景」: 「」, 「weatherCondition」: 「」 ,「expertId」:1,「accidentGenerationNumber」:「2017-05-0113:21:261」,「completedDate」:「」,「freeDescription」:「」,「created_at」:「2017-05-01 13:21 :26" , 「的updated_at」: 「2017年5月1日13時21分26秒」, 「deleted_at」:空}]]

控制檯輸出:

(4)[陣列( 1),陣列(1),陣列(1),陣列(1)]
: Array(1)
: 陣列(1)
: 陣列(1)
: 陣列(1)
長度 : : 陣列(0)

回答

1

Your for loop

for($x=0;$x<count($result);$x++) 
$accident= accident::where('id',$result[$x]['accidentId']) 
->get(); 

已經結束寫同一個$accident變量。因此你只能在變量中獲得最後一條記錄。
更改,如果條件和循環,這樣的:

$accidents = []; 
if(count($result)>0 && empty($expname)) // if result array returns >0 results and exp-name empty // 
{ 
    for($x=0;$x<count($result);$x++) 
     $accidents[] = accident::where('id',$result[$x]['accidentId'])->get(); 
    return $accidents; 
} 

$accidents包含所有事故的數據。

+0

多數民衆贊成那是正確的,我試圖添加$事故[$ x],所以它可能會將它們添加到數組,但仍然沒有工作 – mahditrad

+1

試試我上面寫的代碼。 – gaganshera

+0

好吧,這有點幫助,我現在得到我的表3行,這是在我的數據庫中包含該名稱的行數相同,但作爲第二個問題,表是空的,因爲我在控制檯中看到,它返回3個數組,但是爲空 – mahditrad

相關問題