0

codeigniters のアクティブ レコードを学習し始めており、コントローラーからモデルに渡されたパラメーターを使用してデータベースにクエリを実行しています。

最初に、コントローラーからモデルにIDを渡していますが、それは機能します。

コントローラ

function bret($id){
$this->load->model('school_model');
$data = $this->school_model->get_city_and_population($id);
foreach ($data as $row)
{
echo "<b>Name Of The City</b>...........". $row['Name'];
echo "<br/>";
echo "<b>Total Population</b>...........".$row['Population'];
}
}

モデル

function get_city_and_population($id){
$this->db->select('Name,Population');
$query = $this->db->get_where('city', array('ID'=>$id));
return $query->result_array();
}

先に進み、失敗することを期待して複数のパラメーターを入力しましたが、これは機能しますが、なぜ機能したのか、何が機能したのかわかりません。

コントローラ

public function parameters($id,$name,$district){
    $this->load->model('school_model');
    $data = $this->school_model->multiple_parameters($id,$name,$district);
    foreach ($data as $row)
    {
    echo "<b>Total Population</b>...........".$row['Population'];
    }
    }

モデル

function multiple_parameters($id,$name,$district){
$this->db->select('Population');
$query = $this->db->get_where('city', array('ID'=>$id,'Name'=>$name,'District'=>$district));
return $query->result_array();
}

私の複数のパラメータの例では、私が訪れましたhttp://example.com/env/at/index.php/frontpage/parameters/7/Haag/Zuid-Holland/

ここで、名前 Haagがid7にあり、地区がZuid-Holland

これが私の質問です.codeigniterはURLからモデルにパラメーターを渡す方法をどのように知っていますか.次に、私が少し間違っていた場合7/Haag/Zuid-Hollandes/、そのURLが間違っていることをユーザーにどのように示し、代わりにデフォルト値にフォールバックしますか.パラメーターが間違っている場合に空白を表示するのは?

4

2 に答える 2

4
//In codeiginter URI contains more then two segments they will be passed to your function as parameters.
//if Url: http://example.com/env/at/index.php/frontpage/parameters/7/Haag/Zuid-Holland/

//Controller: forntpage
public function parameters($id,$name,$district){
   echo $id.'-'$name.'-'.$district;
}

//and if you are manually getting url from segment & want to set default value instead of blank then use following:



public function parameters(
$this->load->helper("helper");
$variable=$this->uri->segment(segment_no,default value);
//$id=$this->uri->segment(3,0);
}

//or
 //Controller: forntpage
 public function parameters($id='defaultvalue',$name='defaultvalue',$district='defaultvalue'){
   echo $id.'-'$name.'-'.$district;
}
于 2013-07-06T09:15:00.983 に答える
1

これは、CI での単純な uri マッピング、または必要に応じて uri param バインディングです。
次のようなメソッドがある場合:

public function something($param1, $param2) {
    // get from: controller/something/first-param/second-param
}

つまり、uri セグメントが引数としてコントローラー メソッドに渡されます。

上記のメソッドは次のように記述できます。

public function something() {
    $param1 = $this->uri->segment(3);
    $param2 = $this->uri->segment(4);
    // segment 1 is the controller, segment 2 is the action/method.
}

CI はこのマッピング以外に何もしないため、uri セグメントがまさに希望どおりのものであるかどうかを手動で確認する必要があることを理解する必要があります。

次に、いくつかのデフォルトが必要な場合は、次のステートメントが当てはまります。

public function something($param1 = 'some default value', $param2 = 'other value') {
// get from: controller/something/first-param/second-param
}

つまり、次のような URL/controller/somethingが渡された場合でも、デフォルト値が返されます。がcontroller/something/test渡されると、最初のパラメーターは URL (テスト) からのパラメーターによってオーバーライドされます。

それだけです。

于 2013-07-06T09:21:56.623 に答える