【发布时间】:2017-10-05 12:07:21
【问题描述】:
我正在为一个网站做一个搜索条件,我面临的问题是,如果在数据库中我有多个同名驱动程序,我的查询只返回它找到的最后一条记录, 首先,我从用户那里获取输入,然后在一个名为 drivers 的表中搜索包含与他输入的输入相同的记录,代码:
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;
}
所以我使用 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 函数输出($accident):
[[{"id":1,"accidentFloor":"","accidentRecord":"rexord test","address":"haret hreik","location":"no location","region": "","同事":"","policeMan":"","roadDirection":"","roadType":"","roadNumbers":"","roadWidth":"","date":" 2017-04-12","time":"16:28:29","vision":"","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":"","roadNumbers":"","roadWidth":" ","date":"2017-04-12","time":"16:28:29","vision":"","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":"","accidentR ecord":"rexord test","address":"haret hreik","location":"no location","region":"","colleague":"","policeMan":"","roadDirection" :"","roadType":"","roadNumbers":"","roadWidth":"","date":"2017-04-12","time":"16:28:29"," vision":"","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","region":"","colleague":"","policeMan ":"","roadDirection":"","roadType":"\u0645\u0646\u0639\u0637\u0641","roadNumbers":"2","roadWidth":"","date":"2017 -05-01","time":"13:21:26","vision":"","weatherCondition":"","expertId":1,"accidentGenerationNumber":"2017-05-0113:21 :261","completedDate":"","freeDescription":"","created_at":"2017-05-01 13:21:26","updated_at":"2017-05-01 13:21:26 ","deleted_at":null}]]
控制台输出:
(4) [数组(1), 数组(1), 数组(1), 数组(1)]
0 : 数组(1)
1 : 数组(1)
2 : 数组(1)
3 : 数组(1)
长度 : 4
原型 : 数组(0)
【问题讨论】:
标签: php mysql angularjs laravel