我正在开发Codeigniter上的应用程序,并试图从我的数据库表实现搜索功能。我被这3个错误/警告困住了。
1.(A PHP Error was encountered
Severity: Notice
Message: Undefined index: prtc_search
Filename: controllers/form.php
Line Number: 818)
2.(A PHP Error was encountered
Severity: Notice
Message: Undefined index: prtc_search
Filename: controllers/form.php
Line Number: 818)
3.(A PHP Error was encountered
Severity: Notice
Message: Object of class CI_DB_mysql_result could not be converted to int
Filename: views/prtc_search.php
Line Number: 2)
我使用相同的代码来搜索来自其他数据库表的数据,它工作得很好,没有错误或警告,但是当涉及到这个特定的页面时,相同的代码不工作。如果有人能好心地给我线索或解决方案,我将不胜感激。下面是搜索页面的代码。
//view.php
<?php
if ($result==1)
{
echo "NO DATA FOUND";
}
else
{?>
<div class="table">
<div class="table_header">Edit from the following List of PRTC</div>
<table >
<tr>
<th>ID</th>
<th>Name</th>
<th>Country</th>
<th>Dzongkhag</th>
<th>Geog</th>
<th>Village</th>
<th>Address</th>
<th style="color:#FF2525;width:4%;">Edit</th>
</tr>
<?php
$i=0;
foreach($result as $row)
{
?>
<tr <?php /*if($i%2==0) echo 'style="background-color:#F2DEDE";'; else echo 'style="background-color:#D9EDF7";'; $i++;*/?>>
<td style="vertical-align:middle;"><?php echo $row['id']; ?></td>
<td style="vertical-align:middle;"><?php echo $row['name']; ?></td>
<td style="vertical-align:middle;"><?php echo $row['country']; ?></td>
<td style="vertical-align:middle;"><?php echo $row['dzongkhag']; ?></td>
<td style="vertical-align:middle;"><?php echo $row['geog']; ?></td>
<td style="vertical-align:middle;"><?php echo $row['village']; ?></td>
<td style="vertical-align:middle;"><?php echo $row['address']; ?></td>
<td style="ertical-align:middle;">
<a href = "<?php echo base_url().'form/prtc_one/'.$row['id']; ?>">
<img src="<?php echo base_url();?>images/edit.png" title="Edit"></a>
</td>
</tr>
<?php
}
}
?>
</table>
</div>
</div>
<!-- <a style="margin-left:2%;margin-top:20%;" href="<?php echo site_url('form/search_expo/'.$searchquery);?>">Export This..<img style="margin:0 !important;" src="<?php echo site_url('images/b_export.png')?>"></a> -->
控制器//controller
function prtc_one($id)
{
$session_user=$this->session->userdata('logged_in');
if($session_user['ath']=='admin')
{
$data['result'] = $this->search_model->get_prtc_one($id);
$data['session_user'] = $this->session->userdata('logged_in');
$datah['title'] = "Sasec | PRTC Edit"; //For Title
$this->load->view('includes/header',$datah);
$this->load->view('includes/leftmenu',$data);
$this->load->view('includes/flag',$data);
$this->load->view('prtc_edit',$data);
$this->load->view('includes/footer');
}
else
{
echo ' <script type="text/javascript">
alert("Access Error....");
</script>';
redirect('login/home');
}
}
模型//model
function get_prtc_one($id)
{
$query=$this->db->get_where('prtc_info',array('id' => $id));
$query=$query->row_array();
return $query;
数据库查询function prtc_search($searchquery)
{
$count = 0;
//Search using id
$this->db->select('*');
$this->db->order_by('name ASC');
$this->db->where("id LIKE '%$searchquery%'");
$result = $this->db->get('prtc_info');
if ($result->num_rows() > 0)
{
$row = $result->result_array();
$count = 1;
return $result;
}
您的建议或解决方案将不胜感激。谢谢你!控制器function prtc_search()
{
$session_user=$this->session->userdata('logged_in');
if($session_user['ath']=='admin')
{
$list['result'] = $this->search_model->prtc_search($_POST['prtc_search']);
$list['searchquery']=$_POST['prtc_search'];
$data['session_user'] = $this->session->userdata('logged_in');
$datah['title'] = "Sasec | PRTC Search"; //For Title
$this->load->view('includes/header',$datah);
$this->load->view('includes/leftmenu',$data);
$this->load->view('includes/flag',$data);
$this->load->view('prtc_search',$list);
$this->load->view('includes/footer');
}
else{
echo ' <script type="text/javascript">
alert("Access Error....");
</script>';
redirect('login/home');
}
}
用
更正查询function prtc_search($searchquery) {
$count = 0;
//Search using id
$this->db->select('*');
$this->db->like('id', $searchquery);
$this->db->order_by("name", "asc");
$result = $this->db->get('prtc_info');
if ($result->num_rows() > 0) {
$row = $result->result_array();
$count = 1;
return $row;
} else {
return 1;
}
}
也改变这个模型查询
function get_prtc_one($id) {
$query = $this->db->get_where('prtc_info', array('id' => $id));
if ($query->num_rows() > 0) {
$result = $query->row_array();
return $result;
} else {
return 1;
}
}
删除行号818和819的notice
,使用isset()
if(isset($_POST['prtc_search']))
{
$list['result'] = $this->search_model->prtc_search($_POST['prtc_search']);
$list['searchquery']=$_POST['prtc_search'];
}else{
$list['result']="";
$list['searchquery']="";
}
数据库查询部分
函数prtc_search (searchquery美元){$count = 0;
//Search using id
$this->db->select('*');
$this->db->order_by('name ASC');
$this->db->where("id LIKE '%$searchquery%'");
$result = $this->db->get('prtc_info');
if ($result->num_rows() > 0)
{
$row = $result->result_array();
$count = 1;
return $result;
}
else{
return NULL;
}
And for view.php:
if($result != NULL and count($result)){
//write code for result
}
else{
// No record found
}