0

I'm building a application using codeigniter, this application has a profile page where all the information displayed is dependant on the user currently logged in. On the profile page I'm trying to set up a bio box so each user can write their own unique bio, this would be stored in the database. The code I've currently written is displaying a result on the page, but its not the correct result I was hoping for. The code I've written that relates to the problem is below.

In the model:

public function get_profile_information() { 

    $query = $this->db->query("SELECT profileText from users WHERE 
    idNumber = ?", $this->session->userdata('username'));

    return $query->result_array();
}

In the controller:

public function loginprocess() { 

    $username = $this->input->post('idNumber');
    $password = $this->input->post('passWd');

    $query = $this->db->query("select * from users where idNumber='".$username."' and passWd='$password'");

    $row = $query->num_rows();

    if ($row) {

        $this->session->set_userdata(array('username' => $username));
        $sessionID = $this->session->userdata('username');

        $data['title'] = 'Profile';
        $data['profiletext'] = $this->news_model >get_profile_information();
        $data['sessionID'] = $this->session->userdata('username');

        $this->load->view('templates/header', $data);
        $this->load->view('news/profile', $data);
        $this->load->view('templates/footer');

    } else { 

        $data['error'] = 'Invalid Login Details';
        $data['title'] = 'Login';

        $this->load->view('templates/header', $data);
        $this->load->view('news/login', $data);
        $this->load->view('templates/footer');
    }
}

In the view:

<head>

<style type="text/css">

</style>

</head>

Welcome to the <?php echo $title; ?> page, <?php echo $sessionID; ?>

<?php var_dump($profiletext); ?>

<?php echo anchor('login/logout', 'logout'); ?>

On the webpage, the query to get the profile text from the database based on the logged in user gives this result

array(1) { [0]=> array(1) { ["profileText"]=> string(17) "Test Profile Text" } } 

I was hoping just to get the "Test Profile Text" displayed. I've been unable to work out what is wrong, I'm fairly new to codeigniter and php so I don't have the knowledge to fix this issue.

2
  • You should parameterize your second query as you did with your first. You should store your passwords as hashes, not plain text. var_dump is for debugging. You have an array use it as such, echo $profiletext['profileText']; Commented Mar 31, 2019 at 23:14
  • if you are serious about writing an application open to the public, with different users accessing different parts of your application/database: please have a look at this: github.com/benedmunds/CodeIgniter-Ion-Auth Commented Mar 31, 2019 at 23:18

2 Answers 2

1

Controller:
Make little change in below line

//$data['profiletext'] = $this->news_model >get_profile_information();//old line
$data['profiletext'] = $this->news_model->get_profile_information();//changes

Model:
To get only "Test Profile Text", without any change in your view

public function get_profile_information() { 

    $query = $this->db->query("SELECT profileText from users WHERE idNumber = ".$this->session->userdata('username'));
    return $query->row('profileText');//changes
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should use echo instead of var_dump()

Description:

var_dump ( mixed $expression [, mixed $... ] ) : void This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.

All public, private and protected properties of objects will be returned in the output unless the object implements a __debugInfo() method (implemented in PHP 5.6.0).

Replace

<?php var_dump($profiletext); ?>

By

<?php echo $profiletext[0]['profileText]; ?>

echo prints the variable(s) directly, as we require in our sites.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.