简体   繁体   中英

How to send values from Controller to model in codeigniter

in the view file "(V_tampil_rekomendasi)", I made an input text containing the latitude and longitude values ​​of a function to check the current location. In the results, I sent it to the file controller and it worked. I checked using the var_dump function. But when I send it (latitude and longitude) to the model file (M_Metode).The values ​​of latitude and longitude were missing. And the results that will be displayed on V_display_ recommendation are missing or nothing to display. This my view file :

<button class="btn btn-primary" onclick="getLocation()">Cek Lokasi Anda</button></p>
<form action="<?php echo base_url().'metode/index'; ?>" method="post">
    <div class="row">
      <div class="col">
        <p class="font-weight-bold">LATITUDE :</p>
        <input class="form-control" type = "text" id = "out_latitude" name="latitude_awal" placeholder="Latitude" readonly/>
      </div>
      <div class="col">
        <p class="font-weight-bold">LONGITUDE :</p>
        <input class="form-control" type = "text" id = "out_longitude" name="longitude_awal" placeholder="Longitude" readonly/>
      </div>
    </div>
    <br>
<input class="btn btn-primary" type="submit" value="Cari Fotografer">
</form>
<table class="table table-bordered" style="margin:20px auto;" border="1">
  <tr>
    <thead class="thead-dark">
      <th>No</th>
      <th>Nama</th>
      <th>Alamat</th>
      <th>Kamera</th>
      <th>Spesifikasi Foto</th>
      <th>Jarak</th>
    </thead>
  </tr>
  <?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['keahlian_foto'] ?></td>
      <td><?php echo $fg['distance']?> Km </td>
    </tr>
  <?php } ?>
</table>

My Javascript to check the current location (in View file) :

<script>
var view_lat = document.getElementById("tampilkan_lat");
var view_long = document.getElementById("tampilkan_long");
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        view.innerHTML = "Yah browsernya ngga support Geolocation bro!";
    }
}
 function showPosition(position) {
   var var_latitude  = position.coords.latitude;
   document.getElementById("out_latitude").value = var_latitude ;
   var var_longitude  = position.coords.longitude;
    document.getElementById("out_longitude").value = var_longitude ;
 }
</script>

This my Controller :

function index()
{
    $latitude = $this->input->post('latitude_awal');
    $longitude = $this->input->post('longitude_awal');
    $data_latlong = array(
        'latitude_awal' => $latitude,
        'longitude_awal' => $longitude);
    $data_fotografer= $this->m_metode->tampil_data_fotografer($data_latlong);
    $this->load->view('public/pencari/v_tampil_rekomendasi',['tb_fotografer' => $data_fotografer]);
}

This my model :

  function tampil_data_fotografer($data_latlong)
  {
    $latFrom = deg2rad((float)('latitude_awal'));
    var_dump($latFrom);
    $lonFrom = deg2rad((float)('longitude_awal'));
    var_dump($lonFrom);

    $data_fg = $this->db->query("SELECT fotografer_id, nama, alamat, kamera, keahlian_foto, latitude, longitude FROM tb_fotografer");
    $out = [];
        {
          foreach($data_fg->result_array() as $row)
          {
            $keahlianfoto = $row['keahlian_foto'];
            $latTo = deg2rad((float)$row['latitude']);
            $lonTo = deg2rad((float)$row['longitude']);

            $latDelta = $latTo - $latFrom;
            $lonDelta = $lonTo - $lonFrom;
            $distance = (2 * asin(sqrt(pow(sin($latDelta / 2), 2) + cos($latTo) * cos($latFrom) * pow(sin($lonDelta / 2), 2)))) * 6371;
              if ($distance <=1)
              {
                $out[] = array_merge($row, ['distance' => $distance]);
              }
          }
        }

    return $out;
  }

Anyone kno what the problem ? Help me please, thanks

The following code (in your model):

$latFrom = deg2rad((float)('latitude_awal')); // (float)('latitude_awal') = 0
var_dump($latFrom);
$lonFrom = deg2rad((float)('longitude_awal')); // (float)('longitude_awal') = 0
var_dump($lonFrom);

should be like this:

$latFrom = deg2rad((float) $data_latlong['latitude_awal']);

$lonFrom = deg2rad((float) $data_latlong['longitude_awal']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM