从数据库中获取信息MYSQL&笨

时间:2012-11-12 02:39:03

标签: php mysql database codeigniter

上一个问题在这里,你们需要其他信息:

Fetching information from the database

另一个更新

虽然你们所有人都非常乐于助人。我已经设法获得输出,但我仍然真的很困惑让我说我想首先看到外面所以我无视控制器。

我看到我的命令是正确的后我会做一个控制器:)我不知道为什么,但那就是我如何理解整个概念。反正..

对我来说 的 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;
}
}

和我的 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>&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>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 &nbsp;
<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>&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>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">');?>

那就是我的所有代码。我正在尝试从数据库中获取first_name,last_name,email_address和登录用户的所有其他信息。

如果我在模型中使用$data[]=$row;而不是$data=$row;,则会出现错误:

Severity: Notice

Message: Trying to get property of non-object

Filename: views/members_area.php

Line Number: 20

那么,我应该怎么做。 我在没有控制器的情况下进行编码。我只是查看结果 谢谢!!

2 个答案:

答案 0 :(得分:0)

有一些事情可以澄清:

  • model是一个持久存储在数据库中的实体。在您的情况下:会员
  • controller负责加载模型,并将其传递给要显示的视图
  • view只应该有html标记,没有函数,没有逻辑。

数据库

假设您的数据库具有以下结构。

+---------------+-----------+------------+--------------------+
| id_membership | username  | name       | email              |
+---------------+-----------+------------+--------------------+
|             1 | Marishka  | marishkapv | marishka@email.com |
|             2 | johndoe   | John       | john@doe.com       |
|             3 | dennisv   | Dennis     | dennis@v.com       |
+---------------+-----------+------------+--------------------+

模型

您可以在class Membership extends CI_Model文件中构建模型/application/models/membership.php

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;
        }
    }
}

控制器

假设您有一个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);
    }
}

看起来模型已加载抛出这两行:

//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(); 

浏览

正如您在控制器代码中所述,有两个视图文件login_viewresult_view。第一个将显示一个html表单,您可以在其中放置用户名以触发选择查询。 result_view根据所选成员资格显示成员的信息

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>

答案 1 :(得分:0)

这可以非常简单地阅读用户指南以获得更简单的示例

Model

Class Members Extends CI_Model
{
    function __construct(){
        parent::__construct();
    }

    function member_here()
    {
            $this->db->where('username',$this->input->post('username'));
        $query  =   $this->db->get('membership');

        if($query->num_rows() > 0) 
        {
            return $query->result();
        }
    }
}

Controlelr 

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);
    }
}

And in View you can simple do it

<?php
foreach($member as $row){
?>
<option value="<?php echo $row->id?>"><?php echo $row->name?></option>
<?php
}
?>