CodeIgniter Forums
Display welcome message - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Display welcome message (/showthread.php?tid=33479)



Display welcome message - El Forum - 08-27-2010

[eluser]iijb[/eluser]
Hi all,
Im a newbie to codeIgniter. I have a login form. And if I successfully login I have to display a welcome message using session variable and also display some basic information from the database on the homepage. How can I achieve it.
Please help.

Regards
iijb


Display welcome message - El Forum - 08-27-2010

[eluser]mi6crazyheart[/eluser]
I think these 2 things will help to accomplish u'r task...

Codeigniter session: http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
Active Record Class: http://ellislab.com/codeigniter/user-guide/database/active_record.html


Display welcome message - El Forum - 08-27-2010

[eluser]tkyy[/eluser]
the first function to set flashdata (to do a session variable, like an alert)
the second function to pull some info from the database (from a table called users) and load a new view that should show that data in the variable $database_users (just do a foreach loop and iterate over each item echoing it out)

example welcome controller

Code:
<?php
class welcome extends controller{
    function welcome(){parent::controller();}

    function index(){
        $this->load->view('welcome');
    }
    function flash_me(){
        $this->session->set_flashdata(array('alert'=>'here is an alert! display it by looking for flashdata in the view'));
        redirect('welcome');
    }

    function database_users(){
        $data['database_users'] = $this->db->select('*')->from('users')->limit(5)->get()->result_array();
        $this->load->view('welcome_database_result',$data);
    }
}



the example view to go with the database query, i put the flash_show() function into a helper for my projects and just call it where i want to show flashdata.

Code:
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<?php
function flash_show()
{
    $ci = get_instance();
    $alert = $ci->session->flashdata('alert');
    if($alert!=''){
        echo '<code>'.$alert.'</code>';
    }
}
?&gt;

&lt;?php flash_show(); ?&gt;

&lt;?php foreach($database_users as $item){ ?&gt;
    <h1>Showing user #&lt;?=$item['id'];?&gt; in the users table</h1>
    &lt;?=$item['username']?&gt;<br />
    &lt;?=$item['password']?&gt;<br />
&lt;?php } ?&gt;
&lt;/body&gt;

to go with this make a database table called users with an id, username and password field and make a 5 or 6 rows manually just to get a feel for how its accomplished. i do my queries by method chaining one line but usually people don't do that, they will throw it on multiple lines etc this is just my preference.


Display welcome message - El Forum - 08-27-2010

[eluser]iijb[/eluser]
Hi,
Thanks tkyy. Let me check it out.

Thanks again
iijb