PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 5, 2022

[FIXED] PHP: Display Session variable in View: CodeIgniter

 March 05, 2022     codeigniter, php, session     No comments   

Issue

As mention in the title I am trying to send the Session variables in the View Section after I login, The Session contains let's say name and email.

So what I have tried in the Profile Controller Section is below:

class ProfileController extends Controller{
public function index()
{ 
    $session = session();

    $userDetails = ['name' => $session->get('name'),
                     'email' => $session->get('email')];
    print_r($userDetails);
    echo view('profile', $userDetails); 
}

}

although I am able to print the $userDetails value in the controller Section, the same I am trying in VIEW is giving me nothing.

here's my profile.php code in view.

<h3>Welcome to the Profile Section : <?php echo $userDetails['name']; ?></h3>
<p>Your email id : <?php echo $userDetails['email'] ; ?></p>

The output I am getting is :

Result Image

Please correct me if I am missing something as I am new to the PHP frameworks.


Solution

In your View you would just have the array named indexes as they are converted to variables.

So $userDetails['name'] becomes $name and $userDetails['email'] becomes $email

So in your case. Instead of

<h3>Welcome to the Profile Section : <?php echo $userDetails['name']; ?></h3>
<p>Your email id : <?php echo $userDetails['email'] ; ?></p>

You would have.

<h3>Welcome to the Profile Section : <?php echo $name; ?></h3>
<p>Your email id : <?php echo $email ; ?></p>

And you can replace <?php echo with the shorter version <?=

<h3>Welcome to the Profile Section : <?= $name; ?></h3>
<p>Your email id : <?= $email ; ?></p>

I would strongly suggest to read the CodeIgniter Userguide as it covers this and everything else the framework does. So it's good to get familiar with it.



Answered By - TimBrownlaw
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing