0

我在 CI 视图页面中收到错误,因为为 foreach() 提供的参数无效。这是我的代码:



我的数据库:(彭耶瓦表)

**id_penyewa                        
nama_penyewa                                 
alamat                               
no_telp**

(贾米南表)

**id_penyewa                        
jenis_jaminan
ket_jaminan**

在控制器(penyewa.php)

function Penyewa()
{
    parent::Controller();
    $this->load->database();
    $this->load->model('model_tampil');
}

function tambah()
{
    $ck=$this->input->post('id_penyewa');
        if($ck!='')
        {
            $this->model_tampil->insertPenyewa();
        }
    $this->load->view('tampilpenyewa');
}

function sukses()
{
echo "Data berhasil di input!";
?>
<br />
<a href="<?php echo base_url();?>/index.php/login/input_penyewa">Tambah Data</a>
<br />
<a href="<?php echo base_url();?>/index.php/login/tampil_penyewa">Lihat Data</a>
<?php
}

在模型中(model_tampil.php)

function getPenyewa(){
    $this->db->select('*');
    $this->db->from('penyewa');
    $this->db->join('jaminan','jaminan.id_penyewa = penyewa.id_penyewa');
    $q = $this->db->get();
    $rows = $q->num_rows(); 
    $q_result = $q->result();

    if($rows>0){
        foreach($q_result as $row){
            $data[] = $row;
        }
        return $data;
    }
}
function insertPenyewa()
{   
    /*$this->db->trans_start();

    $this->db->query('INSERT INTO penyewa VALUES($id_penyewa, $nama_penyewa, $alamat, $no_telp, $jenis_jaminan)');
    $table1_id = $this->db->insert_id();

    $this->db->query('INSERT INTO jaminan VALUES(id_penyewa,' . $table1_id .',jenis_jaminan)');

    $this->db->trans_complete(); */

    $this->id_penyewa=$this->input->post('id_penyewa');
    $this->nama_penyewa=$this->input->post('nama_penyewa');
    $this->alamat=$this->input->post('alamat');
    $this->no_telp=$this->input->post('no_telp');
    $this->jenis_jaminan=$this->input->post('jenis_jaminan');

    $this->db->insert('penyewa',$this); 
            redirect('penyewa/sukses');
}

在视图中 (tampilPenyewa.php)

    <center>
<h3>Tabel data Penyewa</h3>
<table border="1">
<tr align="center" bgcolor="#33CC99">
<td width="100">ID Penyewa</td>
<td width="200">Nama Penyewa</td>
<td width="120">Alamat</td>
<td width="150">Nomor Telepon</td>
<td width="150">Jenis Jaminan</td>
<td>Tindakan Lanjut</td>
</tr>

<?php foreach ($records as $row) : ?>
<tr height="35">
<td>&nbsp;<?php echo $row->id_penyewa; ?></td>
<td>&nbsp;<?php echo $row->nama_penyewa; ?></td>
<td>&nbsp;<?php echo $row->alamat; ?></td>
<td>&nbsp;<?php echo $row->no_telp; ?></td>
<td>&nbsp;<?php echo $row->ket_jaminan; ?></td>
<td></td>
</tr>
<?php endforeach; ?>
</table>
<br />

<a href="<?php echo base_url();?>/index.php/login/input_penyewa">Tambah Data</a>

并在视图中(inputpenyewa.php)

<center>
<?php

$this->load->library('validation');
$id_penyewa=array(
              'name'          => 'id_penyewa',
              'id'            => 'id_penyewa',
              'value'         => '',
              'maxlength'     => '100',
              'size'          => '50',
              'validation'    => "required");
$nama_penyewa=array(
              'name'          => 'nama_penyewa',
              'id'            => 'nama_penyewa',
              'value'         => '',
              'maxlength'     => '100',
              'size'          => '50',
              'validation'    => "required");
$alamat=array(
              'name'          => 'alamat',
              'id'            => 'alamat',
              'value'         => '',
              'maxlength'     => '500',
              'size'          => '',
              'validation'    => "required");
$no_telp=array(
              'name'          => 'no_telp',
              'id'            => 'no_telp',
              'value'         => '',
              'maxlength'     => '100',
              'size'          => '50',
              'validation'    => "required");
$ket_jaminan=array(
              'name'          => 'jenis_jaminan',
              'id'            => 'jenis_jaminan',
              'value'         => '',
              'maxlength'     => '100',
              'size'          => '50',
              'validation'    => "required");

$this->load->helper('form');
echo validation_errors();
echo form_open('penyewa/tambah');
echo '<center><h3>Input Data Penyewa</h3></center>';
echo "<table border='0' class='tabledetail' align='center'>";
echo
  "<tr>"."<td>".form_label('ID')."</td>"."<td>".form_input('id_penyewa')."</td>"."</tr>";
echo
  "<tr height=50>"."<td>".form_label('Nama Penyewa')."</td>"."<td>".form_input('nama_penyewa')."</td>"."</tr>";
echo
  "<tr height=220>"."<td>".form_label('Alamat')."</td>"."<td>".form_textarea('alamat')."</td>"."</tr>";
echo
  "<tr>"."<td>".form_label('No Telp')."</td>"."<td>".form_input('no_telp')."</td>"."</tr>";
echo
  "<tr height=220>"."<td>".form_label('Jaminan')."</td>"."<td>".form_textarea('jenis_jaminan')."</td>"."</tr>";
echo
  "<tr height=50>"."<td colspan=2 align='center'>".form_submit('mysubmit','Simpan')."</td>"."</tr>";
echo "</table>";
echo form_close();
?>
<a href="<?php echo base_url();?>/index.php/login/tampil_penyewa">Lihat Data</a>


好吧,如果我的问题非常非常多,我很抱歉..谢谢你:)

更新:

好的,我已经这样做了,但没有任何结果,我的页面再次错误..

而且,我想再问一次,model_tampil->getPenyewa() 有什么错误吗?

这是脚本:

 function getPenyewa(){

    $this->db->select('*');
    $this->db->from('penyewa');
    $this->db->join('jaminan','jaminan.id_penyewa = penyewa.id_penyewa');
    $q = $this->db->get();
    $rows = $q->num_rows(); 
    $q_result = $q->result();

    if($rows>0){
        foreach($q_result as $row){
            $data[] = $row;
        }
        return $data;
    }
}

我想从 1 页 inputpenyewa.php 将数据输入到我的本地主机中的 2 个表中。

4

2 回答 2

0

You are not passing any data to the view.

$this->load->view('tampilpenyewa');

change this line to this.

$this->load->view('tampilpenyewa',$data);

You have to get the values from db. So before this line ,get value from db as

$data['records'] = $this->model_tampil->getPenyewa();  

and then load the view as above, like

$this->load->view('tampilpenyewa',$data);

Hope this helps

Regards

iijb

于 2012-12-26T05:59:07.880 回答
0

In the tambah function of your controller,you are directly calling the view file without giving any data to it..

Do it like this..

function tambah()
{
    $ck=$this->input->post('id_penyewa');
        if($ck!='')
        {
            $this->model_tampil->insertPenyewa();
        }
    $records = $this->model_tampil->getPenyewa(); // The array returned from your whatever model function
    $this->load->view('tampilpenyewa',array('records' => $records));
}
于 2012-12-26T05:59:20.557 回答