从数据库MYSQL& Codeigniter [英] Fetching information from the database MYSQL & Codeigniter
问题描述
上一个问题是在这里,你需要一个其他信息:
其他更新
虽然你们都非常有帮助。我设法得到输出,但我仍然真的很困惑让我说,我想先看到outpurs,所以不考虑控制器。
我会做一个控制器后,我看到我的命令是正确的:)我不知道为什么,但是这是我如何理解整个概念。
p>
function member_here()
{
$ this-> db-> select('');
$ this-> db-> from('membership');
$ this-> db-> where('username',$ this-> input-> post('username'));
$ q = $ this-> db-> get('');
if($ q-> num_rows()> 0){
$ data = array();
foreach($ q-> result()as $ row){
$ data = $ row;
}
return $ data;
}
}
和我 signup_view.php
< html>
< head>
< title>< / title>
< / head>
< body>
<?php $ this-> load-> view('includes / header')?>
< div align =right><?php echo anchor('login / signin','Login');?>< / div>
<?php
$ option1 = array(
'name'=>'你的童年绰号是什么? '=>'你的童年昵称是什么?'
);
$ option2 = array(
'name'=>'你的宠物名字是什么?',
'value'=> pet $\\ name?'
);
$ option3 = array(
'name'=>'你在哪个城市出生?',
'value'=>'在你出生的城市?'
);
$ option4 = array(
'name'=>'你的眼睛的颜色是什么?',
'value'=>你的眼睛?'
);
$ option5 = array(
'name'=>'你最喜欢的颜色是什么?',
'value'=>'你最喜欢的颜色是什么?
);
echo form_open('login / create_member');
echo< table width = \80%\ cellpadding = \3px \>
< tr>
< td>用户信息:< / td>
< / tr>
< tr>
< td>& nbsp;< / td>
< / tr>;
echo< tr>< td> First Name *:< / td>< / tr>;
echo< tr> ;< td>;
echo form_input('first_name',set_value('first_name'));
echo< / tr>< / td>;
echo < tr>< td>姓氏*:< / td>< / tr>;
echo< tr>< td>;
echo form_input('last_name' ,set_value('last_name'));
echo< / td>< / tr>;
echo< tr>< td>电子邮件地址*:< / td& < / tr>;
echo< tr>< td>;
echo form_input('email_address',set_value('email_address'));
echo / tr>< / td>< / tr>;
echo< tr> td>;
?>
< input type =radioname =gendervalue =Male/> Male& nbsp;
& radioname =gendervalue =女性/>女性
<?php
echo< / tr>< / td>;
echo< tr>
< td>< / td>
< / tr>
< / table>;
?>
<?php
echo< table width = \\80%\ cellpadding = \3px \>
< tr>
< td>帐户信息:< / td>
< / tr&
< tr>
< td>& nbsp;< / td>
< / tr>
echo< tr>< td> Userame *:< / td>< / tr>;
echo< tr>< td>;
echo form_input('username',set_value('username'));
echo< / td>< / tr>;
echo< tr>< td>密码*:< / td>< / tr>;
echo< tr>< td>;
echo form_password('password');
echo< / td>< / tr>;
echo< tr>< td>密码确认*:< / td>< / tr>;
echo< tr>< td>;
echo form_password('password2');
echo< / td>< / tr>;
echo< tr>< td>;
echo安全问题*:;
echo< / td>< / tr>;
echo< tr>< td>;
?>
< select name =security_questionvalue =security question>
< option value =你的童年绰号是什么?>你的童年昵称是什么?< / option>
< option value =你的宠物的名字是什么?>你的宠物的名字是什么?< / option>
< option value =您出生在哪个城市?>您出生在哪个城市?< / option>
< option value =你的眼睛的颜色是什么?>你的眼睛的颜色是什么?< / option>
< option value =你最喜欢的颜色是什么?>你最喜欢的颜色是什么?< / option>
< / select>
<?php
echo< / td>< / tr>;
echo< tr>< td>;
echo安全答案*:;
echo< / td>< / tr>;
echo< tr>< td>;
echo form_input('security_answer');
echo< / td>< / tr>;
echo< tr>< td>;
echo form_submit('submit','Register');
echo form_reset('reset','Reset');
echo< / tr>< / td>;
echo form_close();
echo< tr>
< td>< / td>
< / tr>
< / table>
?>
<?php echo validation_errors('< p class =error>');?>
所以,我的所有代码。我想要获取first_name,last_name,email_address和登录用户的所有其他信息,以便从数据库中显示出来。
如果我在我的模型中使用 $ data [] = $ row;
而不是 $ data = $ row;
发生错误时说:
严重性:注意
消息:尝试获取非对象的属性
文件名:views / members_area .php
行号:20
所以,我该怎么办。 我编码没有控制器。我只是检查结果 谢谢!
可以澄清的事情:
- A
模型
数据库。在您的情况下:会员 -
控制器
负责加载模型,要显示的视图 - A
视图
只能有html标记,没有函数,没有逻辑。
数据库
假设您的数据库具有以下结构。
+ --------------- + ----------- + ------ ------ + -------------------- +
| id_membership |用户名|名称|电子邮件|
+ --------------- + ----------- + ------------ + ---- ---------------- +
| 1 | Marishka | marishkapv | marishka@email.com |
| 2 | johndoe | John | john@doe.com |
| 3 | dennisv |丹尼斯| dennis@v.com |
+ --------------- + ----------- + ------------ + ---- ---------------- +
模型
你可以建立你的模型 class Membership extends CI_Model
在 /application/models/membership.php
文件。
class Membership extends CI_Model {
function __construct b $ b {
parent :: __ construct();
}
function member_here()
{
$ this-> db-> select('');
$ this-> db-> from('membership');
$ this-> db-> where('username',$ this-> input-> post('username'));
$ q = $ this-> db-> get('');
if($ q-> num_rows()> 0)
{
$ data = array();
foreach($ q-> result()as $ row)
{
$ data = $ row;
}
return $ data;
}
}
}
控制器
假设您在 example.com/index.php/login
有一个登录页面,它应该驻留在 /application/controllers/login.php
如下:
class Login extends CI_Controller
{
function __construct()
{
parent :: __ construct();
}
public function index()
{
$ this-> load-> view('login_view');
}
public function result()
{
$ this-> load-> model('membership');
$ data ['member'] = $ this-> membership-> member_here();
$ this-> load-> view('result_view',$ data);
}
}
看起来模型被加载抛出这两行:
//使用其函数加载模型
$ this-> load-> model 'membership');
//加载给定用户名的行whit
//将结果赋给具有键member的数组,在视图中您访问此
//值$ member var
$ data ['member'] = $ this-> membership-> member_here();
视图
代码,有两个视图文件, login_view
和 result_view
。第一个将显示一个html表单,您可以放置用户名以触发select查询。 result_view
根据所选成员显示成员的信息
login_view.php
< html>
< head>< / head>
< body>
< form action =login / resultmethod =post>
输入您的用户名:< input type =textname =username/>
< input type =submitvalue =从数据库加载/>
< / form>
< / body>
< / html>
result_view.php
< h4>详细信息:< / h4>
< p>
Id:<?= $ member-> id_membership?> < br />
用户名:<?= $ member-> username?> < br />
电子邮件:<?= $ member->电子邮件?>
< / p>
the previous question is here incase you guys needed a other information:
Fetching information from the database
ANOTHER UPDATE
Although all of you have been very helpful. I've managed to get outputs but i'm still really really confused lets say that I want to first see the outpurs so im disregarding the controller.
i'll make a controller after i see that my commands are correct :) i dont know why but thats kindo of how i get to understand the whole concept. anyway..
for my membership_model.php
function member_here()
{
$this->db->select('');
$this->db->from('membership');
$this->db->where('username',$this->input->post('username'));
$q=$this->db->get('');
if($q->num_rows() > 0) {
$data = array();
foreach($q->result() as $row) {
$data=$row;
}
return $data;
}
}
and my signup_view.php
<html>
<head>
<title></title>
</head>
<body>
<?php $this->load->view('includes/header')?>
<div align="right"><?php echo anchor('login/signin', 'Login');?></div>
<?php
$option1 = array(
'name' => 'What was your childhood nickname?',
'value' => 'What was your childhood nickname?'
);
$option2= array(
'name' => 'What is your pet\'s name?',
'value' => 'What is your pet\s name?'
);
$option3= array(
'name' => 'In what city were you born?',
'value' => 'In what city were you born?'
);
$option4= array(
'name' => 'What is the color of your eyes?',
'value' => 'What is the color of your eyes?'
);
$option5= array(
'name' => 'What is your favorite color?',
'value' => 'What is your favorite color?'
);
echo form_open('login/create_member');
echo "<table width=\"80%\ cellpadding=\"3px\">
<tr>
<td>User Information:</td>
</tr>
<tr>
<td> </td>
</tr>";
echo "<tr><td>First Name*: </td></tr>";
echo "<tr><td>";
echo form_input('first_name', set_value('first_name'));
echo "</tr></td>";
echo "<tr><td>Last Name*: </td></tr>";
echo "<tr><td>";
echo form_input('last_name', set_value('last_name'));
echo "</td></tr>";
echo "<tr><td>Email Address*: </td></tr>";
echo "<tr><td>";
echo form_input('email_address', set_value('email_address'));
echo "</tr></td>";
echo "<tr><td>Gender: </td></tr>";
echo "<tr><td>";
?>
<input type="radio" name="gender" value="Male" />Male
<input type="radio" name="gender" value="Female" />Female
<?php
echo "</tr></td>";
echo "<tr>
<td></td>
</tr>
</table>";
?>
<?php
echo "<table width=\"80%\ cellpadding=\"3px\">
<tr>
<td> Account Information:</td>
</tr>
<tr>
<td> </td>
</tr>";
echo "<tr><td>Userame*: </td></tr>";
echo "<tr><td>";
echo form_input('username', set_value('username'));
echo "</td></tr>";
echo "<tr><td>Password*: </td></tr>";
echo "<tr><td>";
echo form_password('password');
echo "</td></tr>";
echo "<tr><td>Password Confirmation*: </td></tr>";
echo "<tr><td>";
echo form_password('password2');
echo "</td></tr>";
echo "<tr><td>";
echo "Security Question*:";
echo "</td></tr>";
echo "<tr><td>";
?>
<select name = "security_question" value="security question">
<option value = "What was your childhood nickname?">What was your childhood nickname?</option>
<option value = "What is your pet's name?">What is your pet's name?</option>
<option value = "In what city were you born?">In what city were you born?</option>
<option value = "What is the color of your eyes?">What is the color of your eyes?</option>
<option value = "What is your favorite color?">What is your favorite color?</option>
</select>
<?php
echo "</td></tr>";
echo "<tr><td>";
echo "Security Answer*:";
echo "</td></tr>";
echo "<tr><td>";
echo form_input('security_answer');
echo "</td></tr>";
echo "<tr><td>";
echo form_submit('submit', 'Register');
echo form_reset('reset', 'Reset');
echo "</tr></td>";
echo form_close();
echo "<tr>
<td></td>
</tr>
</table>";
?>
<?php echo validation_errors('<p class="error">');?>
So, thats all my code. I'm trying to get the first_name, last_name, email_address and all the other information of the logged in user to come out from the database.
if i use $data[]=$row;
instead of $data=$row;
in my model an error occurs saying this:
Severity: Notice
Message: Trying to get property of non-object
Filename: views/members_area.php
Line Number: 20
So, what should I do. i'm coding without the controller. i'm just checking the results thanks!!
There are a few things that could be clarified:
- A
model
is a entity that persists in the database. In your case: Membership - A
controller
is in charge of load the model, and pass it to the view to be showed - A
view
only should have html markup, no functions, no logic.
Database
Assuming that your database have the structure below.
+---------------+-----------+------------+--------------------+
| id_membership | username | name | email |
+---------------+-----------+------------+--------------------+
| 1 | Marishka | marishkapv | marishka@email.com |
| 2 | johndoe | John | john@doe.com |
| 3 | dennisv | Dennis | dennis@v.com |
+---------------+-----------+------------+--------------------+
Model
You could build your model class Membership extends CI_Model
at /application/models/membership.php
file.
class Membership extends CI_Model {
function __construct()
{
parent::__construct();
}
function member_here()
{
$this->db->select('');
$this->db->from('membership');
$this->db->where('username',$this->input->post('username'));
$q = $this->db->get('');
if($q->num_rows() > 0)
{
$data = array();
foreach($q->result() as $row)
{
$data=$row;
}
return $data;
}
}
}
Controller
Supposing that your have a login page at example.com/index.php/login
, this should resides at /application/controllers/login.php
as below:
class Login extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('login_view');
}
public function result()
{
$this->load->model('membership');
$data['member'] = $this->membership->member_here();
$this->load->view('result_view', $data);
}
}
Look like the models is loaded throws this two lines:
//Load the model in make use of its functions
$this->load->model('membership');
//load the row whit the given username
//the result is assigned to array with the key "member", in the view you access this
//value with $member var
$data['member'] = $this->membership->member_here();
Views
As you noted in controller code, there are two views files, login_view
and result_view
. The first one will show a html form where you can put the username in order to trigger a select query. The result_view
show the informacion of the member according to the selected membership
login_view.php
<html>
<head></head>
<body>
<form action="login/result" method="post">
Type your username: <input type="text" name="username" />
<input type="submit" value="Load from database" />
</form>
</body>
</html>
result_view.php
<h4>Details:</h4>
<p>
Id: <?=$member->id_membership?> <br />
Username: <?=$member->username?> <br />
Email: <?=$member->email?>
</p>
这篇关于从数据库MYSQL& Codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!