我想将$ distance变量的结果显示到视图文件中的表。 但是我的脚本中出现错误,有人可以帮助解决此错误吗? 我还想从最小的距离对$ distance变量的结果进行排序。 错误出现在第38行中,该行在我的视图文件中的foreach函数中。
这是我的控制器文件中的脚本:
function index()
{
$center_lat = -7.2574719;
$center_lng = 112.7520883;
$data= $this->m_metode->tampil_data_fotografer();
{
foreach($data->result_array() as $row)
{
$lat=(float)$row['latitude'];
$lng=(float)$row['longitude'];
// $distance= $lat * $center_lat;
$distance['tb_fotografer'] =( 6371 * acos((cos(deg2rad($center_lat)) ) * (cos(deg2rad($lat))) *
(cos(deg2rad($lng) - deg2rad($center_lng)) )+ ((sin(deg2rad($center_lat))) *
(sin(deg2rad($lat))))));
// var_dump($distance);
}
}
$this->load->view('public/pencari/v_tampil_rekomendasi',$distance);
}
我的视图:
<?php
$no = 1;
foreach($tb_fotografer->result_array() as $fg){
?>
<tr>
<td><?php echo $no++ ?></td>
<td><?php echo $fg->nama ?></td>
<td><?php echo $fg->alamat ?></td>
<td><?php echo $fg->kamera ?></td>
<td><?php echo $fg->spesifikasi_foto ?></td>
<td><?php echo $fg->keahlian_foto ?></td>
<td><?php echo $fg->latitude ?></td>
<td><?php echo $fg->longitude ?></td>
</tr>
<?php } ?>
答案 0 :(得分:0)
此变量“ $ tb_fotografer”存储浮点值而不是数组,并且您不能使用result_array()访问。您必须将其存储为这样的数组:
$distance['tb_fotografer'][] = ( 6371 * acos((cos(deg2rad($center_lat)) ) * (cos(deg2rad($lat))) *
(cos(deg2rad($lng) - deg2rad($center_lng)) )+ ((sin(deg2rad($center_lat))) *
(sin(deg2rad($lat))))));
根据您的视图字段,您需要将$ data发送到视图文件。
$this->load->view('public/pencari/v_tampil_rekomendasi',['tb_fotografer' => $data->result_array()]);
并在您的视图文件中:
foreach($tb_fotografer as $fg){
对于排序,您可以将数据存储在另一个变量中,例如$ output,并在其上放一些距离,然后可以使用以下方法进行排序:usort($ array,“ cmp”); 完成所有步骤后,您就可以发送到视图了。
最终代码:
function index()
{
$center_lat = -7.2574719;
$center_lng = 112.7520883;
$data= $this->m_metode->tampil_data_fotografer();
$out = []
{
foreach($data->result_array() as $row)
{
$lat=(float)$row['latitude'];
$lng=(float)$row['longitude'];
// $distance= $lat * $center_lat;
$distance =( 6371 * acos((cos(deg2rad($center_lat)) ) * (cos(deg2rad($lat))) *
(cos(deg2rad($lng) - deg2rad($center_lng)) )+ ((sin(deg2rad($center_lat))) *
(sin(deg2rad($lat))))));
$out[] = array_merge($row, ['distance' => $distance]);
}
}
// @todo use usort for sort base on distance
$this->load->view('public/pencari/v_tampil_rekomendasi',['tb_fotografer' => $out]);
}
答案 1 :(得分:0)
您在每次迭代中都覆盖了$distance['tb_fotografer']
变量,并且还访问了数组数据上的对象。
尝试像这样修改控制器:
function index()
{
$center_lat = -7.2574719;
$center_lng = 112.7520883;
$data= $this->m_metode->tampil_data_fotografer();
{
foreach($data->result_array() as $key => $row)
{
$lat=(float)$row['latitude'];
$lng=(float)$row['longitude'];
// $distance= $lat * $center_lat;
$distance['tb_fotografer'][$key][] = $row; // get all of the preserved result_array data
$distance['tb_fotografer'][$key][]['distance'] = ( 6371 * acos((cos(deg2rad($center_lat)) ) * (cos(deg2rad($lat))) *
(cos(deg2rad($lng) - deg2rad($center_lng)) )+ ((sin(deg2rad($center_lat))) *
(sin(deg2rad($lat))))));
// var_dump($distance);
}
}
$this->load->view('public/pencari/v_tampil_rekomendasi',$distance);
}
v_tampil_rekomendasi
视图如下:
<?php
$no = 1;
foreach($tb_fotografer as $fg){
?>
<tr>
<td><?php echo $no++ ?></td>
<td><?php echo $fg['nama'] ?></td>
<td><?php echo $fg['alamat'] ?></td>
<td><?php echo $fg['kamera'] ?></td>
<td><?php echo $fg['spesifikasi_foto'] ?></td>
<td><?php echo $fg['keahlian_foto'] ?></td>
<td><?php echo $fg['latitude'] ?></td>
<td><?php echo $fg['longitude'] ?></td>
<td><?php echo $fg['distance'] ?></td>
</tr>
<?php } ?>