gpt4 book ai didi

php - 从数据库获取数据时,CodeIgniter 出现 "Undefined variable"错误

转载 作者:行者123 更新时间:2023-11-29 11:02:33 26 4
gpt4 key购买 nike

从数据库获取数据时,我在 codeigniter 中遇到错误。

这是我的错误:

A PHP Error was encountered

Message: Undefined variable: results

Line Number: 122

这是我的代码:

  <?php $via = $this->session->userdata('firm_name');
if(!isset($firm_name)){ redirect ('Welcome');}
?>
<link href="<?php echo base_url("assets/css/radiostyle.css"); ?>" rel="stylesheet">
<style type="text/css">
.page-heading {
border-top: 0;
padding: 0px 10px 10px 10px;
}
input, button, select, textarea{
margin: 10px;
}
.ibox-content {
background-color: #ffffff;
color: inherit;
padding: 15px 20px 20px 20px;
border-color: #e7eaec;
border-image: none;
border-style: none;
border-width: 1px 0px;
}
</style>

<div class="row wrapper border-bottom white-bg page-heading">
<div style="float: left;" class="col-lg-4">
<img style=" width: 90px;
height: 90px; padding-top: 5px;" src="http://www.miisky.com/ci/GST.png" alt="GST Logo">

</div>
<div class="col-lg-4 text-center">
<h1 style=" padding-top: 7px;
padding-right: 20%;">Know Your Customers</h1>

</div>
<div style="float: right;" class="col-lg-4">
<h1 style="padding-top:9px"><?php echo "Date: ". date("d/m/Y");?></h1>

</div>
</div>
<br><div style="text-align: center;">
<label class="control control--radio">Manufacture
<input type="radio" onclick="document.location='#';" name="radio" checked="checked"/>
<div class="control__indicator"></div>
</label>
<label class="control control--radio">Service
<input type="radio" onclick="document.location='#';" name="radio" />
<div class="control__indicator"></div>
</label>
<label class="control control--radio">Trading
<input type="radio" onclick="document.location='#';" name="radio" />
<div class="control__indicator"></div>
</label>
<label class="control control--radio">Others
<input type="radio" onclick="document.location='#';" name="radio" />
<div class="control__indicator"></div>
</label>



</div>
<div class="fh-breadcrumb">

<div class="full-height">
<div class="full-height-scroll white-bg border-left">
<?php echo $this->session->flashdata('msg'); ?>
<div class="element-detail-box">

<div class="tab-content">
<div class="ibox-content">

<div class="form-group">

<div class="col-lg-12">
<div class="row">

<?php $this->load->helper('form'); ?>
<?php $attributes = array("name" => "Categoryform","autocomplete"=>"off");
echo form_open("Gst_customer/customersearch/", $attributes);?>
<div class="col-lg-6">
<input name="CustomerName" type="text" placeholder="CustomerName" value="<?php echo set_value('CustomerName'); ?>" class="form-control" required>
<span class="text-danger"><?php echo form_error('CustomerName'); ?></span>
</div>


<div class="col-lg-12">
<?php echo form_submit('Search', 'Search','class="btn btn-info"'); ?>
</div>

<?php echo form_close(); ?>

</div>

</div>
</div>

</div>





</div>

</div>

</div>
</div>
<div class="full-height">
<div class="full-height-scroll white-bg border-left">
<?php echo $this->session->flashdata('message'); ?>
<div class="element-detail-box">
<div class="table-responsive">
<?php if(isset($results)){ ?>
<table class='table table-bordered'>
<thead>
<tr>
<th> Customer Name </th>
<th> Customer Id</th>
<th> Customer Code</th>
</tr>
</thead>
<tbody>
<?php foreach($results as $row){ ?>
<tr>
<td><?php echo $row->CustomerName;?></td>
<td><?php echo $row->CustomerID;?></td>
<td><?php echo $row->CustomerCode;?></td>
</tr>

<?php } ?>


</tbody>
</table>
<?php } ?>

</tbody>
</table>












</div>
</div>

</div>
</div>


</div>

这是我的 Controller 文件:

<?php
class Gst_customer extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('url','html'));
$this->load->library('session');
$this->load->database();
$this->load->model('user_model');

}

function index()
{
$details = $this->user_model->get_user_by_id($this->session->userdata('id'));

$data['firm_name'] = $details[0]->firm_name;
$data['email'] = $details[0]->email;
$data['vault_no'] = $details[0]->vault_no;
$data['active'] = 'know_your_customer';
$data['mobile'] = $details[0]->mobile;

$this->load->view('view_header', $data);
$this->load->view('view_menu', $data);
$this->load->view('gst_customer_view', $data);
$this->load->view('view_footer');
}

function customersearch()
{
$CustomerName = $this->input->post('CustomerName');
$data['results'] = $this->user_model->search($CustomerName);
$results = $data['results'];
$this->load->view('view_header', $data);
$this->load->view('view_menu', $data);
$this->load->view('gst_customer_view', $data);
$this->load->view('view_footer');
}

function logout()
{
$user_data = $this->user_model->get_user_by_id($this->session->userdata('id'));
foreach ($user_data as $key => $value) {
if ($key != 'session_id' && $key != 'ip_address' && $key != 'user_agent' && $key != 'last_activity') {
$this->session->unset_userdata($key);
}
}
$this->session->sess_destroy();

redirect('Welcome', 'refresh');
}
}

这是我的模型:module.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model
{
function __construct()
{
parent::__construct();
}



function search($CustomerName,$CustomerID)
{
$this->db->like('CustomerName',$CustomerName);
$this->db->like('CustomerID',$CustomerID);
$query = $this->db->get('gst_customermaster');
return $query->result();
}

}?>

最佳答案

在您的 View 中。如果设置了 $results 数组,则显示表格。因为您正在从不同的函数向同一 View 发送不同的数据。

像这样..

<?php if(isset($results)){ ?>
<table class='table table-bordered'>
<thead>
<tr>
<th> Customer Name </th>
<th> Customer Id</th>
<th> Customer Code</th>
</tr>
</thead>
<tbody>
<?php foreach($results as $row){ ?>
<tr>
<td><?php echo $row->CustomerName;?></td>
<td><?php echo $row->CustomerID;?></td>
<td><?php echo $row->CustomerCode;?></td>
</tr>

<?php } ?>


</tbody>
</table>
<?php } ?>

关于php - 从数据库获取数据时,CodeIgniter 出现 "Undefined variable"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41972606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com