Welcome Guest, Not a member yet? Register   Sign In
help in getting and displaying info from database
#1

[eluser]kalelgwapo[/eluser]
i already know how to input data into my database but i just couldnt get it to display the info. I'm stuck in reading the user_guide and i dont know what to do, here's my codes from my view, model and controller.

from the controller (i tried to test and input the $username if it will display in the views, but it doesnt)
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profilepage extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $username = $this->input->post('username');
        $this->load->model('Profile');
        $this->Profile->getinfo($username);
        $this->load->view('profile');
    }
}

from the views
Code:
<form name="form1" method="post" action="">
<div>
    <div align="center"><a href="profilepic"><img src="CI doesnt allow me to post links" width="120" height="120" alt="Photo Here" /></a></div>
  </div>
  &lt;?php echo $username?&gt;
  <div><p>Name:&lt;?php echo $firstname?&gt; &lt;?php echo $lastname?&gt;</p>
  <p>Email Address:&lt;?php echo $email?&gt;</p>
  <p>Birthdate:&lt;?php echo $bday?&gt;</p>
  <p>Messages</a></p>
  <p>Friends</a></p></div>
&lt;/form&gt;

and from the model
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Profile extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function getinfo($username)
    {
      $dsn = 'CI doesnt allow me to post links';
      $this->load->database($dsn);        
      $query = $this->db->query("select username from users");
      return $firstname = $query->row();
    }

}

?&gt;

im fairly a beginner in CI and doesnt understand it much, i hope you can help me
#2

[eluser]Ben Swinburne[/eluser]
You need to pass the information to the view.

Code:
// getinfo() returns an object so either pass it directly
// or save is as a variable then pass it (like the example below)
$data = $this->Profile->getinfo($username);
$this->load->view('profile', $data);
#3

[eluser]kalelgwapo[/eluser]
thank you, i did not know that and i have been waiting for a reply, thank you so much you have helped me greatly, i will try this right now
#4

[eluser]Ben Swinburne[/eluser]
In response to a private message, for the benefit of any other readers.

Quote:hey thank you for helping me out. but i have a question
was my model correct? what i was trying to do was get the firstname from my db and then display it in my view but it doesnt, where did i do wrong?

Typically I'd have achieved what you're trying to do like this:

Model
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profile extends CI_Model {  
    function get_info($username) {
      $this->load->database($dsn);
      return $this->db->select('firstname')->get_where('users', array('username', $username))->result_array();
    }

}

?&gt;

Controller
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profilepage extends CI_Controller {
    function index() {
        $this->load->model('Profile');
        if( $this->input->post('username') ) {
                $data = $this->Profile->get_info($this->input->post('username'));
                $this->load->view('profile', $data);
        } else {
                show_error("You've not submitted a username");
        }
    }
}

?&gt;

View
Code:
&lt;?php echo $firstname; ?&gt;

I've not commented it but if you have any questions, just ask (in the thread).
#5

[eluser]kalelgwapo[/eluser]
thank you so much. works perfectly!
#6

[eluser]Ben Swinburne[/eluser]
Do you understand what I changed to make it work so that you've learned for next time? I spoonfed you a bit there so thought I'd better ask if you understood why it works! Smile

Ben
#7

[eluser]kalelgwapo[/eluser]
i dont know how to say this but ill try my best

in the model you used a built in function in CI to query and returned an array (this i did not understand why you have to return an array)

in the controller you put $this->input->post('username') into the paramaters of get_info instead of $username, probably because its better
and when i load my view, you added $data in the parameter so that the view(profile) will use it, this I did not and thank you for pointing it out to me Smile




Theme © iAndrew 2016 - Forum software by © MyBB