嘿伙计们我试图在我的数据库中显示一个图像,但是在我的配置文件控制器中收到错误消息:未定义的偏移:0在线18。我使用代码点火器框架。
个人资料查看:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="<?php echo base_url('css/mystyle.css'); ?>" type="text/css"/>
<link rel="stylesheet" type="text/css" href="<? echo base_url('assets/css/mystyle.css');?>" />
<link rel="stylesheet" href="<?php echo base_url()?>assets/css/mystyle.css" type="text/css">
<?php
$autoload['helper'] = array('css_js');?>
</head>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="mascot">
<a class="navbar-brand" href="#">
<img src="<?php echo base_url()?>assets/img/monstercode.png" alt="Green Monster Mascot" style="width:160px;height:32px;"></a>
</div>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="active"><a href="<?php echo site_url('')?>">Home</a></li>
<li><a href="<?php echo site_url('Site/sites')?>">Projects</a></li>
<li><a href="<?php echo site_url('Site/developers')?>">Developers</a></li>
<li><a href="<?php echo site_url('Site/employers')?>">Employers</a></li>
<li><a href="#">Contact</a></li>
<?php if ($this->session->userdata('login')){ ?>
<li class="right nav navbar-nav navbar-right"><p class="navbar-text">Hello <?php echo $this->session->userdata('uname'); ?></p></li>
<li class="right nav navbar-nav navbar-right"><a href="<?php echo base_url(); ?>index.php/home/logout">Log Out</a></li>
<?php } else { ?>
<li><a href="<?php echo base_url(); ?>index.php/login">Login</a></li>
<li><a href="<?php echo base_url(); ?>index.php/signup">Signup</a></li>
<?php } ?>
</ul>
</div>
</div>
</nav>
<div class="container-fluid text-center">
<div class="row content">
<div class="col-sm-2 sidenav">
<div class="well">
<p>User Picture <br/><br/><br/><br/><br/><br/><br/><br/><br/></p>
</div>
<?php echo form_open_multipart('profile/do_upload'); ?>
<?php echo form_upload('userfile'); ?><br />
<?php echo form_submit('upload', 'Upload');?>
<?php echo form_close(); ?>
<div class="well">
<p>User Info<br/> Name: <?php echo $uname; ?><br/> Email: <?php echo $uemail; ?> <br/><br/><br/><br/><br/><br/><br/><br/><br/></p>
</div>
</div>
<div class="col-sm-8 text-left">
<div class="container">
<div class="row">
<div class="col-md-4">
<h4>Profile Summary</h4>
<hr/>
<form method="post" action="<?php echo site_url('profile/update');?>">
<p>Name: <input type="text" name='name'value="<?php echo $uname; ?>"></p>
<p>Email: <input type="text" name='email' value="<?php echo $uemail; ?>"></p>
<button type="submit" class="btn btn-default">Update</button>
</form>
<form method="post" action="<?php echo site_url('profile/delete');?>">
<button type="submit" class="btn btn-default">Delete</button>
</form>
<p>...</p>
</div>
<div class="col-md-8">
<p>lorem ipsum dolum</p>
<p>lorem ipsum dolum</p>
<p>lorem ipsum dolum</p>
<p>...</p>
</div>
</div>
</div>
<hr>
<h3>More User Info</h3>
<p>Lorem ipsum...</p>
</div>
<div class="col-sm-2 sidenav">
<div class="well">
<p>Job Offers <br/><br/><br/><br/><br/><br/><br/><br/><br/></p>
</div>
<div class="well">
<p>Job Offers <br/><br/><br/><br/><br/><br/><br/><br/><br/></p>
</div>
</div>
</div>
</div>
<?php if(isset( $upload_data['file_name'] )){ ?>
<img alt="Your uploaded image" src="<?=base_url(). 'assets/img/' . $upload_data['file_name'];?>">
<?php } ?>
<script type="text/javascript" src="<?php echo base_url("assets/js/jquery-1.10.2.js"); ?>"></script>
<script type="text/javascript" src="<?php echo base_url("assets/js/bootstrap.js"); ?>"></script>
</body>
</html>
配置文件控制器:
<?php
class Profile extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('url', 'html', 'form'));
$this->load->library(array('session', 'form_validation'));
$this->load->database();
$this->load->model('user_model');
}
function index()
{
$details = $this->user_model->get_user_by_id($this->session->userdata('uid'));
//error $data['uname'] = $details[0]->firstname . " " . $details[0]->lastname;
//error $data['uemail'] = $details[0]->email;
$this->load->view('profile_view', $data);
}
function delete()
{
$this->user_model->delete_row($this->session->userdata('uid'));
redirect('Site/index');
}
function update()
{
$this->user_model->update($this->input->post("name"), $this->session->userdata('uid'));
$this->user_model->update($this->input->post("email"), $this->session->userdata('uid'));
$this->user_model->add_image($this->input->post('userfile'), $this->session->userdata('uid'));
redirect('profile/index');
}
public function index1()
{
$this->load->view('profile_view', array('error' => ' '));
}
public function do_upload()
{
$config['upload_path'] = './assets/img';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$details = $this->user_model->get_user_by_id($this->session->userdata('uid'));
$data['uname'] = $details[0]->firstname . " " . $details[0]->lastname;
$data['uemail'] = $details[0]->email;
if (!$this->upload->do_upload('userfile')) {
$data['error'] = $this->upload->display_errors();
$this->load->view('profile_view', $data);
} else {
$upload_data = $this->upload->data();
$images_name = $upload_data['file_name'];
//update user photo intro database
$this->user_model->add_image($images_name, $this->session->userdata('uid'), $upload_data['file_name']);
$data['upload_data'] = $upload_data;
$data['uid'] = $this->session->userdata('uid');
$this->load->view('profile_view', $data);
}
}
}
用户模型:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class user_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function get_user($email, $password)
{
$this->db->where('email', $email);
$this->db->where('password', $password);
$query = $this->db->get('user');
/* Select * from users where email = $_Post['email'] and password = $_post['password'] */
return $query->result();
}
// get user
function get_user_by_id($id)
{
$this->db->where('id', $id);
//select id from users where id = $_Post['id']
$query = $this->db->get('user');
return $query->result();
}
// insert
function insert_user($data)
{
return $this->db->insert('user', $data);
}
//delete row
function delete_row($id)
{
$this->db->where('id', $id);
$this->db->delete('user');
}
function update($uemail, $id)
{
$this->db->where('id', $id);
$this->db->set('email', $uemail);
$this->db->update('user');
}
function add_image($uimage, $id){
$this->db->where('id', $id);
$this->db->set('image', $uimage);
$this->db->update('user');
}
}
?>
请帮助我从今天早上起就被困在这里的任何人。谢谢你的帮助。
答案 0 :(得分:0)
在模型函数get_user_by_id($id)
中,您没有返回数组。默认情况下,该结果是一个对象。您需要在控制器中循环显示该结果。
function index()
{
$details = $this->user_model->get_user_by_id($this->session->userdata('uid'));
foreach ($details as $row) {
$data['uname'] = $row->firstname;
$data['uemail'] = $row->email;
}
$this->load->view('profile_view', $data);
}
或者您可以切换模型函数来返回数组。这是一篇很好的Stack Overflow文章:result() vs. result_array()。希望这会有所帮助。
答案 1 :(得分:0)
当返回空数组时,很可能你没有处理这种情况。
你可以这样做:
if( isset($details[0]) ){
$data['uname'] = $details[0]->firstname . " " . $details[0]->lastname;
$data['uemail'] = $details[0]->email;
}else{
// handle empty array
}
有关PHP&#39; isset()
的更多信息