Codeigniter foreach循环行插入表

时间:2018-09-07 20:33:34

标签: php codeigniter

我在前端有一个表单,我试图将值提取到隐藏的输入元素中,然后尝试插入表中。但这不起作用,我收到未定义变量user_id_ins的错误,我正在从get_autocomplete函数将值提取到隐藏的输入元素中。

你们能帮助我实现这一目标吗?

控制器

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

class AddMeeting extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->database();
        $this->load->model('meetinginsert');
    }

    function index() {
        $this->form_validation->set_rules('organisationName', 'Organisation name', 'required');

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('addmeeting');
        } else {

        $data = array(
            'organisation_name' => $this->input->post('organisationName'),
            'user_id' => $this->input->post('user_id')
        );

        $this->meetinginsert->meeting_insert($data);
        $data['message'] = 'Meeting added';
        $this->load->view('addmeeting', $data);
        }
    }

    function get_autocomplete() {
        if(isset($_GET['term'])) {
            $result = $this->meetinginsert->search_user($_GET['term']);
            if(count($result) > 0) {
                foreach($result as $row) {
                    $user_id_ins = $row->id;
                    $arr_result[] = $row->first_name . " " . $row->last_name;
                    echo json_encode($arr_result);
                }
            }
        }
    }
}
?>

查看

<input type="hidden" name="user_id" value="<?php echo $user_id_ins; ?>">

1 个答案:

答案 0 :(得分:0)

假设您正在向视图中输入import numpy as np import matplotlib.pyplot as plt import matplotlib.transforms # GENERATE DATA x = np.arange(-2.*np.pi,4.*np.pi,0.01) sin_arr = [np.sin(x-dx) for dx in np.arange(0.,2.*np.pi,0.5)] # SET FIGURE SIZE AND RCPARAMETERS plt.rcParams.update( {'figure.figsize' : [5.90551197, 3.64980712]} ) params = { 'text.usetex' : True, 'backend' :'ps', ## FONTS "font.family" : "serif", "font.serif" : [], # blank entries should cause plots to inherit fonts from the document "font.monospace" : [], ## FONT SIZES 'axes.labelsize' : 12, 'font.size' : 12, 'legend.fontsize': 12, 'xtick.labelsize': 12, 'ytick.labelsize': 12, ## LINEWIDTH 'axes.linewidth' : 0.5, 'patch.linewidth': 0.5, # legend frame 'lines.linewidth': 1.5, ## LEGEND 'legend.edgecolor':'black', 'legend.frameon' :True, 'legend.fancybox' :False, } plt.rcParams.update(params) # GENERATE PLOT fig = plt.figure() ax = fig.add_subplot(111) for i,sin in enumerate(sin_arr): ax.plot(x,sin,label=r'$\sin(x)$ '+str(i)) ax.set_xlabel(r'$\varepsilon$') ax.set_ylabel(r'$\sigma$ in [MPa]', labelpad=15) ax.set_xlim([0.,2.*np.pi]) # SHRINK PADDING plt.tight_layout(pad=0) # ADD LEGEND ON TOP OF AXES WITHOUT CHANGING AXES SIZE legend = ax.legend( bbox_to_anchor=(0., 1.02, 1., .102), loc='lower left', ncol=3, borderaxespad=0.,mode="expand" ) # GET LEGEND / AXES HEIGHT IN INCHES ax_box = ax.get_window_extent() # AXES BBOX IN DISPLAY UNITS leg_box = legend.get_bbox_to_anchor() # LEGEND BBOX IN DISPLAY UNITS ax_box_inch = ax_box.transformed( fig.dpi_scale_trans.inverted() ) # TRANSFORM TO INCHES leg_box_inch = leg_box.transformed( fig.dpi_scale_trans.inverted() ) # TRANSFORM TO INCHES # ORIGINAL_AX_HEIGHT ax_height_inch_orig = ax_box_inch.height # CHANGE FINGURE TO FIT LEGEND fig.set_size_inches(fig.get_figwidth(), fig.get_figheight() + leg_box_inch.height) # GET NEW HEIGHT OF AXES ax_box_new_inch = ax.get_window_extent().transformed( fig.dpi_scale_trans.inverted() ) ax_height_inch_new = ax_box_new_inch.height factor = ax_height_inch_orig/ax_height_inch_new # GET AXES BBOX IN FIGURE COORDINATES ax_box = ax.get_window_extent().transformed( fig.transFigure.inverted() ) # CHANGE AXES TO ORIGINAL HEIHGT BUT WITH LEGEND FULLY VISIBLE ax.set_position([ax_box.x0, ax_box.y0,ax_box.width, ax_box.height*factor]) plt.savefig('test.pdf',format='pdf',dpi=90) ,则错误是因为$arr_result中没有包含$user_id_ins,因此没有传递给视图。

$arr_result(您现在拥有的方式)只是一个一维数组,其中每个元素只是名称和姓氏的串联。您需要传递两个参数,例如:

$arr_result

然后,您可以通过调用$arr_result[] = (user_id_ins => $user_id, fullname => $row->first_name . " " . $row->last_name); $user_id_ins(或您愿意使用的任何名称)来访问视图中的两个字段