i.one i.one - 2 months ago 5
PHP Question

show data by session in codeigniter

how to display data by session in codeigniter,
i have sample in php like this:



<?php

include "../admin/fungsi/koneksi.php";
$profil=$con->prepare("select * from pasien where username=?");
$profil->execute(array($_SESSION["username"]));
$db=$profil->fetch(PDO::FETCH_LAZY);

echo "
<img src='../gambar/12.jpg' alt='HTML5 Icon' style='width:1129px;height:164px;'>
<left style='font-size:28px;color:blue'><i>Profil</i></left><hr color='blue' />
<table style='color:blue;'>

<tr><td>Nama</td><td>:</td><td>".$db["nama"]."</td></tr>
<tr><td>Username</td><td>:</td><td>".$db["username"]."</td></tr>
<tr><td>Jenis Kelamin</td><td>:</td><td>".$db["jk"]."</td></tr>
<tr><td>Alamat</td><td>:</td><td>".$db["alamat"]."</td></tr>
</table>";
?>




Answer

first autoload session libaray at config/autoload.php ,

$autoload['libraries'] = array('database','session','form_validation');
  1. store session data

    $admin_id=2; $data = array('admin_id' => $admin_id); $this->session->set_userdata($data);

  2. fetch session data

    $admin_id=$this->session->userdata('admin_id');