在这里我想使用 codeigniter 在 url 中传递 id

here i want to pass the id in url using codeigniter

这是我在视图中的代码。我想在 url 中传递 id 值,但它显示以下错误

Severity: Notice

Message: Trying to get property of non-object

Filename: views/cameralist.php

查看代码

<?php
foreach($field as $f)
if($f !="id"){
{?>
<div class="values"><?php
echo $f."<br>"; ?></div><br><br><br>
<?php 
echo "<a href = '".base_url()."index.php/product/edit/".$f->id."'>Edit</a>"; 
echo "<a href = '".base_url()."index.php/product/delete/".$f->id."'>Delete</a>"; 
}
}
?>

有什么建议吗?

谢谢

型号代码

function get_field(){
     $result = $this->db->list_fields('camera_details');
     foreach($result as $field){    
            $data[] = $field;
     }
    return $data;
}

控制器代码

function cameralist(){
    $this->load->model('p_model');
    $data['field'] = $this->p_model->get_field();
    $this->load->view('cameralist',$data);
}

您的型号代码

function get_field(){
$query = $this->db->query("SELECT * FROM camera_details");
return $query->result();
}

查看代码

foreach ($field as $f)
{   echo "<td>".$f->id."</td>"; 
    echo "<a href = '".base_url()."index.php/product/edit/".$f->id."'>Edit</a>"; 
    echo "<a href = '".base_url()."index.php/product/delete/".$f->id."'>Delete</a>"; 
}
?>

更改模型中的代码。现在可以使用了

list_fields() 将回显提供的数据库 table 名称的列名称。这就是为什么它会返回一个错误,提示您无法获取字符串的偏移量。

因此将您的型号代码更改为:

function get_field()
{
   return $this->db->field_data('camera_details');
}

此外,您的字段将作为对象返回,因此请坚持使用对象 属性 语法:

"index.php/product/edit/".$f->id.

将来如果您 运行 遇到不知道返回什么的问题,您应该尝试像这样转储结果,例如:

$result = $this->db->list_fields('camera_details');
var_dump(results)

会显示以下内容:

array(5) {
  [0]=>
  string(2) "id"
  [1]=>
  string(4) "name"
  [2]=>
  string(5) "price"
  [3]=>
  string(10) "lennsensor"
  [4]=>
  string(5) "thick"
}