0

前の質問は、他の情報が必要な場合に備えてここにあります。

データベースからの情報の取得

別の更新

みなさんはとても助かりましたが。私はなんとか出力を得ることができましたが、私はまだ本当に本当に混乱しています。最初にアウトパーを見たいので、コントローラーを無視します。

コマンドが正しいことを確認した後、コントローラーを作成します:)理由はわかりませんが、それがコンセプト全体を理解する方法の一種です。とりあえず..

私の 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

だから、私は何をすべきか。私はコントローラーなしでコーディングしています。結果をチェックしているだけです ありがとう!

4

2 に答える 2

0

明確にできることがいくつかあります。

  • Amodelは、データベースに保持されるエンティティです。あなたの場合:メンバーシップ
  • Acontrollerはモデルのロードを担当し、表示するビューに渡します。
  • 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);
    }
}

モデルがロードされているように見えると、次の2行がスローされます。

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

ビュー

コントローラコードで述べたように、2つのビューファイルとがlogin_viewありresult_viewます。最初のフォームには、selectクエリをトリガーするためにユーザー名を入力できる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>
于 2012-11-12T04:50:40.630 に答える
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
}
?>
于 2012-11-12T06:07:33.650 に答える